标签:tar contain code hdu print 复杂度 including ref several
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6534
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 160 Accepted Submission(s): 52
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; typedef long long LL; const LL mod=1e9+7; const LL INF=1e9+7; const int maxn=27000+50; LL N,M,K; LL a[maxn],b[maxn]; LL c[maxn]; LL ans[maxn]; LL up[maxn],low[maxn]; LL block; LL len; struct query { LL l,r,num; }q[maxn]; bool cmp(const query x,const query y) { if(x.l/block==y.l/block) return x.r<y.r; return x.l<y.l; } int lowbit(int x) { return x&(-x); } int sea(LL x)//寻找大小为x的数在b数组中的下标 { int l=1,r=len; while(l<=r) { int mid=(l+r)>>1; if(b[mid]==x) return mid; else if(b[mid]>x) { r=mid-1; } else l=mid+1; } } void update(LL x,int add) { while(x<=27000) { c[x]+=add; x+=lowbit(x); } } LL Query(LL x) { LL ret=0; while(x>0) { ret+=c[x]; x-=lowbit(x); } return ret; } int main() { scanf("%lld%lld%lld",&N,&M,&K); block=sqrt(N); for(int i=1;i<=N;i++) { scanf("%lld",&a[i]); b[i]=a[i]; } // sort(a+1,a+1+N,cmp); sort(b+1,b+N+1); len=unique(b+1,b+N+1)-(b+1);//去重 for(int i=1;i<=M;i++) { scanf("%lld%lld",&q[i].l,&q[i].r); q[i].num=i; } sort(q+1,q+M+1,cmp); for(int i=1;i<=N;i++) { LL x=lower_bound(b+1,b+1+len,a[i]-K)-b; LL y=upper_bound(b+1,b+1+len,a[i]+K)-b-1; up[i]=y; low[i]=x-1; } LL L=1,R=0; LL ret=0; for(int i=1;i<=M;i++) { while(R<q[i].r)//区间增大 { // LL x=lower_bound(b+1,b+1+len,a[R+1]-K)-b; // LL y=upper_bound(b+1,b+1+len,a[R+1]+K)-b-1; //现在在b数组[x,y]之间的所有的数都是符合条件的 ret+=Query(up[R+1])-Query(low[R+1]); update(sea(a[R+1]),1);//这个必须放在上面的下面 因为如果先放的话本身的值岂不是算进去了 R++; } while(R>q[i].r)//区间减小 { update(sea(a[R]),-1);//这个必须放在下面的上面 因为你要减去它的影响 自然也不能算本身-本身 // LL x=lower_bound(b+1,b+1+len,a[R]-K)-b; // LL y=upper_bound(b+1,b+1+len,a[R]+K)-b-1; ret-=Query(up[R])-Query(low[R]); R--; } while(L<q[i].l)//区间减小 { update(sea(a[L]),-1); // solve(L,-1); // LL x=lower_bound(b+1,b+1+len,a[L]-K)-b; // LL y=upper_bound(b+1,b+1+len,a[L]+K)-b-1; ret-=Query(up[L])-Query(low[L]); L++; } while(L>q[i].l)//区间增大 { // LL x=lower_bound(b+1,b+1+len,a[L-1]-K)-b; // LL y=upper_bound(b+1,b+1+len,a[L-1]+K)-b-1; ret+=Query(up[L-1])-Query(low[L-1]); update(sea(a[L-1]),1); L--; } ans[q[i].num]=ret; } for(int i=1;i<=M;i++) printf("%lld\n",ans[i]); //cout<<ans[i]<<endl; return 0; }
Chika and Friendly Pairs(莫队+树状数组+离散化+预处理上下界)
标签:tar contain code hdu print 复杂度 including ref several
原文地址:https://www.cnblogs.com/caijiaming/p/10896352.html