hdu 5023 A Corrupt Mayor's Performance Art (线段树)

把求和操作改为或操作,就可以了。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <cmath>
 6 #define lson l,m,rt<<1
 7 #define rson m+1,r,rt<<1|1
 8 using namespace std;
 9 const int maxn=1111111;
10 int sum[maxn<<2];
11 int col[maxn<<2];
12 void up(int rt){
13     sum[rt]=sum[rt<<1]|sum[rt<<1|1];
14 }
15 void down(int rt){
16     if(col[rt]){
17         col[rt<<1]=col[rt<<1|1]=col[rt];
18         sum[rt<<1]=sum[rt<<1|1]=sum[rt];
19         col[rt]=0;
20     }
21 }
22 void build(int l,int r,int rt){
23     col[rt]=0;
24     sum[rt]=4;
25     if(l==r)return;
26     int m=(l+r)>>1;
27     build(lson);
28     build(rson);
29     up(rt);
30 }
31 void update(int L,int R,int c,int l,int r,int rt){
32     if(L<=l&&R>=r){
33         col[rt]=c;
34         sum[rt]=c;
35         return;
36     }
37     down(rt);
38     int m=(l+r)>>1;
39     if(L<=m)update(L,R,c,lson);
40     if(R>m)update(L,R,c,rson);
41     up(rt);
42 }
43 int query(int L,int R,int l,int r,int rt){
44     if(L<=l&&R>=r)return sum[rt];
45     down(rt);
46     int m=(l+r)>>1;
47     int ans=0;
48     if(L<=m)ans|=query(L,R,lson);
49     if(m<R)ans|=query(L,R,rson);
50     return ans;
51 }
52 int main()
53 {
54 //    freopen("in","r",stdin);
55     int n,q;
56     while(scanf("%d%d",&n,&q)>0&&(n|q)){
57         build(1,n,1);
58         char ch[5];
59         int a,b,c;
60         while(q--){
61             scanf("%s",ch);
62             if(ch[0]=='P'){
63                 scanf("%d%d%d",&a,&b,&c);
64                 update(a,b,1<<c,1,n,1);
65             }
66             else {
67                 scanf("%d%d",&a,&b);
68                 int ans=query(a,b,1,n,1);
69                 int fir=0;
70                 for(int i=1;i<31;i++){
71                     if(ans&(1<<i)){
72                         if(fir)printf(" ");
73                         printf("%d",i);
74                         fir=1;
75                     }
76                 }
77                 puts("");
78             }
79         }
80     }
81     return 0;
82 }
原文地址:https://www.cnblogs.com/wshh/p/3984252.html