题目链接: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\n", i, cnt[i]);
77             }
78         }
79         puts("");
80     }
81 }

题目:

Count the Colors


Time Limit: 2 Seconds      Memory Limit: 65536 KB


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

转载于:https://www.cnblogs.com/bolderic/p/7294428.html

ZOJ 1060 Count the Color相关推荐

  1. ZOJ 1610 Count the Colors

    段树:延迟标志+暴力更新 我记得刚学段树做的时候这个话题WA一个版本.....如今,每分钟获得.... Count the Colors Time Limit: 2 Seconds      Memo ...

  2. ZOJ 1610 Count the Colors (线段树区间更新)

    题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...

  3. ZOJ Problem Set - 1067 Color Me Less

    这道题目很简单,考察的就是结构体数组的应用,直接贴代码了 #include <stdio.h> #include <math.h>typedef struct color {i ...

  4. ZOJ 1610 Count the Colors 【线段树】

    <题目链接> 题目大意: 在[0,8000]这个区间内,不断进行一些操作,将其中的一些区间染成特定颜色,如果区间重复的话,后面染的色块会覆盖前面染的色块,问最终[0,8000]这个区间内每 ...

  5. python color属性_Python turtle.color方法代码示例

    本文整理汇总了Python中turtle.color方法的典型用法代码示例.如果您正苦于以下问题:Python turtle.color方法的具体用法?Python turtle.color怎么用?P ...

  6. ZOJ Monthly,Feburary 2012 部分题解

    题目链接:点击打开链接 ZOJ 3573 Under Attack 距离做这套题到写题解间隔比较久,题意有些忘了.. #include <iostream> #include <cs ...

  7. python turtle color_Python turtle.color方法代碼示例

    本文整理匯總了Python中turtle.color方法的典型用法代碼示例.如果您正苦於以下問題:Python turtle.color方法的具體用法?Python turtle.color怎麽用?P ...

  8. C语言tzoj1067答案,zoj 1067

    输入一组RGB颜色列表,每行一个颜色,是三个从0~255的整数 前16行是目标颜色组,-1 -1 -1表示结束 16组颜色以后接下来的几行是需要判断的,看它和哪个颜色的距离D最小,找出这个对应的颜色 ...

  9. ZZ:windbg 常用命令

    ZZ from:http://www.cppblog.com/Walker/archive/2012/06/28/146523.html 不要再假装自己写的程序没bug了,不可能的,debug工具你早 ...

  10. Network Dissection:Quantifying Interpretability of Deep Visual Representations(深层视觉表征的量化解释)

    NetworkDissection(深层视觉表征的量化解释)Network Dissection(深层视觉表征的量化解释)NetworkDissection(深层视觉表征的量化解释) conv_lay ...

最新文章

  1. IIS服务器五大安全要素
  2. static,extern关键字作用?
  3. Java黑皮书课后题第5章:**5.21(金融应用:比较不同利率下的贷款)编写程序,让用户输入输入贷款总额和以年为单位的贷款期限,然后显示利率从5%到8%每次递增1/8的各种利率下,每月支付额和总支付
  4. sourceTree添加git密钥步骤
  5. 元素在父元素内垂直居中的思路
  6. java中如果需要返回多个值怎么办
  7. 笔试题:在整数数组中找到重复的数字
  8. 模块公有包中不能导入私有包的内容
  9. 多功能雨伞项目计划书_多功能的雨伞创业项目计划书
  10. MATLAB机器人工具箱使用
  11. 程序员必备的七个电脑软件
  12. ESP8266多任务处理---Ticker库
  13. 云、PaaS、DevOps难以名状的三角恋,你怎么看?
  14. MySQL Cluster导入数据表时报错:Got error 708 'No more attribute metadata records (increas
  15. 风控系统就该这么设计(万能通用),那是相当稳定
  16. 【PTA】平面向量加法
  17. ASA防火墙之透明模式的使用及配置
  18. 如何设计一个比较通用的爬虫系统
  19. 标贝科技| AI裁判、写手、保姆…站在人性和技术的十字路口
  20. 别让自己 “墙” 了自己

热门文章

  1. C++类中在构造器中调用本类的另外构造器
  2. 管理感悟:要知道感恩,要改正错误
  3. TranslateMessage 和 DispatchMessage
  4. python4.2_python4.2参数传入
  5. python3 获取文件目录_python3--os.path获取当前文件的绝对路径和所在目录
  6. int转字符串_python3基础01数值和字符串(一)
  7. linux 创建交换文件格式,Linux系统下的交换分区和交换文件的含义 (创建交换分区文件基本文件的swap)...
  8. matlab frontier,使用Matlab计算Efficient frontier
  9. 数据结构知识点总结pdf_闭关修炼31天,“啃完”346页pdf,我终于四面拿下阿里offer!...
  10. vgh电压高了有什么_能源网建设的风电高电压穿越测试有何作用