当前位置:网站首页>2022 Henan Mengxin League game (2): Henan University of technology d-pair
2022 Henan Mengxin League game (2): Henan University of technology d-pair
2022-07-25 00:00:00 【WA_ automata】
D - Number pair
The topic requires us to find satisfaction a l + a l + 1 + … + a r − 1 + a r ≤ x + y × ( r − l + 1 ) al+al+1+…+ar−1+ar ≤ x+y×(r−l+1) al+al+1+…+ar−1+ar≤x+y×(r−l+1) Number pair ( l , r ) (l, r) (l,r) Of ( a l − y ) + ( a l + 1 − y ) + … + ( a r − 1 − y ) + ( a r − y ) ≤ x (al−y)+(al+1−y)+…+(ar−1−y)+(ar−y) ≤ x (al−y)+(al+1−y)+…+(ar−1−y)+(ar−y)≤x, Make b i = a i − y bi = ai − y bi=ai−y, On the array b b b Preprocess to get prefix and array s u m sum sum, The formula above
Can become s u m r − s u m l − 1 < = x sumr − suml−1 <= x sumr−suml−1<=x, And then you get s u m r − x < = s u m l − 1 sumr − x <= suml−1 sumr−x<=suml−1, such
We can maintain a tree array on the value field , Maintain the number of prefixes and values in front of each
and , Enumerate each right endpoint r r r, Add the answers to the tree array every time, which is greater than or equal to s u m r − x sumr −x sumr−x Total number of , That is, all the left boundaries that meet the above conditions , And then to s u m r sumr sumr The number of this position plus 1 1 1 .
Because the range of values is very large , Tree array cannot be opened , So we need to discretize the prefix and array first .
Time complexity : O ( n l o g ( n ) ) O(nlog(n)) O(nlog(n))
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 200010;
int n;
LL a[N],x,y;
int tr[N*2];
vector<LL> numbers;
int lowbit(int x)
{
return x&-x;
}
void add(int x,int v)
{
for(int i=x;i<=(int)numbers.size();i+=lowbit(i))
tr[i]+=v;
}
int query(int x)
{
int ans=0;
for(int i=x;i>0;i-=lowbit(i))
ans+=tr[i];
return ans;
}
int get(LL x)// Get the value after discretization
{
// The tree array is from 1 At the beginning , So it must be added 1, Ensure that all subscripts are from 1 Start
return lower_bound(numbers.begin(),numbers.end(),x)-numbers.begin()+1;
}
int main()
{
scanf("%d%lld%lld",&n,&x,&y);
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
for(int i=1;i<=n;i++) a[i]+=a[i-1]-y;
numbers.push_back(0);// Left boundary l yes 1-n, But what we inquired about is sumr-suml-1, So we need to sum[0] Put it in the discretized array
for(int i=1;i<=n;i++)
{
// Discretize all prefixes and and values used in subsequent queries , Ensure that the size relationship between them is correct
numbers.push_back(a[i]);
numbers.push_back(a[i]-x);
}
sort(numbers.begin(),numbers.end());// Discrete operation
numbers.erase(unique(numbers.begin(),numbers.end()),numbers.end());
add(get(0),1);// For the same reason, first sum[0] Add to the tree array
LL ans=0;
for(int i=1;i<=n;i++)
{
ans+=i-query(get(a[i]-x)-1);
// Find greater than or equal to... In the tree array sumr-x Total number of , That is, the number of left boundary satisfying the condition
add(get(a[i]),1);
}
printf("%lld\n",ans);
return 0;
}
边栏推荐
- Leetcode 0125. validate palindrome string
- Multithreading & high concurrency (the latest in the whole network: interview questions + map + Notes) the interviewer is calm
- The new version of SSM video tutorial in shangsilicon valley was released
- Processing of ffmpeg wasapi can't activate audio endpoint error
- Only by learning these JMeter plug-ins can we design complex performance test scenarios
- LeetCode_ 392_ Judgement subsequence
- 91. (leaflet chapter) leaflet situation plotting - offensive direction drawing
- Leetcode 0123. the best time to buy and sell stocks III: dynamic programming + simulation in constant space
- Pit record: typeerror:'module'object is not callable
- Answers to some problems encountered in the process of Xperia XZ (f8332) brushing and root
猜你喜欢

Add a little surprise to life and be a prototype designer of creative life -- sharing with X contestants in the programming challenge
Simple message queue implementation nodejs + redis =mq

C language program environment and preprocessing

Use es to realize fuzzy search and search recommendation of personal blog

ROS manipulator movelt learning notes 3 | kinect360 camera (V1) related configuration

91. (leaflet chapter) leaflet situation plotting - offensive direction drawing

UART

Exception, import package and file operation

Notes of Teacher Li Hongyi's 2020 in-depth learning series 6

SQL file import database - Nanny level tutorial
随机推荐
[Nuxt 3] (十)运行时配置
MySQL common basic commands
Technical operation
Video chat source code - one-to-one live broadcast system source code
Notes of Teacher Li Hongyi's 2020 in-depth learning series 8
Let me introduce you to the partition automatic management of data warehouse
BGP related knowledge points
Can Baidu network disk yundetectservice.exe be disabled and closed
@Mapkey usage instructions
Leetcode 0123. the best time to buy and sell stocks III: dynamic programming + simulation in constant space
必会面试题:1.浅拷贝和深拷贝_深拷贝
Paper notes: accurate causal influence on discrete data
JS ------ Chapter II JS logic control
Where are MySQL version numbers 6 and 7?
C language program environment and preprocessing
Kubernetes application design guide
Architecture design of multi live shopping mall
云图
Use SQLite provided by the system
WP wechat export chat record backup to computer