您的位置:首页 > 其它

BZOJ3781小B的询问

2016-03-09 20:29 211 查看
3781: 小B的询问

Time Limit: 10 Sec Memory Limit: 128 MB

Submit: 417 Solved: 278

Description

小B有一个序列,包含N个1~K之间的整数。他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L..R]中的重复次数。小B请你帮助他回答询问。

Input

第一行,三个整数N、M、K。

第二行,N个整数,表示小B的序列。

接下来的M行,每行两个整数L、R。

Output

M行,每行一个整数,其中第i行的整数表示第i个询问的答案。

Sample Input

6 4 3

1 3 2 1 1 3

1 4

2 6

3 5

5 6

Sample Output

6

9

5

2

HINT

对于全部的数据,1<=N、M、K<=50000

普通莫队算法。。

题不能再裸了。。人不能再弱了。。

附上本蒟蒻的代码:

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int n,m,k,b[50001];
long long cnt[50001],res[50001];
struct kx
{
int l,r,block,id;
}a[50001];
bool operator < (kx a,kx b)
{
if (a.block==b.block)
return a.r<b.r;
return a.block<b.block;
}

int read()
{
int w=0,c=1;
char ch=getchar();
while (ch<'0' || ch>'9')
{
if (ch=='-')
c=-1;
ch=getchar();
}
while (ch>='0' && ch<='9')
{
w=w*10+ch-'0';
ch=getchar();
}
return w*c;
}

int main()
{
int size,l=1,r=0,i;
long long ans=0;
n=read(),m=read(),k=read();
size=sqrt(n);
for (i=1;i<=n;i++)
b[i]=read();
for (i=1;i<=m;i++)
{
a[i].l=read(),a[i].r=read();
a[i].id=i,a[i].block=(a[i].l-1)/size+1;
}
sort(a+1,a+m+1);
for (i=1;i<=m;i++)
{
while (l>a[i].l)
l--,cnt[b[l]]++,ans+=2*cnt[b[l]]-1;
while (r<a[i].r)
r++,cnt[b[r]]++,ans+=2*cnt[b[r]]-1;
while (l<a[i].l)
cnt[b[l]]--,ans-=2*cnt[b[l]]+1,l++;
while (r>a[i].r)
cnt[b[r]]--,ans-=2*cnt[b[r]]+1,r--;
res[a[i].id]=ans;
}
for (i=1;i<=m;i++)
printf("%lld\n",res[i]);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: