3781: 小B的询问

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 426  Solved: 284
[Submit][Status][Discuss]

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

Source

 这道题同 小Z的袜子 。。。
只需要把 加和减 改为 加减 个数的平方 即可。。。
 
代码:
 #include<bits/stdc++.h>
using namespace std;
#define LL long long
#define MAXN 50010
struct node
{
int l,r,id;
}q[MAXN];
LL ans[MAXN];
int C[MAXN],pos[MAXN],tot[MAXN];
int read()
{
int s=,fh=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')fh=-;ch=getchar();}
while(ch>=''&&ch<=''){s=s*+(ch-'');ch=getchar();}
return s*fh;
}
bool cmp(node a,node b)
{
if(pos[a.l]==pos[b.l])return a.r<b.r;
return a.l<b.l;
}
int main()
{
int n,m,k,block,i,L,R;
LL res;
n=read();m=read();k=read();
for(i=;i<=n;i++)C[i]=read();
block=(int)sqrt(n);
for(i=;i<=n;i++)pos[i]=(i-)/block+;
for(i=;i<=m;i++)
{
q[i].l=read();q[i].r=read();
q[i].id=i;
}
sort(q+,q+m+,cmp);
L=;R=;res=;
memset(tot,,sizeof(tot));
for(i=;i<=m;i++)
{
while(L<q[i].l)
{
res-=tot[C[L]]*tot[C[L]];
tot[C[L]]--;
res+=tot[C[L]]*tot[C[L]];
L++;
}
while(L>q[i].l)
{
L--;
res-=tot[C[L]]*tot[C[L]];
tot[C[L]]++;
res+=tot[C[L]]*tot[C[L]];
}
while(R<q[i].r)
{
R++;
res-=tot[C[R]]*tot[C[R]];
tot[C[R]]++;
res+=tot[C[R]]*tot[C[R]];
}
while(R>q[i].r)
{
res-=tot[C[R]]*tot[C[R]];
tot[C[R]]--;
res+=tot[C[R]]*tot[C[R]];
R--;
}
ans[q[i].id]=res;
}
for(i=;i<=m;i++)printf("%lld\n",ans[i]);
fclose(stdin);
fclose(stdout);
return ;
}

最新文章

  1. gulp + webpack + sass 学习
  2. iOS-多线程基础
  3. java 22 - 4 多线程的代码实现的方式1
  4. Java集合系列:-----------01集合的整体框架
  5. ubuntu --- shortcut key
  6. shell 脚本编程概述
  7. cellery ImportError &amp; AttributeError
  8. HDU 1180 (BFS搜索)
  9. [转]编译Android源代码常见错误解决办法
  10. HDU 5422 Rikka with Graph
  11. 手游:cocos2d-x3.0 移植 wp8 开发 各种 “蛋疼”问题的汇总
  12. Top命令查看内存
  13. CodeForces 689A -Mike and Cellphone
  14. Adobe Flash Builder 4.7 新功能详解
  15. CPU-Z五大主要功能及使用方法初步了解
  16. linux中一些常用的目录简要说明
  17. git安装以及初始化
  18. recv() failed (104: Connection reset by peer) while reading response header from upstream
  19. 主题模型之潜在语义分析(Latent Semantic Analysis)
  20. sencha touch 在线实战培训 第一期 第五节

热门文章

  1. AngularJs的UI组件ui-Bootstrap-Tooltip
  2. sql Server 触发器 调用java.
  3. POJ 2039 To and Fro(模拟)
  4. [CSS]文本属性(Text)
  5. RHEL 7特性说明(六):集群
  6. linux建立信任关系
  7. SendMail
  8. winfrom获得鼠标的坐标
  9. Uva_11427 Expect the Expected
  10. 滴滴过节送10元打车券是不是bug