原题Link:https://www.luogu.org/problemnew/show/P2709
好吧这是一道莫队板子题
但由于本蒟蒻太蒻了so敲了1h。。。
放模板。。。
1 #include<bits/stdc++.h> 2 #define SB long long 3 using namespace std; 4 SB n,m,k,col[50010],ans,fk,FK[50010],L=1,R,s[50010]; 5 struct h{ 6 int l; 7 int r; 8 int place; 9 int data; 10 }Q[50010]; 11 12 void doing(int T) 13 { 14 for(;R<Q[T].r;R++) 15 { 16 s[col[R+1]]++; 17 ans+=(2*s[col[R+1]])-1; 18 } 19 for(;R>Q[T].r;R--) 20 { 21 s[col[R]]--; 22 ans-=(2*s[col[R]])+1; 23 } 24 for(;L<Q[T].l;L++) 25 { 26 s[col[L]]--; 27 ans-=(2*s[col[L]])+1; 28 } 29 for(;L>Q[T].l;L--) 30 { 31 s[col[L-1]]++; 32 ans+=(2*s[col[L-1]])-1; 33 } 34 Q[T].data=ans; 35 return; 36 } 37 38 int cmp(h x,h y) 39 { 40 if(FK[x.l]!=FK[y.l]) 41 { 42 return x.l<y.l; 43 } 44 return x.r<y.r; 45 } 46 47 int Cmp(h x,h y) 48 { 49 return x.place<y.place; 50 } 51 52 int main() 53 { 54 cin>>n>>m>>k; 55 for(int i=1;i<=n;i++) 56 { 57 cin>>col[i]; 58 } 59 fk=sqrt(n); 60 for(int i=1;i<=m;i++) 61 { 62 cin>>Q[i].l>>Q[i].r; 63 Q[i].place=i; 64 } 65 for(int i=1;i<=n;i++) 66 { 67 FK[i]=(i-1)/fk+1; 68 } 69 sort(Q+1,Q+m+1,cmp); 70 for(int i=1;i<=m;i++) 71 { 72 doing(i); 73 } 74 sort(Q+1,Q+m+1,Cmp); 75 for(int i=1;i<=m;i++) 76 { 77 cout<<Q[i].data<<endl; 78 } 79 return 0; 80 }