HDU 5023 a running upt mayor's performance art (line segment tree + state compression)
Question B of last week's online competition. The question is very long, but it is useless.
Question: Line Segment Tree operations
P l r c: Replace the [L, R] interval color with C
Q l r queries the total number of colors in the [L, R] interval and outputs them in ascending order.
[In the initial state, the walls are all 2 colors, with a total of 30 colors]
Analysis ①: considering a maximum of 30 colors, a single int type variable can be used to store the status, with 32-bit
Specifically, we use the segment update of the Line Segment tree, and lazy is the lazy mark. Note: 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 ll; 22 typedef unsigned 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,, 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 = (); 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] = La ZY [O]; // pass down and directly modify Col value 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; 7 9 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", & N, & M) & (n + M) 104 {105 getchar (); 106 CLR (COL, 0 ), CLR (lazy, 0); 107 build (1, n, 1); 108 // update (1, n, 109, N, 1); rep (I, 0, m) 110 {111 CH = getchar (); 112 if (CH = 'P') 113 {114 scanf ("% d", & L, & R, & Co); 115 Update (L, R, co-1, 1, n, 1); 116} 117 else118 {119 scanf ("% 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 int main () 140 {141 # ifdef bug142 freopen ("in.txt", "r", stdin); 143 # endif144 orz (); 145 return 0; 146}Code Jun
Analysis ②: Col [O] stores the color of the current line segment,
HDU 5023 a snapshot upt mayor's performance art