HDU 5023 a segment upt mayor's performance art (line segment tree)

Source: Internet
Author: User
A snapshot upt 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 descriptionWhen upt 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
 
Question:
Two operations are available for coloring fragments:
First, p a B c changes the color from segment A to segment B to C.
Type 2: q a B: Ask what colors are available from segment A to segment B, and enter these colors in numbers from small to large. Do not repeat them.
The default initial color of the clip is 2.
Ideas:
1. Use the line segment tree for range coverage.
2. Attach a 60-size array to each node to merge the color numbers of the left and right sons into the array of the current node (because the color number is up to 30 ).
3. Each merge operation requires sorting and deduplication of the color numbers of the left and right sons to prevent the number of records from exceeding the array size.
4. Because 0 <n <= 1,000,000 is too large, you can hash all A and B to reduce the size of the build and prevent memory bursts.

/*************************************** * *********************************> File Name: hdu5023.cpp> author: bslin> mail: [email protected]> created time: ******************************** **************************************** /# include <stdio. h> # include <algorithm> # include <string. h> using namespace STD; const int n = 200010; const int M = 100010; struct node {int L, R; int CNT; bool flag; int num [70];} tree [n <2]; int ans [70], TOT; int hash [N], a [m], B [m], C [m]; char op [m]; void pushup (INT p) {int I, CNT; memset (tree [p]. num, 0, sizeof (tree [p]. num); for (I = 0; I <tree [P <1]. CNT; I ++) {tree [p]. num [I] = tree [P <1]. num [I];} int Len = tree [P <1]. CNT; for (I = 0; I <tree [P <1 | 1]. CNT; I ++) {tree [p]. num [Len + I] = tree [P <1 | 1]. num [I];} CNT = tree [P <1]. CNT + tre E [P <1 | 1]. CNT; sort (tree [p]. num, tree [p]. num + CNT); tree [p]. CNT = unique (tree [p]. num, tree [p]. num + CNT)-tree [p]. num;} void Pushdown (INT p) {If (tree [p]. flag) {tree [P <1]. flag = tree [P <1 | 1]. flag = 1; tree [p]. flag = 0; memset (tree [P <1]. num, 0, sizeof (tree [P <1]. num); memset (tree [P <1 | 1]. num, 0, sizeof (tree [P <1 | 1]. num); tree [P <1]. CNT = 1; tree [P <1 | 1]. CNT = 1; tree [P <1]. Num [0] = tree [p]. num [0]; tree [P <1 | 1]. num [0] = tree [p]. num [0] ;}} void build (int l, int R, int p) {tree [p]. L = L; tree [p]. R = r; tree [p]. flag = 0; tree [p]. CNT = 1; memset (tree [p]. num, 0, sizeof (tree [p]. num); tree [p]. num [0] = 2; If (L = r) {return;} int mid = (L + r)> 1; build (L, mid, P <1); Build (Mid + 1, R, P <1 | 1);} void Update (int l, int R, int Val, int P) {If (tree [p]. L = L & tree [p]. R = r) {Memset (tree [p]. num, 0, sizeof (tree [p]. num); tree [p]. CNT = 1; tree [p]. num [0] = val; tree [p]. flag = 1; return;} Pushdown (p); int mid = (tree [p]. L + tree [p]. r)> 1; if (r <= mid) Update (L, R, Val, P <1); else if (L> mid) Update (L, R, val, P <1 | 1); else {Update (L, mid, Val, P <1); Update (Mid + 1, R, Val, P <1 | 1) ;}pushup (p) ;}void query (int l, int R, int p) {If (tree [p]. L = L & tree [p]. R = r) {int I; for (I = 0; I <tree [p]. CNT; I ++) {ans [tot ++] = tree [p]. num [I];} Sort (ANS, ANS + ToT); Tot = unique (ANS, ANS + ToT)-ans; return;} Pushdown (P ); int mid = (tree [p]. L + tree [p]. r)> 1; if (mid> = r) query (L, R, P <1); else if (L> mid) query (L, R, P <1 | 1); else {query (L, mid, P <1); query (Mid + 1, R, P <1 | 1 );}} int main (INT argc, char * argv []) {# ifndef online_judgefreopen ("in", "r", stdin); # En Difint n, m, I, j; int HH; while (scanf ("% d", & N, & M )! = EOF) {If (n = 0 & M = 0) break; HH = 0; getchar (); for (I = 0; I <m; I ++) {scanf ("% C", & OP [I]); If (OP [I] = 'P ') {scanf ("% d", & A [I], & B [I], & C [I]); hash [HH ++] = A [I]; hash [HH ++] = B [I];} else {scanf ("% d ", & A [I], & B [I]); hash [HH ++] = A [I]; hash [HH ++] = B [I];} getchar () ;}sort (hash, hash + HH); HH = unique (hash, hash + HH)-Hash; build (1, HH, 1 ); for (I = 0; I <m; I ++) {A [I] = lower_bound (hash, hash + HH, a [I])-Hash + 1; B [I] = lower_bound (hash, hash + HH, B [I])-Hash + 1 ;}for (I = 0; I <m; I ++) {If (OP [I] = 'P') {update (A [I], B [I], C [I], 1 );} else {memset (ANS, 0, sizeof (ANS); Tot = 0; query (A [I], B [I], 1); For (j = 0; j <tot; j ++) {If (j = 0) printf ("% d", ANS [0]); else printf ("% d ", ans [J]);} printf ("\ n ");}}}}


HDU 5023 a segment upt mayor's performance art (line segment tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.