莫队算法 小Z的袜子
2016-02-19 13:17
316 查看
#include<iostream> #include<cstdio> #include<cstdlib> #include<vector> #include<cmath> #include<string> #include<algorithm> #include<set> #include<map> #include<cstring> #include<queue> #include<stack> #include<list> using namespace std; typedef long long ll; const int MAXN=50010; struct Query{ int L,R,id; }node[MAXN]; ll gcd(ll a,ll b){ if(b==0)return a; return gcd(b,a%b); } struct Ans{ ll a,b; void reduce(){ ll d=gcd(a,b); a/=d;b/=d; } }ans[MAXN]; int a[MAXN]; int num[MAXN]; int n,m,unite; bool cmp( Query a,Query b ){ if(a.L/unite==b.L/unite)return a.R<b.R; return a.L/unite<b.L/unite; } ll pow2(ll x){ return x*x; } void work(){ ll tmp=0; memset(num,0,sizeof(num)); int L=1; int R=0; for(int i=0;i<m;i++){ while( R<node[i].R ){ R++; tmp-= pow2( num[a[R]] ); num[ a[R] ]++; tmp+= pow2( num[a[R]] ); } while( R>node[i].R ){ tmp-= pow2( num[a[R]] ); num[ a[R] ]--; tmp+= pow2( num[a[R]] ); R--; } while( L<node[i].L ){ tmp-= pow2( num[a[L]] ); num[ a[L] ]--; tmp+= pow2( num[a[L]] ); L++; } while( L>node[i].L ){ L--; tmp-= pow2( num[a[L]] ); num[ a[L] ]++; tmp+= pow2( num[a[L]] ); } ans[ node[i].id ].a=tmp-(R-L+1); ans[ node[i].id ].b=(ll) (R-L+1)*(R-L); ans[ node[i].id ].reduce(); } } int main() { while(scanf("%d%d",&n,&m)!=EOF){ for(int i=1;i<=n;i++){ scanf("%d",&a[i]); } for(int i=0;i<m;i++){ node[i].id=i; scanf("%d%d",&node[i].L,&node[i].R); } unite=(int ) sqrt(n); sort( node,node+m,cmp ); work(); for(int i=0;i<m;i++){ printf("%lld/%lld\n",ans[i].a,ans[i].b); } } return 0; }
相关文章推荐
- android -- 蓝牙 bluetooth (二) 打开蓝牙
- jdk下载官方地址
- Linux流量监控工具 - iftop (最全面的iftop教程)
- PLSQL数组学习(二)
- android开发之popupwindow半透明
- VMware 12安装虚拟机Mac OS X 10.10(VMware12安装/共享文件夹)
- 浏览器历史和发展趋势浅析
- HDU 5479 Scaena Felix(简单的数据结构题目)
- windows 安装python的scipy库,报错error: no lapack/blas resources found
- android自适应屏幕方向和大小
- iOS 此证书签发者无效 解决办法
- ERP SAP 培训----经管、信管、计算机同学请注意!史上最寒就业季,入职国企拿高薪!
- Duplicate files copied in APK META-INF/LICENSE.txt
- 开源流媒体服务器EasyDarwin支持epoll网络模型,大大提升流媒体服务器网络并发性能
- MongoDB索引使用
- 爱因斯坦论宗教与自然科学的关系
- [leetcode] 334. Increasing Triplet Subsequence 解题报告
- Eclipse自定义快捷键导入,快捷键冲突
- linux设备模型-kobject
- android FM启动流程