xtu数据结构 G. Count the Colors

本文介绍如何使用线段树解决彩色段落计数问题,包括输入解析、线段树构建、更新操作和最终颜色计数输出。通过实例演示,展示线段树在解决此类问题中的应用。

G. Count the Colors

Time Limit: 2000ms
Memory Limit: 65536KB
64-bit integer IO format: %lld      Java class name: Main
 
 
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

 

 解题:线段树。。。比较奇葩的线段树,此树的叶子节点必须是一个长度为1的线段,而不是我通常所写的那种叶子节点就是一个点的那种。
 
[0,4]
[0,2][2,4]
[0,1][1,2][2,3][3,4]
是这种线段树。
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #define LL long long
13 #define INF 0x3f3f3f
14 using namespace std;
15 const int maxn = 8010;
16 struct node {
17     int lt,rt,color;
18 } tree[maxn<<2];
19 int col[maxn],temp;
20 void build(int lt,int rt,int v) {
21     int mid = (lt+rt)>>1;
22     tree[v].lt = lt;
23     tree[v].rt = rt;
24     tree[v].color = -1;
25     if(lt+1 == rt) return;
26     build(lt,mid,v<<1);
27     build(mid,rt,v<<1|1);
28 }
29 void update(int lt,int rt,int c,int v) {
30     if(lt == rt || tree[v].color == c) return;
31     if(tree[v].lt >= lt && tree[v].rt <= rt) {
32         tree[v].color = c;
33         return;
34     }
35     if(tree[v].color >= 0) {
36         tree[v<<1].color = tree[v<<1|1].color = tree[v].color;
37         tree[v].color = -2;
38     }
39     int mid = (tree[v].lt+tree[v].rt)>>1;
40     if(rt <= mid) {
41         update(lt,rt,c,v<<1);
42     } else if(lt >= mid) {
43         update(lt,rt,c,v<<1|1);
44     } else {
45         update(lt,mid,c,v<<1);
46         update(mid,rt,c,v<<1|1);
47     }
48     tree[v].color = -2;
49 }
50 void query(int v) {
51     if(tree[v].color == -1) {
52         temp = -1;
53         return;
54     }
55     if(tree[v].color != -2) {
56         if(tree[v].color != temp) {
57             temp = tree[v].color;
58             col[temp]++;
59         }
60         return;
61     }
62     if(tree[v].lt+1 != tree[v].rt) {
63         query(v<<1);
64         query(v<<1|1);
65     }
66 }
67 int main() {
68     int n,i,j,x,y,c,mx;
69     while(~scanf("%d",&n)) {
70         build(0,8002,1);
71         memset(col,0,sizeof(col));
72         mx = 0;
73         for(i = 0; i < n; i++) {
74             scanf("%d%d%d",&x,&y,&c);
75             update(x,y,c,1);
76             if(c > mx) mx = c;
77         }
78         temp = -1;
79         query(1);
80         for(i = 0; i <= mx; i++)
81             if(col[i]) printf("%d %d\n",i,col[i]);
82         printf("\n");
83     }
84     return 0;
85 }
View Code

 

 

转载于:https://www.cnblogs.com/crackpotisback/p/3858359.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值