水博客的
#include<bits/stdc++.h>
#define ll long long
const int N=50005;
using namespace std;
template<class T>
inline void read(T &x)
{
x=0; int f=1;
static char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
x*=f;
}
int n,m,k,size,num,v[N],cnt[N],ans=0,op[N];
struct Query
{
int l,r,id,pos;
bool operator <(const Query &x) const
{
if(this->pos==x.pos) return this->r<x.r;
return this->pos<x.pos;
}
}q[N];
inline void Remove(int p)
{
ans-=cnt[v[p]]*cnt[v[p]];
cnt[v[p]]--;
ans+=cnt[v[p]]*cnt[v[p]];
}
inline void Add(int p)
{
ans-=cnt[v[p]]*cnt[v[p]];
cnt[v[p]]++;
ans+=cnt[v[p]]*cnt[v[p]];
}
int main()
{
ios::sync_with_stdio(false);
cout.tie(NULL);
read(n),read(m),read(k);
size=sqrt(n);
for(int i=1;i<=n;i++) read(v[i]);
for(int i=1;i<=m;i++)
{
read(q[i].l),read(q[i].r);
q[i].id=i,q[i].pos=(q[i].l-1)/size+1;
}
sort(q+1,q+m+1);
int l=1,r=0;
for(int i=1;i<=m;i++)
{
while(l<q[i].l) Remove(l++);
while(l>q[i].l) Add(--l);
while(r<q[i].r) Add(++r);
while(r>q[i].r) Remove(r--);
op[q[i].id]=ans;
}
for(int i=1;i<=m;i++) cout<<op[i]<<'\n';
return 0;
}