HYSBZ 2038 小Z的袜子 莫队算法
2015-07-31 19:51
309 查看
#include <cstdio> #include <iostream> #include <set> #include <vector> #include <cstring> #include <string> #include <algorithm> #include <cmath> #include <map> #include <queue> #include <iomanip> using namespace std; const int size=111111; typedef long long ll; typedef double dd; typedef pair<int,int> P; int n,m,a[size]; int Q[size],cnt; ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} struct node{ int l,r,id; }b[size]; int cmp(node a,node b){ return Q[a.l]<Q[b.l]||(Q[a.l]==Q[b.l]&&a.r<b.r); } ll ans,num[size],ret[size],len[size]; ll add(int a,int k){ ll ret=num[a]*num[a]; num[a]+=k; return num[a]*num[a]-ret; } void work(){ int l=1,r=num[a[1]]=ans=1; for(int i=0;i<m;i++){ while(b[i].r>r)r++,ans+=add(a[r],1); while(b[i].r<r)ans+=add(a[r],-1),r--; while(b[i].l>l)ans+=add(a[l],-1),l++; while(b[i].l<l)l--,ans+=add(a[l],1); ret[b[i].id]=ans; } } int main(){ while(cin>>n>>m){ cnt=sqrt(n); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); num[a[i]]=0; Q[i]=(i-1)/cnt+1; } for(int i=0;i<m;i++){ scanf("%d%d",&b[i].l,&b[i].r); b[i].id=i; len[i]=b[i].r-b[i].l+1; } sort(b,b+m,cmp); work(); for(int i=0;i<m;i++){ ll _g=gcd(ret[i]-len[i],len[i]*(len[i]-1)); printf("%lld/%lld\n",(ret[i]-len[i])/_g,len[i]*(len[i]-1)/_g); } } }
相关文章推荐
- HDU 4336 Card Collector(概率DP)
- opencv sobel导数
- 查看ubuntu是32位还是64位
- (一)mybatis+mysql 基本环境搭建
- 选择排序
- opencv图像边界的填充
- 文本切换
- hdu 5319 2015多校对抗赛三
- JAVA EE 自学路线
- 黑马程序员--C语言——基础语法
- (easy)LeetCode 235.Lowest Common Ancestor of a Binary Search Tree
- UIScrollView的属性总结
- 浅析notifyDataSetChanged内部工作流程
- 秒杀多线程第五篇 经典线程同步 关键段CS
- Android学习笔记之事件分发机制(二)
- JUnit4---eclipse实现java代码单元测试
- 使用信鸽推送时打开指定页面的同时获取相关参数
- HDU 4297 One and One Story 分类讨论, LCA 2012年成都网络赛J题
- HTML 标签列表(4)
- Sql 新建表并插入1000随机数