【luogu 2709 / BZOJ 3781】小B的询问
【原题题面】传送门
【题解大意】
莫队。这题未免太果。
【code】
#include<bits/stdc++.h> using namespace std; #define File "" #define ll long long #define ull unsigned long long inline void file(){ freopen(File".in","r",stdin); freopen(File".out","w",stdout); } inline int read(){ int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();} while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0'; ch=getchar();} return x*f; } const int mxn = 5e4+5; int n,m,k; int Block; int a[mxn],ans[mxn],cnt[mxn]; struct Q{ int l,r,id; }q[mxn]; inline bool cmp(Q x,Q y){ if((x.l/Block) ^ (y.l/Block)) return x.l < y.l; else if((x.l/Block)&1) return x.r < y.r; else return x.r > y.r; } ll answer(0); inline void mov(int x,int d){ answer -= cnt[a[x]]*cnt[a[x]]; cnt[a[x]] += d; answer += cnt[a[x]]*cnt[a[x]]; } int main(){ // file(); n = read(),m = read(),k = read(); Block = sqrt(n); for(int i = 1;i <= n; ++i) a[i] = read(); for(int i = 1;i <= m; ++i) q[i].l = read(),q[i].r = read(),q[i].id = i; sort(q+1,q+1+m,cmp); int l(1),r(0); for(int i = 1;i <= m; ++i){ int ql = q[i].l,qr = q[i].r; while(ql < l) mov(l-1,1),l--; while(ql > l) mov(l,-1),l++; while(qr < r) mov(r,-1),r--; while(qr > r) mov(r+1,1),r++; ans[q[i].id] = answer; } for(int i = 1;i <= m; ++i) printf("%d\n",ans[i]); return 0; } /* 6 4 3 1 3 2 1 1 3 1 4 2 6 3 5 5 6 */
G102的孤儿们都要好好的啊。