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。
1<=N、M、K<=50000

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 //在区间[1..4]]之间,1出现了2次,2和3分别出现1次,于是结果=4+1+1=6
9
5
2

HINT

1莫队算法

2因为是求出Sigma(c(i)^2)的值,所以在模板上要改进一下

3可知当c(i)++,总和加上c(i+1)^-c(i)^=c(i)*2+1,就有了代码中的ans+=num[x]*2+1;(如果在最后再枚举一遍会超时,所以要在线统计,c(i)--的情况读者可以先自行推导再看代码)

#include<bits/stdc++.h>
using namespace std;
long long n,m,cnt[200000],ans,k,num[1000001],l,r,anss[200001];
struct data {
long long ll,rr,num;
}a[200001];
bool cmp(data c,data d) {
long long bol=sqrt(n);
if(c.ll/bol==d.ll/bol)
return c.rr<d.rr;
else
return c.ll/bol<d.ll/bol;
}//莫队排序
void add(long long xx){
long long x=cnt[xx];
ans+=num[x]*2+1;
num[x]++; }
void del(long long xx){
long long x=cnt[xx];
if(num[x]-1>=0)
ans=ans-num[x]*2+1,
num[x]--;
}
void dowork(long long x){
while(l>a[x].ll) add(--l);
while(r<a[x].rr) add(++r);
while(l<a[x].ll) del(l++);
while(r>a[x].rr) del(r--);
anss[a[x].num]=ans;
}//莫队板子
int main(){
cin>>n>>m>>k;
for(long long i=1;i<=n;i++){
scanf("%lld",&cnt[i]);
}
for(long long i=1;i<=m;i++){
scanf("%lld%lld",&a[i].ll,&a[i].rr);
a[i].num=i;
}
sort(a+1,a+m+1,cmp);
for(long long i=1;i<=m;i++){
dowork(i);
}
for(long long i=1;i<=m;i++)
printf("%lld\n",anss[i]);
}

--

最新文章

  1. can&#39;t connect to mysql server on &#39;localhost&#39;(10061)
  2. Ubuntu杂记——Ubuntu下用虚拟机共享上网
  3. Ubuntu 安装Samba服务器
  4. centos克隆,网卡启动失败
  5. Bugtags 2016-06-16 更新内容
  6. Android studio 启动时出现Android studio was unable to create a local connection in order
  7. CSS padding margin border属性
  8. mybatis+spring+struts2框架整合
  9. 如何在VS2010中使用Async功能?
  10. JVM笔记6:JVM类加载机制
  11. Script: Who’s using a database link?(找出谁在使用dblink)
  12. PHP设计模式之:工厂模式
  13. Deprecated: Call-time pass-by-reference has been deprecated in E:\wamp\www\admin\htdocs\busi.php on line 381
  14. Date的使用
  15. 【Linux】CentOS 学习笔记之二(命令)
  16. 使用python发送QQ邮件
  17. TCP常见的定时器三次握手与四次挥手
  18. Java正则过滤
  19. 好用的 over the wall教程
  20. react的dva框架初试

热门文章

  1. python初次接触
  2. PyQt(Python+Qt)学习随笔:QTableView的sortingEnabled属性
  3. Java进阶学习之集合与泛型(1)
  4. LeetCode初级算法之数组:36 有效数独
  5. C# 9.0新特性详解系列之五:记录(record)和with表达式
  6. NOI 2020 D1T3 本人题解
  7. AcWing 294. 计算重复
  8. js生成随机数、随机数列、数值转金融格式
  9. mysql中FILE权限
  10. [日常摸鱼]bzoj2823 [AHOI2012]信号塔