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
Test instructions: To the n interval coloring, all painted color, the output can be seen in the final color, and the number of intervals. The 1~2,2~3 is an interval, but the 1~2,3~4 is two intervals.
#include <stdio.h>#include<string.h>#include<algorithm>using namespacestd;Const intn=8010;structnode{intLeft , right, col;} no[4*N];intColor[n], t;///the color array records the number of intervalsvoidBulid (intLeftintRightintroot) { intmid; No[root].left=Left ; No[root].right=Right ; No[root].col= -1; if(left = right)return ; Mid= (left+right)/2; Bulid (left, Mid, Root*2); Bulid (Mid+1, right, root*2+1);}voidUpdate (intLeftintRightintColintroot) { intmid; if(No[root].col = = col)return ; if(No[root].left = = Left && No[root].right = =Right ) {No[root].col=Col; return ; } if(No[root].col >=0)///If the interval is colored, it is updated downward{no[root*2].col =No[root].col; No[root*2+1].col =No[root].col; No[root].col= -2; } Mid= (no[root].left+no[root].right)/2; if(Right <= mid) Update (left, right, Col, root*2); Else if(Left > mid) Update (left, right, Col, root*2+1); Else{Update (left, Mid, col, Root*2); Update (Mid+1, right, col, root*2+1); } No[root].col= -2;///mark the interval partially or completely painted}voidSolve (introot) { if(No[root].col = =-1) {T= -1; return ; } if(No[root].col! =-2)///This interval has only one color { if(No[root].col! = t)///determines whether the interval color is the same as the previous interval, and the value of color and T is updated differently{Color[no[root].col]++; T=No[root].col; } return ; } if(No[root].left! = no[root].right)///Look down{Solve (root*2); Solve (Root*2+1); }}intMain () {intN, a, B, C, I; while(SCANF ("%d", &n)! =EOF) {Bulid (0,8000,1); while(n--) {scanf ("%d%d%d", &a, &b, &c); Update (A+1, B, C,1);///interval discretization To prevent the merging of two or more two or more consecutive and color-identical intervals} memset (color,0,sizeof(color)); T= -1; Solve (1); for(i =0; I <=8000; i++) if(Color[i] >0) printf ("%d%d\n", I, color[i]); printf ("\ n"); } return 0;}
ZOJ 1610 Count the Colors