POJ 2777 count color(线段树,lazy标记)
来源:互联网 发布:数据库存储过程优点 编辑:程序博客网 时间:2024/05/17 09:00
这里有一个思想:我们在更新的时候不必要更新到叶子节点,只要更新到当前区间包含线段树区间即可。
设计一个标志位,更新到此。
A Simple Problem with Integers 也是一个类似的题目
设计两个函数
push_down 将结点信息传递到下层节点(inc, sub,)
push_up 将下层节点信息反馈到上层(max,min,count)
#include <map>#include <set>#include <queue>#include <cmath>#include <cmath>#include <vector>#include <bitset>#include <vector>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define inf 0x3fffffff#define mid ((l+r)>>1)#define LSON(x) (x) << 1#define RSON(x) (x) << 1 | 1#define mem(x) memset(x,0,sizeof(x))#define max(a,b) ((a)>(b)?(a):(b))const double eps=1e-6;typedef unsigned __int64 ull;vector< vector<int> > edge;#define lson l, m, rt << 1 #define rson m + 1, r, rt << 1 | 1 const int maxn = 100010;struct {int l,r,color;int lazy;}tt[maxn<<2];void push_up(int p){tt[p].color=tt[p<<1].color | tt[p<<1|1].color;}void push_down(int p){tt[p<<1].lazy=tt[p].lazy;tt[p<<1].color=tt[p].lazy;tt[p<<1|1].lazy=tt[p].lazy;tt[p<<1|1].color=tt[p].lazy;tt[p].lazy=0;}void build(int p,int l,int r){tt[p].l=l;tt[p].r=r;tt[p].color=1;tt[p].lazy=0;if(l==r)return;build(p<<1,l,mid);build(p<<1|1,mid+1,r);}void update(int p,int l,int r,int c){if(tt[p].l>=l && tt[p].r<=r){tt[p].color=c;tt[p].lazy=c;return;}if(tt[p].lazy)push_down(p);int m=(tt[p].l+tt[p].r)>>1;if(r<=m) update(p<<1,l,r,c);else if(l>m) update(p<<1|1,l,r,c);else{update(p<<1,l,m,c);update(p<<1|1,m+1,r,c);}push_up(p);}int query(int p,int l,int r){if(tt[p].l>=l && tt[p].r<=r)return tt[p].color;if(tt[p].lazy)push_down(p);int m=(tt[p].l+tt[p].r)>>1;int ans=0;if(r<=m) ans=query(p<<1,l,r);else if(l>m) ans=query(p<<1|1,l,r);else{ans=query(p<<1,l,m);ans|=query(p<<1|1,m+1,r);}push_up(p);return ans;}int getbit(int x){int i,ans=0;for(i=0;i<32;i++)if( ( (1<<i) & x ) )ans++;return ans;}int main(){int L,T,O;int i,j,k;int A,B,C;char str[30];scanf("%d%d%d",&L,&T,&O);build(1,1,L);for(i=0;i<O;i++){scanf("%s",str);if(str[0]=='C'){scanf("%d%d%d",&A,&B,&C);if(A>B) swap(A,B);k=1<<(C-1);update(1,A,B,k);continue;}scanf("%d%d",&A,&B);if(A>B) swap(A,B);k=query(1,A,B);k=getbit(k);printf("%d\n",k);}return 0;}
1 0
- POJ 2777 Count Color 线段树 + lazy标记
- POJ 2777 count color(线段树,lazy标记)
- POJ 2777 Count Color(线段树、lazy思想)
- POJ 2777 Count Color (线段树、lazy思想)
- 【线段树(lazy)】poj 2777 Count Color
- poj 2777 Count Color (线段树 + 覆盖标记)
- POJ 2777 Count Color(线段树+懒惰标记)
- poj 2777 Count Color(线段树 Lazy-Tag思想 成段更新+区间统计)
- POJ 2777 Count Color(位运算+线段树+lazy+区间更新)
- poj 2777 Count Color 【线段树lazy区间染色 + 查询区间颜色数目 + 状态压缩】
- poj 2777 Count Color(线段树+lazy优化+位运算)
- POJ 2777 Count Color (线段树的区间更新+lazy tag)
- POJ 2777 Count Color 线段树
- POJ 2777 Count Color 线段树
- POJ 2777 Count Color(线段树#1)
- poj 2777 -- Count Color ( 线段树 )
- POJ 2777 Count Color (线段树)
- POJ 2777 Count Color 线段树
- 为了双11写了几个来替代手工的瑕疵品
- 1060: 鹊桥相会
- 混合图欧拉回路 POJ 1637
- mysql 忘记了 root 密码
- hdu2196——Computer
- POJ 2777 count color(线段树,lazy标记)
- 360公司2015年秋季校园招聘笔试考卷(技术类 D)部分试题程序验证和解析5
- 我的CSDN主页开启了
- EK算法模版
- C语言字符串的那些事儿
- 字典序打印1到n的自然数 与 1打印到最大的n位数是不同的
- java内存回收机制
- The K-League (网络流,建图难)
- Java之动态代理