light oj 1135 (懒惰标记)转自网络
来源:互联网 发布:网络威胁类型 编辑:程序博客网 时间:2024/06/14 05:05
转自:http://www.cnblogs.com/wuyiqi/archive/2012/05/27/2520642.html
有n个数,刚开始都为0
add i , j 给i,j区间内的数都加1
Q i j 询问i、j间能被三整除的数的个数
线段树记录三个域
对三取余为0的数的个数
。。。。。1.。。。。。
。。。。。2.。。。。。
可以保存在一个数组里面
考虑到每次给一个区间加1的时候,区间内对3取余为1的数的个数变成了对三取余为2,2的变成了0,0的变成了1
所以每次更新到区间或者把信息(懒惰标记)往下传的时候只需要把相应的域做一下调整即可
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1const int maxn = 100010;int cov[maxn<<2];int sum[maxn<<2][3];void pushup(int rt){ for(int i=0;i<3;i++){ sum[rt][i]=sum[rt<<1][i]+sum[rt<<1|1][i]; }}void build(int l,int r,int rt){ cov[rt]=0; if(l==r){ sum[rt][0]=1; sum[rt][1]=sum[rt][2]=0; return ; } int m=(l+r)>>1; build(lson); build(rson); pushup(rt);}void make(int rt){ int tmp=sum[rt][0]; sum[rt][0]=sum[rt][2]; sum[rt][2]=sum[rt][1]; sum[rt][1]=tmp;}void pushdown(int rt){ if(cov[rt]){ cov[rt<<1]+=cov[rt]; cov[rt<<1|1]+=cov[rt]; for(int i=0;i<cov[rt]%3;i++){ make(rt<<1); make(rt<<1|1); } cov[rt]=0; }}void update(int L,int R,int l,int r,int rt){ if(L<=l&&r<=R){ cov[rt]++; make(rt); return ; } pushdown(rt); int m=(l+r)>>1; if(L<=m) update(L,R,lson); if(R>m) update(L,R,rson); pushup(rt);}int query(int L,int R,int l,int r,int rt){ if(L<=l&&r<=R){ return sum[rt][0]; } pushdown(rt); int m=(l+r)>>1; int ret=0; if(L<=m) ret+=query(L,R,lson); if(R>m) ret+=query(L,R,rson); return ret;}int main(){ int t,ca=1,i,j,k,a,b,n,q,op; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&q); printf("Case %d:\n",ca++); build(1,n,1); while(q--){ scanf("%d%d%d",&op,&a,&b);a++;b++; if(op==0) update(a,b,1,n,1); else printf("%d\n",query(a,b,1,n,1)); } } return 0;}
- light oj 1135 (懒惰标记)转自网络
- light oj 1135 线段树 有助于加深对懒惰标记的理解
- HDU 3275 Light (线段树+懒惰标记)
- LightOJ 1135 Count the Multiples of 3 (Segmengt + 懒惰标记)
- light oj(简化复杂度)
- hdu 1556 Color the ball(线段树+懒惰标记)
- 线段树特殊懒惰标记hdu3954(hdu4902)
- 然而沼跃鱼早就看穿了一切(暴力+懒惰标记)
- bzoj1251 序列终结者(Splay Tree+懒惰标记)
- bzoj1503 [NOI2004]郁闷的出纳员(名次树+懒惰标记)
- hdu1698 Just a Hook(成段更新+懒惰标记)
- hdu1698Just a Hook(线段树+懒惰标记)
- POJ 2777 Count Color(线段树+懒惰标记)
- light oj
- light oj
- Light OJ
- Light OJ
- !Light oj 1066--BFS--(队列溢出)
- 编译linux内核
- MFC中release版本和debug版本区别
- 关于UILocalNotification一些更深刻的认识
- 10个在线正则表达式测试网站推荐
- OE的中文参考资源
- light oj 1135 (懒惰标记)转自网络
- JS 获取动太ID,播放视频
- matlab 绘图 消除白边的问题
- 13种提高系统伸缩性的最佳实践
- sql语句使用(一)
- 最近工作的一些感想以及个人弱点
- 实战数据结构(3)_两个单链表间的合并操作
- 15种提高系统伸缩性和性能的最佳实践
- 转载的有用的博客