小z的袜子
来源:互联网 发布:淘宝卖二手的叫什么 编辑:程序博客网 时间:2024/04/27 07:42
在学了莫队算法之后,又去搞了此题,主要是找转移式子上,莫队算法还是很好理解的。
#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<cmath>#define LL long longusing namespace std;LL gcd(LL a,LL b){ if(b==0)return a; return gcd(b,a%b);}const int maxn = 50050;int n,m,sqrtn,c[maxn],num[maxn];struct node{ int l,r,i; LL a,b;} u[maxn];int cmp(node a,node b){ if(a.r/sqrtn==b.r/sqrtn) return a.l<b.l; return a.r/sqrtn<b.r/sqrtn;}int comp(node a,node b){ return a.i<b.i;}int main(){ while(~scanf("%d %d",&n,&m)) { memset(num,0,sizeof(num)); for(int i=1; i<=n; i++)scanf("%d",&c[i]); for(int i=0; i<m; i++)scanf("%d %d",&u[i].l,&u[i].r),u[i].i=i; sqrtn=sqrt(n); sort(u,u+m,cmp); int x=1,y=0; LL temp=0; for(int i=0; i<m; i++) { while(y<u[i].r) { y++; temp-=(LL)num[c[y]]*(LL)num[c[y]]; num[c[y]]++; temp+=(LL)num[c[y]]*(LL)num[c[y]]; } while(x<u[i].l) { temp-=(LL)num[c[x]]*(LL)num[c[x]]; num[c[x]]--; temp+=(LL)num[c[x]]*(LL)num[c[x]]; x++; } while(y>u[i].r) { temp-=(LL)num[c[y]]*(LL)num[c[y]]; num[c[y]]--; temp+=(LL)num[c[y]]*(LL)num[c[y]]; y--; } while(x>u[i].l) { x--; temp-=(LL)num[c[x]]*(LL)num[c[x]]; num[c[x]]++; temp+=(LL)num[c[x]]*(LL)num[c[x]]; } u[i].a=(LL)(temp-(y-x+1)); u[i].b=(LL)(y-x+1)*(y-x); if(u[i].a!=0) { LL k=gcd((LL)u[i].a,(LL)u[i].b); u[i].a/=k; u[i].b/=k; } else { u[i].a=0; u[i].b=1; } } sort(u,u+m,comp); for(int i=0; i<m; i++) printf("%lld/%lld\n",u[i].a,u[i].b); } return 0;}
0 0
- 小Z的袜子
- 小z的袜子
- 小z的袜子
- [BZOJ2038]小Z的袜子
- 小Z的袜子(hose)
- bzoj2038小z的袜子
- 小Z的袜子--莫队
- 【莫队算法】小z的袜子
- BZOJ 2038: 小Z的袜子
- BZOJ 2038小Z的袜子 分块
- BZOJ 2038 小z的袜子
- bzoj-2038 小Z的袜子 hose
- HYSBZ 2038 小Z的袜子(hose)
- BZOJ 2038 小Z的袜子(hose)
- BZOJ 2038 小Z的袜子
- 【BZOJ】2038 小Z的袜子
- 小Z的袜子(莫队算法)
- 小Z的袜子【莫队算法】
- RubyGems简介和使用 ruby+gem常用命令
- 凹凸贴图---------------- Bump Map vs Normal Map vs Parallax Map
- 【HEVC学习与研究】31、HM编码器的基本结构
- 【搞笑】如何逼死维护人员
- Objective-C的动态特性
- 小z的袜子
- 《数字图象处理——基本的灰度变化》
- linux下java环境设置
- 【IOS编程 01】开发环境
- hibernate 的hql查询语句总结
- SlidingMenu , ActionBarSherLock 工程的导入
- 关于“cvPyrSegmentation”找不到标识符和与之相关的编译失败的问题
- 【HEVC学习与研究】32、编码一个CU(帧内部分)1
- 解决eclipse中java代码注释变成乱码的问题