题目描述
/*
显然询问的相邻两个区间左右移动的次数越少越好
那么可以考虑把询问的每个区间抽象成坐标系里的点
让点与点之间的曼哈顿距离最短,但是直接排序还是会超时,所以再加一个分块
*/
#include<complex>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=5e4+7;
struct node{
int id,l,r;
}q[N];
int n,m,k;
int a[N],num[N],ans[N],pos[N];
int qread()
{
int x=0;
char ch=getchar();
while(ch<'0' || ch>'9')ch=getchar();
while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x;
}
bool cmp(const node &a,const node &b)
{
if(pos[a.l]==pos[b.l])return a.r<b.r;
return pos[a.l]<pos[b.l];
}
void change(int &tot,int id,int add)
{
tot+=2*add*num[a[id]]+1;
num[a[id]]+=add;
}
int main()
{
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=n;i++)
a[i]=qread();
int tmp=sqrt(n);
for(int i=1;i<=m;i++)
{
q[i].l=qread();q[i].r=qread();
q[i].id=i;
}
for(int i=1;i<=n;i++)
pos[i]=(i-1)/tmp+1;
sort(q+1,q+m+1,cmp);
int l=1,r=0,tot=0;
for(int i=1;i<=m;i++)
{
while(l<q[i].l)
change(tot,l++,-1);
while(l>q[i].l)
change(tot,--l,1);
while(r<q[i].r)
change(tot,++r,1);
while(r>q[i].r)
change(tot,r--,-1);
ans[q[i].id]=tot;
}
for(int i=1;i<=m;i++)
printf("%d
",ans[i]);
for(int i=1;i<=n;i++)
printf("%d ",pos[i]);
return 0;
}