题目:
题解:
比某Z的袜子还要裸,LL换成int之后达到速度巅峰
代码:
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
struct hh
{
int id,l,r;
}nod[50005];
int num[50005],pos[50005],m,n,a[50005];
int ans[50005];
bool operator <(const hh &x,const hh &y)
{
if (pos[x.l]!=pos[y.l]) return pos[x.l]<pos[y.l];
return x.r<y.r;
}
int cmp(hh a,hh b){return a.id<b.id;}
void mobi(int &ans,int i,int add)
{
ans=ans+2*add*num[a[i]]+1;
num[a[i]]+=add;
}
void work()
{
int i,l=1,r=0;int Ans=0;
for (i=1;i<=m;i++)
{
while (l<nod[i].l){mobi(Ans,l,-1);l++;}
while (l>nod[i].l){l--;mobi(Ans,l,1);}
while (r<nod[i].r){r++;mobi(Ans,r,1);}
while (r>nod[i].r){mobi(Ans,r,-1); r--;}
ans[nod[i].id]=Ans;
}
for (i=1;i<=m;i++)
printf("%d\n",ans[i]);
}
int main()
{
int k,i;
scanf("%d%d%d",&n,&m,&k);
for (i=1;i<=n;i++)
scanf("%d",&a[i]);
int unit=(int)sqrt(n);
for (i=1;i<=n;i++)
pos[i]=(i-1)/unit+1;
for (i=1;i<=m;i++)
{
scanf("%d%d",&nod[i].l,&nod[i].r);
nod[i].id=i;
}
sort(nod+1,nod+1+m);
work();
}