小B的询问(小Z的袜子简单版,莫队)
小B的询问
刚写完小Z的袜子,然后删了些代码,反手把这题A了。。。
题意、思路:见小Z的袜子
#include "bits/stdc++.h"
#define hhh printf("hhh\n")
#define see(x) (cerr<<(#x)<<'='<<(x)<<endl)
using namespace std;
typedef long long ll;
typedef pair<int,int> pr;
inline int read() {int x=0;char c=getchar();while(c<'0'||c>'9')c=getchar();while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();return x;}
const int maxn = 5e4+10;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;
const double eps = 1e-7;
int n, m, k, len;
int c[maxn], cnt[maxn];
ll ans1[maxn], ans;
struct Q{
int l, r, id;
friend bool operator < (const Q &a, const Q &b) {
if((a.l-1)/len==(b.l-1)/len) {
if((a.l-1)/len%2) return a.r>b.r;
else return a.r<b.r;
}
return a.l<b.l;
}
}q[maxn];
inline void update(int p, int f) {
ans-=1ll*cnt[c[p]]*cnt[c[p]];
cnt[c[p]]+=f;
ans+=1ll*cnt[c[p]]*cnt[c[p]];
}
int main() {
//ios::sync_with_stdio(false); cin.tie(0);
n=read(), m=read(), k=read(); len=sqrt(n);
for(int i=1; i<=n; ++i) c[i]=read();
for(int i=1; i<=m; ++i) q[i]=(Q){read(),read(),i};
sort(q+1,q+1+m);
int l=1, r=0;
int cnt=0;
for(int i=1; i<=m; ++i) {
while(l<q[i].l) update(l++,-1);
while(l>q[i].l) update(--l,1);
while(r<q[i].r) update(++r,1);
while(r>q[i].r) update(r--,-1);
ans1[q[i].id]=ans;
}
for(int i=1; i<=m; ++i) printf("%lld\n", ans1[i]);
}