讲道理我之前暂时跳过染色的题是因为总觉得有什么很高端的算法来query

直到我做了F题(ZOJ 1610)才发现就是个暴力统计.....也对 也就几万个长度单位而已....

F就不po上来了 选了有点难度的D题 需要用到离散化

单纯的离散化又会碰到后染色覆盖前染色的 所以要在每两个差距大于1的位置之间插入一个位置

以上思路参考了 http://blog.csdn.net/non_cease/article/details/7383736 但是原po的有点小错 混过了POJ的数据但是确实是错的...

就是说底色可能会被不小心算进去 所以统计的时候要忽略底色

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #define INF 0x3f3f3f3f
  5. #define mem(str,x) memset(str,(x),sizeof(str))
  6. #define lson l, m, rt<<1
  7. #define rson m+1, r, rt<<1|1
  8. using namespace std;
  9. typedef long long LL;
  10.  
  11. const int MAXN = ;
  12. int n, hash[MAXN<<], col[MAXN<<];
  13. int ans, li[MAXN], ri[MAXN];
  14. bool vis[MAXN];
  15.  
  16. inline void push_down(int rt){
  17. if(~col[rt]){
  18. col[rt<<] = col[rt<<|] = col[rt];
  19. col[rt] = -;
  20. }
  21. }
  22.  
  23. void update(int L, int R, int c, int l, int r, int rt)
  24. {
  25. if(L <= l && R >= r){
  26. col[rt] = c;
  27. //printf("%d - %d : %d\n", l, r, c);
  28. return;
  29. }
  30.  
  31. push_down(rt);
  32. int m = (l + r) >> ;
  33. if(L <= m) update(L, R, c, lson);
  34. if(R > m) update(L, R, c, rson);
  35. }
  36.  
  37. void query(int l, int r, int rt)
  38. {
  39. if(l == r){
  40. if(!vis[col[rt]] && ~col[rt]){
  41. //printf("%d at %d\n", col[rt], l);
  42. ans++;
  43. vis[col[rt]] = true;
  44. }
  45. return;
  46. }
  47.  
  48. push_down(rt);
  49. int m = (l + r) >> ;
  50. query(lson);
  51. query(rson);
  52. }
  53.  
  54. int binary_search(int l, int r, int c)
  55. {
  56. int m;
  57. while(l <= r){
  58. m = (l + r) >> ;
  59. if(hash[m] == c) return m;
  60. else if(hash[m] > c) r = m - ;
  61. else l = m + ;
  62. }
  63. return -;
  64. }
  65.  
  66. int main()
  67. {
  68. int t;
  69. scanf("%d", &t);
  70. while(t--){
  71. mem(col, -);
  72. mem(vis, false);
  73. scanf("%d", &n);
  74. int nn = , mm = ;
  75. for(int i = ; i <= n; i++){
  76. scanf("%d%d", &li[i], &ri[i]);
  77. hash[++nn] = li[i];
  78. hash[++nn] = ri[i];
  79. }
  80. sort(hash+, hash++nn);
  81. //去除重复数据
  82. for(int i = ; i <= nn; i++){
  83. if(hash[i] != hash[i-]) hash[++mm] = hash[i];
  84. }
  85. //在相距超过1的点之间插入一个点防止覆盖
  86. for(int i = mm; i > ; i--){
  87. if(hash[i] - hash[i-] > ) hash[++mm] = hash[i] - ;
  88. }
  89. sort(hash+, hash++mm);
  90. //离散化构造线段树
  91. for(int i = ; i <= n; i++){
  92. int l = binary_search(, mm, li[i]);
  93. int r = binary_search(, mm, ri[i]);
  94. update(l, r, i, , mm, );
  95. //printf("%d - %d : %d\n", l, r, i);
  96. }
  97. ans = ;
  98. query(, mm, );
  99. printf("%d\n", ans);
  100. }
  101. return ;
  102. }

kuangbin_SegTree D (POJ 2528)的更多相关文章

  1. POJ 2528 Mayor's posters

    Mayor's posters Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  2. poj 2528 Mayor's posters(线段树+离散化)

    /* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...

  3. POJ 2528 Mayor's posters(线段树区间染色+离散化或倒序更新)

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 59239   Accepted: 17157 ...

  4. poj 2528 Mayor's posters 线段树+离散化技巧

    poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...

  5. poj 2528 (线段树+离散化)

    poj 2528 For each input data set print the number of visible posters after all the posters are place ...

  6. POJ - 2528 Mayor's posters(dfs+分治)

    POJ - 2528 Mayor's posters 思路:分治思想. 代码: #include<iostream> #include<cstdio> #include< ...

  7. POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)

    POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...

  8. POJ 2528——Mayor's posters——————【线段树区间替换、找存在的不同区间】

    Mayor's posters Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  9. poj 2528 线段树区间修改+离散化

    Mayor's posters POJ 2528 传送门 线段树区间修改加离散化 #include <cstdio> #include <iostream> #include ...

随机推荐

  1. hibernate整合spring开发的时候遇到的一些小问题

    1 在spring整合hibernate开发的时候,在数据源里面配置show_sql为true,但是在实际查询的时候并没有打印sql语句,正确的解决方案为: 把<prop key="s ...

  2. WC项目要求

    #include "stdio.h" #include "string.h" #include "stdlib.h" int charcal ...

  3. 每天一点 js join 函数

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  4. bak骗子公司

    李波 身份证:310101197510313215 手机:13916407777 18621624812 13916821206住址:上海QQ:87766938 沪EE5781 奥迪Q7李寻欢77 s ...

  5. java 文件压缩和解压(ZipInputStream, ZipOutputStream)

    最近在看java se 的IO 部分 , 看到 java 的文件的压缩和解压比较有意思,主要用到了两个IO流-ZipInputStream, ZipOutputStream,不仅可以对文件进行压缩,还 ...

  6. 把一个英语句子中的单词次序颠倒后输出。例如输入“how are you”,输出“you are how”;

    import java.util.Scanner; public class Test2 { public void reverse(String str) { String[] wordArray ...

  7. css 笔记

    外边距合并 当一个元素出现在另一个元素的上面时,第一个元素的下外边距和第二个元素的上外边距会产生合并,两个盒子之间的上下间距为大的数值. 当一个子元素包含在另外一个父元素(假设没有内边距 没有边框), ...

  8. php 画图片2

    <?php // 使用php操作gd库做图 // 1. 创建一个画布资源 $im = imagecreatetruecolor(200, 50); // 2. 创建背景色 // 2.1 得到背景 ...

  9. 转一下关于struct字对齐的原因

    作者:張道遠链接:https://www.zhihu.com/question/27862634/answer/38506197来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  10. php 队列

    一.php中对共享内存,消息队列的操作 http://blog.csdn.net/haitun312366/article/details/8614797 二.PHP memcache 队列类 htt ...