Title Link: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610
Description
Painting some colored segments on a line, some previously painted segments could be covered by some the subsequent ones.
Your task is counting the segments of different colors you can see at last.
Input
The first line of all data set contains exactly one integer n, 1 <= n <= 8000, equal to the number of colored Segme Nts.
Each of the following n lines consists of exactly 3 nonnegative integers separated by single spaces:
X1 X2 C
X1 and X2 indicate the left endpoint and right endpoint of the segment, C indicates the color of the segment.
All the numbers is in the range [0, 8000], and they is all integers.
Input may contain several data set, and process to the end of file.
Output
Each line of the output should contain a color index this can be seen from the top, following the count of the segments of This color, they should is printed according to the color index.
If some color can ' t is seen, you shouldn ' t print it.
Print a blank line after every dataset.
Sample Input
5
0 4 4
0 3 1
3 4 2
0 2 2
0 2 3
4
0 1 1
3 4 1
1 3 2
1 3 1
6
0 1 0
1 2 1
2 3 1
1 2 0
2 3 0
1 2 1
Sample Output
1 1
2 1
3 1
1 1
0 2
1 1
When [0, 8000] is painted, the coating will cover the front, and ask how many blocks each color can last distinguish.
In the achievement, the unit of the tree is the interval, rather than the point such as [0, 8000] The left and right sons should be [0, 4000], [4000,8000];
when asked, it is necessary to determine whether the color of the adjacent interval is the same as the color recorded in the previous interval of the temp to solve the problem.
1#include <iostream>2#include <cstring>3 using namespacestd;4 5 intcolor[8010];6 intmark[20000];7 inttemp;8 9 voidBuild (intIintLintR) {TenMark[i] =-1;//-1 means no coating One A if(L +1= = R)return; - - intMid = (L + r) >>1; theBuild (i <<1, L, mid); -Build ((i <<1) |1, Mid, R); - } - + voidInsert (intIintLintRintZintYintc) { - if(L = = r)return; + if(z <= l && y >=R) { AMark[i] =C; at return; - } - if(Mark[i] = = c)return; - if(Mark[i] >=0 ){ -Mark[i <<1] =Mark[i]; -mark[(i <<1) |1] =Mark[i]; inMark[i] =-2;//the interval contains multiple colors - } to intMid = (L + r) >>1; + if(Y <= mid) Insert (i <<1, L, Mid, Z, y, c); - Else if(Z >= mid) Insert ((i <<1) |1, Mid, R, Z, y, c); the Else{ *Insert (i <<1, L, Mid, Z, Mid, c); $Insert ((i <<1) |1, Mid, R, Mid, Y, c);Panax Notoginseng } -Mark[i] =-2; the } + A voidQueue (intIintLintR) { the if(Mark[i] = =-1 ){ +temp =-1; - return; $ } $ if(Mark[i] >=0 ){ - if(Temp! =Mark[i]) { -temp = Mark[i];//record the color of the previous paragraph thecolor[mark[i]]++; - }Wuyi return; the } - if(L +1!=R) { Wu intMid = (L + r) >>1; -Queue (i <<1, L, mid); AboutQueue ((i <<1) |1, Mid, R); $ } - } - - intMain () { AIos::sync_with_stdio (false ); + the intN; - while(Cin >>N) { $ theBuild (1,0,8000 ); thememset (Color,0,sizeof(color)); the the intA, B, c, max =0; - for(inti =1; I <= N; i++ ){ inCin >> a >> b >>C; theInsert (1,0,8000, A, b, c); themax = c > Max?C:max; About } the thetemp =-1; theQueue (1,0,8000 ); + - for(inti =0; I <= Max; i++ ) the if(Color[i]) cout << i <<" "<< Color[i] <<Endl;Bayi thecout <<Endl; the } - - return 0; the}
ZOJ-1610 Count the Colors (segment tree)