题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610
解题思路:线段树区间更新,点查询即可。需要注意的是,线段数量n与线段的端点最大值不同,因此每次建树都要build(1, 8000, 1);另外由于题中给出的是端点坐标,与实际线段不是完全对应的。
代码:
1 const int maxn = 8010 * 4; 2 int allsame[maxn], col[maxn]; 3 int n; 4 int cnt[maxn]; 5 6 void build(int l, int r, int k){ 7 allsame[k] = 0; col[k] = -1; 8 if(l == r) return; 9 10 int lc = k << 1, rc = k << 1 | 1; 11 int mid = (l + r) >> 1; 12 build(l, mid, lc); 13 build(mid + 1, r, rc); 14 } 15 void update(int ul, int ur, int x, int l, int r, int k){ 16 if(ul <= l && ur >= r){ 17 allsame[k] = 1; 18 col[k] = x; 19 return; 20 } 21 if(ul > r || ur < l) return; 22 if(ul == l && l == r) { 23 allsame[k] = 1; 24 col[k] = x; 25 return; 26 } 27 int lc = k << 1, rc = k << 1 | 1; 28 int mid = (l + r) >> 1; 29 if(allsame[k] == 1 && col[k] != -1){ 30 allsame[lc] = 1; col[lc] = col[k]; 31 allsame[rc] = 1; col[rc] = col[k]; 32 allsame[k] = 0; col[k] = -1; 33 } 34 35 update(ul, ur, x, l, mid, lc); 36 update(ul, ur, x, mid + 1, r, rc); 37 } 38 int query(int x, int l, int r, int k){ 39 if(l <= x && r >= x && allsame[k]){ 40 return col[k]; 41 } 42 if(l == r && l == x){ 43 return col[k]; 44 } 45 46 int mid = (l + r) >> 1, lc = k << 1, rc = k << 1 | 1; 47 if(allsame[k] && col[k] != -1){ 48 allsame[lc] = 1; col[lc] = col[k]; 49 allsame[rc] = 1; col[rc] = col[k]; 50 allsame[k] = 0; col[k] = -1; 51 } 52 if(x <= mid) return query(x, l, mid, lc); 53 else return query(x, mid + 1, r, rc); 54 } 55 int main(){ 56 while(scanf("%d", &n) != EOF){ 57 build(1, 8000, 1); 58 memset(cnt, 0, sizeof(cnt)); 59 int maxc = 0, st, ed, pc, maxed = 0; 60 for(int i = 0; i < n; i++){ 61 scanf("%d %d %d", &st, &ed, &pc); 62 maxc = max(maxc, pc); 63 maxed = max(maxed, ed); 64 update(st + 1, ed, pc, 1, 8000, 1); 65 } 66 int lac = -1; 67 for(int i = 1; i <= maxed; i++){ 68 int tmp = query(i, 1, 8000, 1); 69 if(tmp != -1) { 70 if(tmp != lac) cnt[tmp]++; 71 } 72 lac = tmp; 73 } 74 for(int i = 0; i <= maxc; i++){ 75 if(cnt[i] > 0){ 76 printf("%d %d ", i, cnt[i]); 77 } 78 } 79 puts(""); 80 } 81 }
题目:
Painting some colored segments on a line, some previously painted segments may 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 each data set contains exactly one integer n, 1 <= n <= 8000, equal to the number of colored segments.
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 are in the range [0, 8000], and they are all integers.
Input may contain several data set, process to the end of file.
Output
Each line of the output should contain a color index that can be seen from the top, following the count of the segments of this color, they should be printed according to the color index.
If some color can't be 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