Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 5023
Problem descriptionupt governors always find ways to get dirty money. paint something, then encrypt 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.
Because a lot of people praised Mayor X's painting (of course, X was a mayor), Mayor x believed more and more that he was a very talented painter. soon Mayor X was not satisfied with only making money. he wanted to be a famous painter. so he joined the local painting associates. other painters had to elect him as the chairman of the associates. then his painting sold at better price.
The local middle school from which Mayor x graduated, wanted to beat Mayor X's horse fart (in Chinese English, beating one's horse fart means flattering one hard ). they built a wall, and invited Mayor X to paint on it. mayor X was very happy. but he really had no idea about what to paint because he cocould only paint very abstract paintings which nobody really understand. mayor X's Secretary suggested that he cocould make this thing not only a painting, but also a performance art work.
This was the secretary's idea:
The wall was divided into N segments and the width of each segment was one Cun (Cun is a Chinese length unit ). all segments were numbered from 1 to n, from left to right. there were 30 kinds of colors Mayor x cocould use to paint the wall. they named those colors as color 1, color 2 .... color 30. the wall's original color was color 2. every time mayor x wocould paint some consecutive segments with a certain kind of color, and he did this for every times. trying to make his performance art fancy, Mayor X declared that at any moment, if someone asked how many kind of colors were there on any consecutive segments, He cocould give the number immediately without counting.
But Mayor X didn't know how to give the right answer. your friend, Mr. W was an secret officer of anti-attack uption bureau, he helped Mayor X on this problem and gained his trust. do you know how mr. Q did this? Inputthere are several test cases.
For each test case:
The first line contains two integers, N and M, meaning that the wall is divided into N segments and there are m operations (0 <n <= 1,000,000; 0 <m <= 100,000)
Then M lines follow, each representing an operation. There are two kinds of operations, as described below:
1) P A B C
A, B and C are integers. this operation means that Mayor x painted all segments from segment A to segment B with color C (0 <A <= B <= N, 0 <C <= 30 ).
2) Q A B
A and B are integers. this is a query operation. it means that someone asked that how many kinds of colors were there from segment A to segment B (0 <A <= B <= N ).
Please note that the operations are given in time sequence.
The input ends with m = 0 and n = 0. outputfor each query operation, print all kinds of color on the queried segments. for color 1, print 1, for color 2, print 2... etc. and this color sequence must be in ascending order. sample Input
5 10P 1 2 3P 2 3 4Q 2 3Q 1 3P 3 5 4P 1 2 7Q 1 3Q 3 4P 5 5 8Q 1 50 0
Sample output
43 44 744 7 8
Source2014 ACM/ICPC Asia Regional Guangzhou online
Question:
P: The C color in the diagram between A and B;
Q: The color types of all vertices from A to B;
PS: according to the Code provided by my teammates, I was stunned;
Ideas:
Is the use of the beautiful bit operation, initially 1, when there is a certain color, <representing the number of this color,
At last, the color ranges from 1 to 30 colors in each output.> and & 1 can be used to check the color!
The Code is as follows:
# Include <cstdio> # include <cstring> # include <algorithm> using namespace STD; # define lson L, mid, RT <1 # define rson Mid + 1, R, RT <1 | 1 # define ll intconst int maxn = 1100017; ll add [maxn <2]; ll sum [maxn <2]; void pushup (int rt) {// update the information of the current node to the parent node sum [RT] = sum [RT <1] | sum [RT <1 | 1]; // total color} void Pushdown (int rt, int m) {If (add [RT]) {Add [RT <1] = add [RT]; add [RT <1 | 1] = add [RT]; sum [RT <1] = add [RT]; sum [RT <1 | 1] = add [RT]; add [RT] = 0; // After the mark is moved to the son node, after the delay mark of the parent node is removed, the current node's mark domain is cleared} void build (int l, int R, int RT) {Add [RT] = 0; // Initialization is not marked as if (L = r) {sum [RT] = 2; // The initial color is 2 return ;} int mid = (L + r)> 1; build (lson); Build (rson); pushup (RT);} void Update (int l, int R, int C, int L, int R, int RT) {If (L <= L & R <= r) {Add [RT] = 1 <(c-1 ); // bitwise shifts left to indicate that sum [RT] = 1 <(c-1); return;} Pushdown (RT, R-l + 1) has a certain color ); // ---- the latency flag field transmits int mid = (L + r)> 1; if (L <= mid) Update (L, R, C, lson ); // update the left son if (mid <r) Update (L, R, C, rson); // update the right son pushup (RT);} ll query (INT l, int R, int L, int R, int RT) {If (L <= L & R <= r) {return sum [RT];} // when you want to obtain the value of the RT subnode, you must first move the RT delay mark downward Pushdown (RT, R-l + 1 ); int mid = (L + r)> 1; ll ret = 0; If (L <= mid) RET | = query (L, R, lson ); if (mid <r) RET | = query (L, R, rson); return ret;} int main () {int N, Q; int A, B, C; while (scanf ("% d", & N, & Q) {If (n = 0 & Q = 0) break; build (1, n, 1); // build while (Q --) // Q is the number of inquiries {char op [2]; scanf ("% s", OP ); if (OP [0] = 'q') // Q indicates the number of inquiries {scanf ("% d", & A, & B ); ll TT = query (A, B, 1, n, 1); LL flag = 0; For (INT I = 1; I <= 30; I ++) {If (tt> (I-1) & 1 & flag = 0) {printf ("% d", I); flag = 1 ;} else if (TT >>( i-1) & 1) printf ("% d", I);} printf ("\ n ");} else {scanf ("% d", & A, & B, & C); Update (a, B, c, 1, n, 1); }}return 0 ;}
HDU 5023 a percentile upt mayor's performance art (line segment tree + beautiful bitwise Operation)