设为首页 加入收藏

TOP

线段树区间更新,区间统计 poj 2777 Count Color
2015-11-21 01:01:09 来源: 作者: 【 】 浏览:2
Tags:线段 区间 更新 统计 poj 2777 Count Color

题意:

将一段长为L的板子染色,板子可分为编号为1,2,3...L的L段,总共有O次操作,操作有两种:1.将A到B区间染为颜色C 2.询问A到B区间有多少种颜色。颜色从1到T编号,不超过30种。

?

思路:1.由于颜色不超过30种,所以可以考虑位运算,每一位代表一种颜色,一个32位整数就可以存储所有的颜色状态。

2.对于操作一,就是区间更新操作,需要用lazy操作,当需要更新子节点时才往下更新,把子节点的颜色染为lazy的颜色(即上一个节点染得颜色),并且把lazy下移。(代码中的pushdown函数),更新子节点后,用pushup函数将子节点的染色状态更新到父节点。

3.对于操作二,就是区间询问,也用到lazy操作,需要询问子节点时使用pushdown函数将子节点染色,然后查询到目标区间后,返回目标区间的染色状态(一个32位整数),最后将所查区间的染色状态按位与,二进制中1的个数即所用颜色的数目。

易错点:

1.注意使用lazy操作,不然超时。

2.建树的时候也要用pushup操作更新父节点

3.在pushup函数中,是把左右子节点的染色状态按位或,而pushdown函数中,是直接将父节点的上次所染颜色更新到子节点。原因是pushup是将子区间并起来,而pushdown是区间染色。

4.查询的时候也要使用pushdown,因为有可能所查子节点状态未被更新。

5.A可能大于B

代码:

?

#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
      
        #include
       
         #include 
        
          #include 
         
           #include 
          
            #include 
           
             #include 
            
              #include
              #include 
              
                #include 
               
                 #include 
                
                  #include 
                 
                   #include 
                  
                    #include 
                   
                     #include 
                    
                      #include 
                     
                       #include 
                      
                        #include 
                       
                         #include 
                        
                          #include 
                         
                           using namespace std; #define PB push_back #define MP make_pair #define REP(i,x,n) for(int i=x;i<(n);++i) #define FOR(i,l,h) for(int i=(l);i<=(h);++i) #define FORD(i,h,l) for(int i=(h);i>=(l);--i) #define SZ(X) ((int)(X).size()) #define ALL(X) (X).begin(), (X).end() #define RI(X) scanf("%d", &(X)) #define RII(X, Y) scanf("%d%d", &(X), &(Y)) #define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z)) #define DRI(X) int (X); scanf("%d", &X) #define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y) #define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z) #define OI(X) printf("%d",X); #define RS(X) scanf("%s", (X)) #define MS0(X) memset((X), 0, sizeof((X))) #define MS1(X) memset((X), -1, sizeof((X))) #define LEN(X) strlen(X) #define F first #define S second #define Swap(a, b) (a ^= b, b ^= a, a ^= b) #define Dpoint strcut node{int x,y} #define cmpd int cmp(const int &a,const int &b){return a>b;} /*#ifdef HOME freopen("in.txt","r",stdin); #endif*/ const int MOD = 1e9+7; typedef vector
                          
                            VI; typedef vector
                           
                             VS; typedef vector
                            
                              VD; typedef long long LL; typedef pair
                             
                               PII; //#define HOME int Scan() { int res = 0, ch, flag = 0; if((ch = getchar()) == '-') //判断正负 flag = 1; else if(ch >= '0' && ch <= '9') //得到完整的数 res = ch - '0'; while((ch = getchar()) >= '0' && ch <= '9' ) res = res * 10 + ch - '0'; return flag ? -res : res; } /*----------------PLEASE-----DO-----NOT-----HACK-----ME--------------------*/ long long int col[400000+10]; int num; int lazy[400000+10]; int l,t,o; void pushdown(int rt) { if(lazy[rt]) { lazy[rt<<1]=lazy[(rt<<1)+1]=lazy[rt]; col[rt<<1]=lazy[rt]; col[(rt<<1)+1]=lazy[rt]; lazy[rt]=0; } } void pushup(int rt) { col[rt]=col[rt<<1]|(col[(rt<<1)+1]); } void build(int l,int r,int rt) { if(l==r) { col[rt]=1; return; } int m=(l+r)>>1; build(l,m,rt<<1); build(m+1,r,(rt<<1)+1); pushup(rt); } void update(int l,int r,int rt,int a,int b,int c) { if(a<=l&&r<=b) { col[rt]=c; lazy[rt]=c; return; } pushdown(rt); int m=(l+r)>>1; if(a<=m) update(l,m,rt<<1,a,b,c); if(b>m) update(m+1,r,(rt<<1)+1,a,b,c); pushup(rt); } long long int query(int l,int r,int rt,int a,int b) { if(a<=l&&r<=b) { return col[rt]; } pushdown(rt); int m=(l+r)>>1; long long int ans=0; if(a<=m) ans=ans|query(l,m,rt<<1,a,b); if(b>m) ans=ans|query(m+1,r,(rt<<1)+1,a,b); return ans; } int main() { cin>>l>>t>>o; MS0(col); MS0(lazy); num=0; build(1,l,1); for(int i=0;i
                              
                               b) { Swap(a,b); } update(1,l,1,a,b,1<<(co-1)); } else if(c=='P') { RII(a,b); num=0; if(a>b) Swap(a,b); long long int ans=query(1,l,1,a,b); for(int j=0;j
                               
                                

?

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇UVA1025---A Spy in the Metro(简.. 下一篇sgu-255 Winsock 3 Beta

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: