HDU 5023 A Corrupt Mayor's Performance Art

HDU 5023A Corrupt Mayor‘s Performance Art (线段树 + 状态压缩)

上周网络赛的B题,题目很长但是前面根本没有用题意:线段树操作
P l r c      将 [l,r] 区间颜色换为 cQ l r       查询 [l,r]区间一共有多少种颜色,并按升序输出[ 初始状态墙壁颜色全部为2,总共30种颜色 ]

分析①: 考虑到最多30种颜色,可以直接用一个int型的变量来存放状态,32位正好具体就是用线段树的成段更新,lazy为懒惰标记。注意下Query

  1 /* ***********************************************
  2 Problem       :
  3 File Name     :
  4 Author        :
  5 Created Time  :
  6 ************************************************ */
  7 //#define BUG
  8 #include <cstdio>
  9 #include <cstring>
 10 #include <algorithm>
 11 #include <map>
 12 #include <vector>
 13 #include <list>
 14 #include <queue>
 15 #include <ctime>
 16 #include <iostream>
 17 #include <cmath>
 18 #include <set>
 19 #include <string>
 20 using namespace std;
 21 typedef long long LL;
 22 typedef unsigned long long ULL;
 23 #define INF                 0x7fffffff
 24 #define MAX                 0x3f3f3f3f
 25
 26 #define CLR( a, b )         memset( a, b, sizeof(a) )
 27 #define REP( i, a, b )      for( int i = (a); i < (b); ++i )
 28 #define FOR( i, a, b )      for( int i = (a); i <=(b); ++i )
 29 #define FOD( i, a, b )      for( int i = (a); i >=(b); --i )
 30 #define MID                 ( l + r ) >> 1
 31 #define lson                l, m, o << 1
 32 #define rson                m + 1, r, o << 1 | 1
 33 #define ls                  o << 1
 34 #define rs                  o << 1 | 1
 35
 36 #define MAXN                1010101
 37
 38 int lazy[MAXN << 2];
 39 int col[MAXN << 2];
 40
 41 void PushUp( int o )
 42 {
 43     col[o] = col[ls] | col[rs];
 44 }
 45
 46 void PushDown( int o )
 47 {
 48     if( lazy[o] )
 49     {
 50         lazy[ls] = lazy[o];
 51         lazy[rs] = lazy[o];
 52         col[ls] = col[rs] = lazy[o];//向下传递并直接修改col的值
 53         lazy[o] = 0;
 54     }
 55 }
 56
 57 void Build( int l, int r, int o )
 58 {
 59     if( l == r )
 60     {
 61         col[o] = 1 << (2-1);
 62         return;
 63     }
 64     int m = MID;
 65     Build( lson );
 66     Build( rson );
 67     PushUp( o );
 68 }
 69
 70 void Update( int L, int R, int v, int l, int r, int o )
 71 {
 72     if( L <= l && r <= R )
 73     {
 74         lazy[o] = 1 << v;
 75         col[o] = 1 << v;
 76         return;
 77     }
 78     int m = MID;
 79     PushDown( o );
 80     if(L <= m)  Update( L, R, v, lson );
 81     if(R >  m)  Update( L, R, v, rson );
 82     PushUp( o );
 83
 84 }
 85
 86 int Query ( int L , int R , int l , int r , int o )
 87 {
 88     if( L <= l && r <= R ) return col[o];
 89     int m = MID;
 90     PushDown( o );
 91     if ( R <= m ) return Query ( L , R , lson ) ;
 92     if ( m <  L ) return Query ( L , R , rson ) ;
 93
 94     return Query ( L , R , lson ) | Query ( L , R , rson ) ;
 95 }
 96
 97
 98 void Orz()
 99 {
100     int N,M;
101     int l, r, co;
102     char ch;
103     while( ~scanf("%d%d",&N,&M) && (N+M) )
104     {
105         getchar();
106         CLR( col, 0 ), CLR( lazy, 0 );
107         Build(1,N,1);
108         //Update(1,N,1,1,N,1);
109         REP( i,0,M )
110         {
111             ch = getchar();
112             if( ch == ‘P‘ )
113             {
114                 scanf( "%d %d %d", &l, &r, &co );
115                 Update( l, r, co-1, 1, N, 1 );
116             }
117             else
118             {
119                 scanf( "%d %d", &l, &r );
120                 int ans = Query(l,r,1,N,1);
121                 int flag = 0;
122                 REP( i, 0, 30 )
123                 {
124                     if( ans & (1 << i) )
125                     {
126                         if( flag )    printf(" ");
127                         flag = 1;
128                         printf( "%d", i + 1 );
129                     }
130                 }
131                 puts("");
132             }
133             getchar();
134         }
135     }
136 }
137
138
139 int main()
140 {
141     #ifdef  BUG
142         freopen( "in.txt", "r", stdin );
143     #endif
144     Orz();
145     return 0;
146 }

代码君

分析②: col[o] 存放当前线段内的颜色,

HDU 5023 A Corrupt Mayor's Performance Art

时间: 2024-12-21 16:25:37

HDU 5023 A Corrupt Mayor's Performance Art的相关文章

HDU 5023 A Corrupt Mayor&#39;s Performance Art 线段树区间更新+状态压缩

Link:  http://acm.hdu.edu.cn/showproblem.php?pid=5023 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 #include <vector> 6 #include <string> 7 #include <cmath> 8 using namesp

HDU 5023 A Corrupt Mayor&#39;s Performance Art(线段树区间更新)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 解题报告:一面墙长度为n,有N个单元,每个单元编号从1到n,墙的初始的颜色是2,一共有30种颜色,有两种操作: P a b c  把区间a到b涂成c颜色 Q a b 查询区间a到b的颜色 线段树区间更新,每个节点保存的信息有,存储颜色的c,30种颜色可以压缩到一个int型里面存储,然后还有一个tot,表示这个区间一共有多少种颜色. 对于P操作,依次往下寻找,找要更新的区间,找到要更新的区间之前

HDU 5023 A Corrupt Mayor&#39;s Performance Art (线段树)

A Corrupt Mayor's Performance Art Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Total Submission(s): 255    Accepted Submission(s): 114 Problem Description Corrupt governors always find ways to get dirty money.

ACM学习历程—HDU 5023 A Corrupt Mayor&#39;s Performance Art(广州赛区网赛)(线段树)

Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sell the worthless painting at a high price to someone who wants to bribe him/her on an auction, this seemed a safe way for mayor X to make money. Becaus

2014 网选 广州赛区 hdu 5023 A Corrupt Mayor&#39;s Performance Art

1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<algorithm> 5 #define N 1000005 6 using namespace std; 7 8 int c[35]; 9 int tree[N*4];//正值表示该节点所管理的区间的颜色是纯色,-1表示的是非纯色 10 int n, m; 11 12 void buildT(int ld, int

HDU - 5023 A Corrupt Mayor&#39;s Performance Art(区间修改)

Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sell the worthless painting at a high price to someone who wants to bribe him/her on an auction, this seemed a safe way for mayor X to make money. Becaus

HDU 5023 A Corrupt Mayor&#39;s Performance Art(线段树+优美的位运算)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sell the worthless painting at a high price to someone who wants to bribe him/her on an auction, this

hdu 5023 A Corrupt Mayor&#39;s Performance Art(线段树区间更新)

#include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> using namespace std; int tree[5001000],add[5001000]; int color[50]; int n,m; void pushup(int pos) { tree[pos]=tree[pos<<1]|tree[pos<<1|1]; //更新

hdu 5023 A Corrupt Mayor&#39;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 ma