当前位置:网站首页>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;
}
边栏推荐
- Branch and loop statements in C language learning
- Sql文件导入数据库-保姆级教程
- UART
- 必会面试题:1.浅拷贝和深拷贝_浅拷贝
- Leetcode 0125. validate palindrome string
- Notes of Teacher Li Hongyi's 2020 in-depth learning series 3
- Where are MySQL version numbers 6 and 7?
- Processing of ffmpeg wasapi can't activate audio endpoint error
- Restructuredtext grammar summary for beginners
- The laneatt code is reproduced and tested with the video collected by yourself
猜你喜欢

1、 MFC introduction

Can Baidu network disk yundetectservice.exe be disabled and closed

How painful is it to write unit tests? Can you do it

With screen and nohup running, there is no need to worry about deep learning code anymore | exiting the terminal will not affect the operation of server program code

From the big guy baptism! 2022 headline first hand play MySQL advanced notes, and it is expected to penetrate P7

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

Digital stopwatch based on Verilog HDL

Only by learning these JMeter plug-ins can we design complex performance test scenarios

Processing PDF and JPG files in VB6

Weekly summary (*66): next five years
随机推荐
How to speculate on the Internet? Is it safe to speculate on mobile phones
SQL file import database - Nanny level tutorial
【无标题】
Use SQLite provided by the system
3. Pressure test
线段树杂谈
Mandatory interview questions: 1. shallow copy and deep copy_ Shallow copy
数组中只出现一次的两个数字
每周小结(*66):下一个五年
Horizontally centered element
See project code Note 1
c语言:深度刨析函数栈帧
With screen and nohup running, there is no need to worry about deep learning code anymore | exiting the terminal will not affect the operation of server program code
[nuxt 3] (x) runtime configuration
必会面试题:1.浅拷贝和深拷贝_浅拷贝
LeetCode_ 392_ Judgement subsequence
codeforces round #805 ABCDEFG
Optaplanner will abandon DRL (drools) scoring method!!!
Excel file processing tool class (based on easyexcel)
Processing PDF and JPG files in VB6