This problem can prefix and maintenance, mainly talk about prefixes and
- and the prefix
provided $ pre_i $ represents $ \ sum ^ {i} _ {1} a_ {i} $
there is
$ pre_i pre_ = {I}. 1-a_i $ +
- and query prefix
With pre $ $ array if we claim $ [l, r] $ and has
$ pre_. 1} = {L-A_2 A_1 + + + ... + A_3 A_. 1-L {} $
$ R & lt pre_} = { a_1 + a_2 ... a_ {l- 1} + a_ {l} + ... a_r $
be a difference, found twenty-two offset, we get
$$ \ sum ^ r_ {i = l} a_i = pre_r- {L}. 1-pre_ $$
(pen can not understand the math)
- Code
code can achieve such
#include<iostream> #include<cstdio> using namespace std; inline int read(){ register int x=0,v=1,ch=getchar(); while(!isdigit(ch)){if(ch=='-')v=-1;ch=getchar();} while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^'0');ch=getchar();} return x*v; } const int MAX=100005; int n,k,L,R,l,r; int x,ans,res[MAX],pre[MAX]; int main(){ n=read(),k=read(); for(register int i=1;i<=n;++i){ x=read();res[x]+=read(); L=min(L,x),R=max(R,x);//查询最小 位置和最大位置 } for(register int i=L;i<=R;++i){//前缀和 pre[i]=pre[i-1]+res[i]; } for(register int i=L;i+k-1<=R;++i){ l=i,r=i+k-1; ans = max (ans, pre [ r] -pre [l-1]); // determines whether the maximum and } the printf ( "% D \ n-", ANS); // output answer return 0; }