P2709 小B的询问(莫队)
莫队模板
资磁离线询问
维护两个跳来跳去的指针
先分块,蓝后询问按块排序。
蓝后每次指针左右横跳更新答案
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; #define N 50005 struct data{int x,y,t;}a[N]; int n,m,k,Len,b[N],c[N],ans[N],tot,L,R; inline int bel(int x){return (x-1)/Len+1;} inline bool cmp(data A,data B){ return bel(A.x)==bel(B.x)?bel(A.y)<bel(B.y):bel(A.x)<bel(B.x); } inline void Del(int x){tot-=c[b[x]]*2-1,--c[b[x]];} inline void Add(int x){tot+=c[b[x]]*2+1,++c[b[x]];} int main(){ scanf("%d%d%d",&n,&m,&k);Len=sqrt(n); register int i; for(i=1;i<=n;++i) scanf("%d",&b[i]); for(i=1;i<=m;++i) scanf("%d%d",&a[i].x,&a[i].y),a[i].t=i; sort(a+1,a+m+1,cmp); L=R=1; c[b[1]]=tot=1; for(i=1;i<=m;++i){ while(L<a[i].x) Del(L++); while(L>a[i].x) Add(--L); while(R>a[i].y) Del(R--); while(R<a[i].y) Add(++R); ans[a[i].t]=tot; } for(i=1;i<=m;++i) printf("%d\n",ans[i]); return 0; }