light oj 1135 线段树 有助于加深对懒惰标记的理解
来源:互联网 发布:数据挖掘 数学基础 编辑:程序博客网 时间:2024/05/22 01:44
有n个数,刚开始都为0
add i , j 给i,j区间内的数都加1
Q i j 询问i、j间能被三整除的数的个数
线段树记录三个域
对三取余为0的数的个数
。。。。。1.。。。。。
。。。。。2.。。。。。
可以保存在一个数组里面
考虑到每次给一个区间加1的时候,区间内对3取余为1的数的个数变成了对三取余为2,2的变成了0,0的变成了1
所以每次更新到区间或者把信息(懒惰标记)往下传的时候只需要把相应的域做一下调整即可
View Code
#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 (线段树+懒惰标记)
- hdu 1698 线段树 懒惰标记
- 一个纯字符的(最原始版本)led驱动(有有助于加深对字符设备的理解)
- 加深对组件的理解
- 对scanf的加深理解
- 加深对JAVA的理解
- 线段树1(带懒惰标记的区间更新和整棵树的查询)
- 蓝桥杯 算法训练 校门外的树(线段树+懒惰标记)
- poj3468 A Simple Problem with Integers (线段树的懒惰标记)
- POJ 3468 线段树 区间操作 懒惰标记
- hdu 1556 Color the ball(线段树+懒惰标记)
- hdu 3397 线段树+区间合并+懒惰标记 好题
- 【HDU】4092 Nice boat ——线段树 懒惰标记
- 线段树特殊懒惰标记hdu3954(hdu4902)
- 线段树区间修改 懒惰标记 维护和、最大值、最小值
- hdu1698 Just a Hook (线段树区间更新 懒惰标记)
- 织梦会员系统bug
- light oj 1427 AC自动机+dfs
- Codeforces Beta Round #3 练习
- CF 4 练习
- 背包输出方案 Pro-Test Voting
- light oj 1135 线段树 有助于加深对懒惰标记的理解
- CF 5 练习
- codeforces 121 (比赛) codeforces 191C
- codeforces 191 E 数据结构题
- uva 11983 求矩形覆盖k次以上的面积并
- codeforces 122
- CF 85D 五颗线段树
- poj 2785 哈希!!!
- 树链剖分初步 spoj 375 hdu 3966 codeforces 191C