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
普通莫队算法。。
题不能再裸了。。人不能再弱了。。
附上本蒟蒻的代码:
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; }
相关文章推荐
- Hadoop2.6完全分布式多节点集群安装配置
- kafka学习教程
- 【计算机视觉】TPAMI的Editors
- C++愤恨者札记1——类对象作为函数参数的数据传递过程 http://blog.csdn.net/tms_li/article/details/7765626
- Java中的序列化Serialable高级详解
- git总结 -- 远程仓库篇
- 古代赌局
- 20160309 SOHU实习面试
- POJ-3187(Backward Digit Sums)(全排列+dfs())
- 自动复制文本到剪贴版(Auto Copy to clipboard)
- 一个iOS开发者的修真之路
- unity C#预处理器指令
- 51nod1289 大鱼吃小鱼
- 给出一个有效的算法来确定在整数A1<A2<A3<...<AN的数组中是否存在整数i使得Ai=i
- 单链表的创建(头插法尾插法),插入,删除
- 使用Eclipse签名打包,项目出现错误怎么办?
- 深入浅出MySQL事务处理和锁机制
- python学习笔记:python对象
- 把书《CUDA By Example an Introduction to General Purpose GPU Programming》读薄
- 匈牙利命名法收藏