链接:

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82832#problem/F

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610

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

一个涂色问题,我看到了好几种不同的解法, 感觉太神奇,太奇妙了,先附上自己的代码,再学习一下别人的,写了好几题了,不能追速度,我需要好好琢磨琢磨

代码:

  1. #include<stdio.h>
  2. #include<algorithm>
  3. #include<stdlib.h>
  4. #include<string.h>
  5. using namespace std;
  6.  
  7. #define Lson r<<1
  8. #define Rson r<<1|1
  9.  
  10. const int N = ;
  11.  
  12. struct Node
  13. {
  14. int l, r, c;
  15. } s[N<<];
  16.  
  17. struct node
  18. {
  19. int L, R;
  20. int color;
  21. int Mid()
  22. {
  23. return (L+R)>>;
  24. }
  25. } a[N<<];
  26.  
  27. int Color[N];
  28.  
  29. void CoverColor(int L, int R, int e)
  30. {
  31. for(int i=L; i<=R; i++)
  32. Color[i]=e;
  33. }
  34.  
  35. void UpDate(int r)
  36. {
  37. if(a[r].L != a[r].R)
  38. if(a[Lson].color== && a[Rson].color==)
  39. a[r].color = ;
  40. }
  41.  
  42. void BuildTree(int r, int L, int R)
  43. {
  44. a[r].L = L, a[r].R = R;
  45. a[r].color = ; // 0代表没有颜色覆盖, 1代表未覆盖, 2代表子树有被别的颜色覆盖
  46.  
  47. if(L==R) return ;
  48.  
  49. BuildTree(Lson, L, a[r].Mid());
  50. BuildTree(Rson, a[r].Mid()+, R);
  51. }
  52.  
  53. void Insert(int r, int L, int R, int e)
  54. {
  55. if(a[r].color == ) return ;
  56.  
  57. if(a[r].L==L && a[r].R==R && !a[r].color)
  58. {
  59. a[r].color=;
  60. CoverColor(L, R, e);
  61. return ;
  62. }
  63.  
  64. a[r].color = ;
  65.  
  66. if(R<=a[r].Mid())
  67. Insert(Lson, L, R, e);
  68. else if(L>a[r].Mid())
  69. Insert(Rson, L, R, e);
  70. else
  71. {
  72. Insert(Lson, L, a[r].Mid(), e);
  73. Insert(Rson, a[r].Mid()+, R, e);
  74. }
  75.  
  76. UpDate(r);
  77. }
  78.  
  79. int main()
  80. {
  81. int n;
  82.  
  83. while(scanf("%d", &n)!=EOF)
  84. {
  85. int i, ans[N]={};
  86.  
  87. memset(s, , sizeof(s));
  88. for(i=; i<=n; i++)
  89. scanf("%d%d%d", &s[i].l, &s[i].r, &s[i].c);
  90.  
  91. BuildTree(, , N-);
  92. memset(Color, -, sizeof(Color));
  93.  
  94. for(i=n; i>; i--)
  95. Insert(, s[i].l+, s[i].r, s[i].c);
  96.  
  97. for(i=; i<N; i++)
  98. {
  99. if(Color[i]!=- && (!i || Color[i]!=Color[i-]))
  100. ans[Color[i]]++;
  101. }
  102.  
  103. for(i=; i<N; i++)
  104. if(ans[i]) printf("%d %d\n", i, ans[i]);
  105.  
  106. printf("\n");
  107. }
  108. return ;
  109. }

队友暴力过的代码, 没用线段树

  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<string.h>
  5. using namespace std;
  6. #define N 8006
  7. int cnt[N],a[N];
  8. int main()
  9. {
  10. int n, p, q, c;
  11. while(scanf("%d", &n) != EOF)
  12. {
  13. int Max = ;
  14. memset(a, , sizeof(a));
  15. memset(cnt, , sizeof(cnt));
  16. for(int i=; i<n; i++)
  17. {
  18. scanf("%d%d%d", &p, &q, &c);
  19. for(int j=p; j<q; j++)
  20. a[j] = c+;
  21. Max = max(Max, q);
  22. }
  23. for(int i=; i<Max; i++)
  24. {
  25. while(i!= && a[i]!= && a[i]==a[i-])
  26. i++;
  27. if(a[i] != )
  28. cnt[ a[i]- ]++;
  29. }
  30. for(int i=; i<N; i++)
  31. {
  32. if(cnt[i]!=)
  33. printf("%d %d\n", i, cnt[i]);
  34. }
  35. printf("\n");
  36. }
  37. return ;
  38. }

(线段树) Count the Colors --ZOJ --1610的更多相关文章

  1. F - Count the Colors ZOJ - 1610 线段树染色(染区间映射)

    题意:给一段0-8000的线段染色 问最后 颜色x 有几段 题解:标准线段树  但是没有push_up  最后查询是单点按顺序查询每一个点 考虑过使用区间来维护不同的线段有多少种各色的线段  思路是 ...

  2. F - Count the Colors - zoj 1610(区间覆盖)

    有一块很长的画布,现在想在这块画布上画一些颜色,不过后面画的颜色会把前面画的颜色覆盖掉,现在想知道画完后这块画布的颜色分布,比如 1号颜色有几块,2号颜色有几块.... *************** ...

  3. Count the Colors ZOJ - 1610 区间颜色覆盖

    #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> ...

  4. 线段树区间染色 ZOJ 1610

    Count the Colors ZOJ - 1610 传送门 线段树区间染色求染色的片段数 #include <cstdio> #include <iostream> #in ...

  5. F - Count the Colors

    F - Count the Colors ZOJ - 1610   思路:调了一个小时,但是发现自己线段树木有写错,颜色统计出了错误.但是不明白自己颜色统计为什么错了. 求大佬指点迷津.思路很简单,就 ...

  6. Count Colour_poj2777(线段树+位)

    POJ 2777 Count Color (线段树)   Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  7. ZOJ 1610——Count the Colors——————【线段树区间替换、求不同颜色区间段数】

    Count the Colors Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Subm ...

  8. ZOJ 1610 Count the Colors【题意+线段树区间更新&&单点查询】

    任意门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 Count the Colors Time Limit: 2 ...

  9. ZOJ 1610 Count the Colors (线段树区间更新与统计)

    Painting some colored segments on a line, some previously painted segments may be covered by some th ...

随机推荐

  1. sqlserver列重命名

    EXEC sp_rename 'tablename.[OldFieldName ]', 'NewFieldName', 'COLUMN'

  2. sqlserver分布式 用触发器插入数据

    这个月总公司收购了一家小公司,这家小公司的数据库用的是32位的 Sql2000 ,已经使用很长一段时间了,系统也比较稳定.本着节约成本的原则,总公司保留原公司的一套管理系统,但要求重要数据每天上传到总 ...

  3. c++实现扫雷(坐标)

    昨天在观察贪食蛇的代码时,看到了有如何实现扫雷的c++代码,觉得挺有趣,今天便又试了一下 #include <ctime> #include <cstdlib> #includ ...

  4. ubuntu的文本界面修改字体大小

    使用命令: dpkg-reconfigure console-setup

  5. springboot自定义消息转换器HttpMessageConverter

    在SpringMVC中,可以使用@RequestBody和@ResponseBody两个注解,分别完成请求报文到对象和对象到响应报文的转换,底层这种灵活的消息转换机制就是利用HttpMessageCo ...

  6. 8-导弹拦截一(n^2 and nlogn)

    /*某国为了防御敌国的导弹袭击,研发出一套导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发拦截炮弹能够到达任意的高度,但是以后每一发拦截炮弹都不能高于前一发的高度.某天,雷达捕捉到敌国的多 ...

  7. CentOS 7安装Samba 4.6 版本步骤及错误解决方法

    首先通过这次教训,让我养成一个好习惯:备份  备份  备份      不管做什么配置或者更改什么东西之前先做好备份! 还有我本身的一个坏毛病:眼高手低! 工厂有一台服务器,由以前的运维装的Samba ...

  8. C#控制台自定义背景颜色,字体颜色大全

    效果: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...

  9. 使用jsonp跨域发送请求

    如果获取的数据文件存放在远程服务器上(域名不同,也就是跨域获取数据),则需要使用jsonp类型. 使用这种类型的话,会创建一个查询字符串参数 callback=? ,这个参数会加在请求的URL后面. ...

  10. (转载)我的java问题排查工具单

    原文地址:https://yq.aliyun.com/articles/69520 我的问题排查工具箱 前言 平时的工作中经常碰到很多疑难问题的处理,在解决问题的同时,有一些工具起到了相当大的作用,在 ...