附动态线段树AC代码

http://acm.hdu.edu.cn/showproblem.php?pid=1199

因为昨天做了一道动态线段树的缘故,今天遇到了这题没有限制范围的题就自然而然想到了动态线段树的解法,写完看题解发现原来只要离散化就好了(干。。),总结了一下这题和昨天hdu5367的区别在于,虽然都是两题范围超级大的线段树,但是昨天的强制要求在线求解,只能选择空间复杂度更大一些的动态线段树来求解,而今天的这题可以选择离线操作,因而可以采用先读入所有输入,离散化之后建树的方法来操作。下次遇到这样的题还是应当优先考虑离散化。

在一个全部涂黑色的条子上涂上一些白色或黑色的片段,问最大白色片段。

仅仅从线段树维护节点的角度上来看很简单,维护最大白色片段,左边最大白色片段,右边最大白色片段就好了。

由于条子的长度长达1-INT_MAX;

采用离散化,或者像我一样失了智去用动态线段树的方法,也能AC

  1. #include <map>
  2. #include <set>
  3. #include <cmath>
  4. #include <queue>
  5. #include <stack>
  6. #include <vector>
  7. #include <string>
  8. #include <cstdio>
  9. #include <cstdlib>
  10. #include <cstring>
  11. #include <sstream>
  12. #include <iostream>
  13. #include <algorithm>
  14. #include <functional>
  15. #define For(i, x, y) for(int i=x; i<=y; i++)
  16. #define _For(i, x, y) for(int i=x; i>=y; i--)
  17. #define Mem(f, x) memset(f, x, sizeof(f))
  18. #define Sca(x) scanf("%d", &x)
  19. #define Scl(x) scanf("%lld",&x);
  20. #define Pri(x) printf("%d\n", x)
  21. #define Prl(x) printf("%lld\n",x);
  22. #define CLR(u) for(int i = 0; i <= N ; i ++) u[i].clear();
  23. #define LL long long
  24. #define ULL unsigned long long
  25. #define mp make_pair
  26. #define PII pair<int,int>
  27. #define PIL pair<int,long long>
  28. #define PLL pair<long long,long long>
  29. #define pb push_back
  30. #define fi first
  31. #define se second
  32. using namespace std;
  33. typedef vector<int> VI;
  34. const double eps = 1e-;
  35. const int maxn = ;
  36. const int INF = 0x3f3f3f3f;
  37. const int mod = 1e9 + ;
  38. inline int read()
  39. {
  40. int now=;register char c=getchar();
  41. for(;!isdigit(c);c=getchar());
  42. for(;isdigit(c);now=now*+c-'',c=getchar());
  43. return now;
  44. }
  45. int N,M;
  46. struct Tree{
  47. LL sum; //最大连续
  48. LL lsum; //左连续
  49. LL rsum; //右连续
  50. int lt;
  51. int rt;
  52. int lazy;
  53. void init(){
  54. lsum = rsum = sum = lt = rt = ;
  55. lazy = -;
  56. }
  57. }tree[maxn * ];
  58. int tot;
  59. void check(int &t){
  60. if(t) return;
  61. t = ++tot;
  62. tree[t].init();
  63. }
  64. void add(int &t,LL L,LL R,int v){
  65. if(v){
  66. tree[t].sum = tree[t].lsum = tree[t].rsum = R - L + ;
  67. }else{
  68. tree[t].sum = tree[t].lsum = tree[t].rsum = ;
  69. }
  70. tree[t].lazy = v;
  71. }
  72. void Pushdown(int& t,LL L,LL R){
  73. if(tree[t].lazy == -) return;
  74. int &lt = tree[t].lt; int &rt = tree[t].rt;
  75. LL M = (L + R) >> ;
  76. check(lt); check(rt);
  77. add(lt,L,M,tree[t].lazy);
  78. add(rt,M + ,R,tree[t].lazy);
  79. tree[t].lazy = -;
  80. }
  81. void Pushup(int t,LL L,LL R){
  82. int &ls = tree[t].lt; int &rs = tree[t].rt;
  83. LL M = (L + R) >> ;
  84. check(ls); check(rs);
  85. tree[t].sum = max(tree[ls].sum,tree[rs].sum);
  86. tree[t].sum = max(tree[t].sum,tree[ls].rsum + tree[rs].lsum);
  87. tree[t].lsum = tree[ls].lsum;
  88. if(tree[ls].lsum == M - L + ){
  89. tree[t].lsum = tree[ls].lsum + tree[rs].lsum;
  90. }
  91. tree[t].rsum = tree[rs].rsum;
  92. if(tree[rs].rsum == R - M){
  93. tree[t].rsum = tree[rs].rsum + tree[ls].rsum;
  94. }
  95. }
  96. void update(int &t,int q1,int q2,LL L,LL R,int v){
  97. check(t);
  98. if(q1 <= L && R <= q2){
  99. add(t,L,R,v);
  100. return;
  101. }
  102. Pushdown(t,L,R);
  103. LL m = (L + R) >> ;
  104. if(q1 <= m) update(tree[t].lt,q1,q2,L,m,v);
  105. if(q2 > m) update(tree[t].rt,q1,q2,m + ,R,v);
  106. Pushup(t,L,R);
  107. }
  108. LL Left,Right;
  109. void query(int t,LL L,LL R){
  110. if(L == R){
  111. Left = L;
  112. Right = R;
  113. return;
  114. }
  115. check(tree[t].lt); check(tree[t].rt);
  116. int ls = tree[t].lt; int rs = tree[t].rt;
  117. LL M = (L + R) >> ;
  118. if(tree[ls].sum == tree[t].sum) query(ls,L,M);
  119. else if(tree[rs].sum == tree[t].sum) query(rs,M + ,R);
  120. else{
  121. Left = M - tree[ls].rsum + ;
  122. Right = M + tree[rs].lsum;
  123. return;
  124. }
  125.  
  126. }
  127. int main()
  128. {
  129. while(~Sca(N)){
  130. LL L = ; LL R = ;
  131. tot = ;
  132. int root = ;
  133. update(root,L,R,L,R,);
  134. For(i,,N){
  135. LL l,r;
  136. char op[];
  137. scanf("%lld%lld%s",&l,&r,&op);
  138. if(op[] == 'w'){
  139. update(root,l,r,L,R,);
  140. }else{
  141. update(root,l,r,L,R,);
  142. }
  143. }
  144. if(!tree[root].sum){
  145. puts("Oh, my god");
  146. continue;
  147. }
  148. query(root,L,R);
  149. printf("%lld %lld\n",Left,Right);
  150. }
  151. return ;
  152. }

HDU1199 动态线段树 // 离散化的更多相关文章

  1. POJ 2528 Mayor's posters(线段树+离散化)

    Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...

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

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

  3. [poj2528] Mayor's posters (线段树+离散化)

    线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...

  4. [UESTC1059]秋实大哥与小朋友(线段树, 离散化)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1059 普通线段树+离散化,关键是……离散化后建树和查询都要按照基本法!!!RE了不知道多少次………………我真 ...

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

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

  6. BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针

    BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针 Description 在数轴上有 n个闭区间 [l1,r1],[l2,r2],...,[ln,rn].现在要从中选出 m 个区间, ...

  7. D - Mayor's posters(线段树+离散化)

    题目: The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campai ...

  8. 主席树||可持久化线段树+离散化 || 莫队+分块 ||BZOJ 3585: mex || Luogu P4137 Rmq Problem / mex

    题面:Rmq Problem / mex 题解: 先离散化,然后插一堆空白,大体就是如果(对于以a.data<b.data排序后的A)A[i-1].data+1!=A[i].data,则插一个空 ...

  9. HDU5367 思维map // 动态线段树

    地主毛毛有n座山,这些山在地主家门前排成一条直线.这些山一开始均有相同的高度.  每一天,毛毛都会要求花花开挖机把几座山挖掉一定高度,或者给一些山堆上一些高度.并且要求花花报告现在有多少座山属于“高山 ...

随机推荐

  1. Scrum Meeting day 3

                第三次会议 No_00:工作情况 No_01:任务说明 待完成 已完成 No_10:燃尽图 No_11:照片记录 No_100:代码/文档签入记录

  2. SQL中常用函数

    SELECT CONVERT(varchar(100), GETDATE(), 23) AS  日期   结果:2017-01-05     select ISNULL(price,'0.0')   ...

  3. Repair U Disk logo unvisiable in task bar on windows XP

    Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersio ...

  4. Jmeter 通过json Extracted 来获取 指定的值的id

    在没有 精确或模糊查询的接口时可以使用jmeter 获取指定的值的ID import java.lang.String ; String getTargetName="iphone632g& ...

  5. Centos7搭建LAMP+Typecho博客

    一.安装Apache的httpd服务 yum install httpd # 安装httpd服务 systemctl start httpd # 启动httpd服务 systemctl status ...

  6. [转帖] 打开加密SQLite文件的方法

    Copy From http://blog.csdn.net/sean4m/article/details/50211565 mark 下 正好工作用到了这个东西. 版本:SQLiteExpertPr ...

  7. insertBefore(),appendChild()创建添加列表实例

    定义: insertBefore() 方法在您指定的已有子节点之前插入新的子节点. 语法: 父级.insertBefore(新的子节点,指定的已有子节点) 实例: <input id=" ...

  8. 软件工程_2nd weeks

    本周上课没有板书,都由老师口头叙述 因此有的笔记记得不是很全,幸好有郑蕊师姐发布的课堂笔记,很好的梳理上课的内容~ 1.根据老师上课给的建议,进行了深刻的思考和反思 1.1 作为一个学硕研究生,这门课 ...

  9. Python 2和Python 3的编码问题

    在Python2中,字符串无法完全地支持国际字符集和Unicode编码.为了解决这种限制,Python2对Unicode数据使用了单独的字符串类型.要输入Unicode字符串字面量,要在第一个引号前加 ...

  10. 可视化数据matplotlib之安装与简单折线图

    matplotlib是一个可视化数据的模块,安装前需要先安装Visual Studio Community:然后去https://pypi.python.org/pypi上查找matplotlib并下 ...