不是很难的莫队,打来熟悉一下,很久没打了。
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<iostream>
#include<queue>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
int n,m,k;
typedef long long ll;
const int N=1e5+5;
int a[N],pos[N],t[N];
struct node
{
int id,x,y;
ll ans;
}q[N];
ll ans;
inline bool cmp(node a,node b)
{
return pos[a.x]<pos[b.x]||pos[a.x]==pos[b.x]&&a.y<b.y;
}
inline bool cmp1(node a,node b)
{
return a.id<b.id;
}
inline void updata(int x,int y)
{
ans-=1ll*t[a[x]]*t[a[x]];
t[a[x]]+=y;
ans+=1ll*t[a[x]]*t[a[x]];
}
inline void query()
{
updata(1,1);
int l=1,r=1;
fo(i,1,m)
{
for(;r<q[i].y;r++)updata(r+1,1);
for(;l>q[i].x;l--)updata(l-1,1);
for(;r>q[i].y;r--)updata(r,-1);
for(;l<q[i].x;l++)updata(l,-1);
q[i].ans=ans;
}
}
int main()
{
scanf("%d%d%d",&n,&m,&k);
int block=int(sqrt(n));
fo(i,1,n)
{
scanf("%d",&a[i]);
pos[i]=(i+block-1)/block;
}
fo(i,1,m)
{
int x,y;
scanf("%d%d",&x,&y);
q[i].x=x;
q[i].y=y;
q[i].id=i;
}
sort(q+1,q+m+1,cmp);
query();
sort(q+1,q+m+1,cmp1);
fo(i,1,m)printf("%lld\n",q[i].ans);
return 0;
}