题目大意:有n个花瓶,每个花瓶中只能放一朵花。两种操作,一种是从A开始放F朵花,如果有的花瓶中已经有花则跳过这个花瓶,往下一个花瓶放;第二种是将区间[A,B]之间花瓶中的花清空。如果是第一种操作,输出这次放的花的左右端点;如果是第二种操作,输出这次总共清理出了多少支花。

题目分析:建立线段树,节点维护在相应的区间中,没有放入花的花瓶数目。有三种操作:一、查询某个区间中第k个没有插入花的花瓶位置;二、更新区间,使区间全部插入花;三、更新区间,使区间中的花瓶全部清空;

代码如下:

  1. # include<iostream>
  2. # include<cstdio>
  3. # include<queue>
  4. # include<vector>
  5. # include<list>
  6. # include<map>
  7. # include<set>
  8. # include<cstdlib>
  9. # include<string>
  10. # include<cstring>
  11. # include<algorithm>
  12. using namespace std;
  13. # define LL long long
  14.  
  15. const int N=50100;
  16. const int INF=1<<30;
  17. const double oo=1e20;
  18. const double eps=1e-20;
  19.  
  20. int lazy[N*4+5];
  21. int tr[N*4+5];
  22.  
  23. void pushDown(int rt,int l,int r)
  24. {
  25. if(lazy[rt]!=-1){
  26. lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];
  27. if(lazy[rt]==1)
  28. tr[rt<<1]=tr[rt<<1|1]=0;
  29. else{
  30. int mid=l+(r-l)/2;
  31. tr[rt<<1]=mid-l+1;
  32. tr[rt<<1|1]=r-mid;
  33. }
  34. lazy[rt]=-1;
  35. }
  36. }
  37.  
  38. void pushUp(int rt)
  39. {
  40. tr[rt]=tr[rt<<1]+tr[rt<<1|1];
  41. }
  42.  
  43. void build(int rt,int l,int r)
  44. {
  45. tr[rt]=r-l+1;
  46. lazy[rt]=-1;
  47. if(l==r)
  48. return ;
  49. int mid=l+(r-l)/2;
  50. build(rt<<1,l,mid);
  51. build(rt<<1|1,mid+1,r);
  52. }
  53.  
  54. void update(int rt,int l,int r,int L,int R)
  55. {
  56. if(L<=l&&r<=R){
  57. tr[rt]=0;
  58. lazy[rt]=1;
  59. }else{
  60. pushDown(rt,l,r);
  61. int mid=l+(r-l)/2;
  62. if(L<=mid) update(rt<<1,l,mid,L,R);
  63. if(R>mid) update(rt<<1|1,mid+1,r,L,R);
  64. pushUp(rt);
  65. }
  66. }
  67.  
  68. int clear(int rt,int l,int r,int L,int R)
  69. {
  70. if(L<=l&&r<=R){
  71. int temp=tr[rt];
  72. tr[rt]=r-l+1;
  73. lazy[rt]=0;
  74. return tr[rt]-temp;
  75. }else{
  76. pushDown(rt,l,r);
  77. int mid=l+(r-l)/2;
  78. int res=0;
  79. if(L<=mid) res+=clear(rt<<1,l,mid,L,R);
  80. if(R>mid) res+=clear(rt<<1|1,mid+1,r,L,R);
  81. pushUp(rt);
  82. return res;
  83. }
  84. }
  85.  
  86. int query(int rt,int l,int r,int L,int R)
  87. {
  88. if(L>r||R<l) return 0; ///这句话必须加上,否则query可能会一直被调用下去
  89. if(L<=l&&r<=R)
  90. return tr[rt];
  91. pushDown(rt,l,r);
  92. int mid=l+(r-l)/2;
  93. int res=0;
  94. if(L<=mid)
  95. res+=query(rt<<1,l,mid,L,R);
  96. if(R>mid)
  97. res+=query(rt<<1|1,mid+1,r,L,R);
  98. return res;
  99. }
  100.  
  101. int ask(int rt,int l,int r,int L,int R,int num)
  102. {
  103. if(l==r){
  104. return l;
  105. }else{
  106. pushDown(rt,l,r);
  107. int mid=l+(r-l)/2;
  108. int t=query(rt<<1,l,mid,L,R);
  109. if(num<=t) return ask(rt<<1,l,mid,L,R,num);
  110. return ask(rt<<1|1,mid+1,r,L,R,num-t);
  111. }
  112. }
  113.  
  114. int main()
  115. {
  116. int T,n,m;
  117. scanf("%d",&T);
  118. while(T--)
  119. {
  120. scanf("%d%d",&n,&m);
  121. build(1,1,n);
  122. int k,a,b;
  123. while(m--)
  124. {
  125. scanf("%d%d%d",&k,&a,&b);
  126. if(k==1){
  127. int t=query(1,0,n-1,a,n-1);
  128. if(t==0)
  129. printf("Can not put any one.\n");
  130. else{
  131. int l=ask(1,0,n-1,a,n-1,1);
  132. int r=ask(1,0,n-1,a,n-1,min(b,t));
  133. printf("%d %d\n",l,r);
  134. update(1,0,n-1,l,r);
  135. }
  136. }else{
  137. int ans=clear(1,0,n-1,a,min(n-1,b));
  138. printf("%d\n",ans);
  139. }
  140. }
  141. printf("\n");
  142. }
  143. return 0;
  144. }

  

HDU-4614 Vases and Flowers (线段树区间更新)的更多相关文章

  1. HDU 4614 Vases and Flowers(线段树+二分)

    题目链接 比赛的时候一直想用树状数组,但是树状数组区间更新之后,功能有局限性.线段树中的lz标记很强大,这个题的题意也挺纠结的. k = 1时,从a开始,插b个花,输出第一个插的位置,最后一个的位置, ...

  2. HDU-4614 Vases and Flowers 线段树区间更新

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4614 线段树保存区间是否被覆盖以及区间的和即可,在询问的时候在线段树上二分查找就可以了...代码写得比 ...

  3. hdu 4614 Vases and Flowers 线段树

    题目链接 一共n个盒子, 两种操作, 第一种是给出两个数x, y, 从第x个盒子开始放y朵花, 一个盒子只能放一朵, 如果某个盒子已经有了, 那么就跳过这个盒子放下面的盒子. 直到花放完了或者到了最后 ...

  4. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  5. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  6. (简单) HDU 1698 Just a Hook , 线段树+区间更新。

    Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...

  7. HDU 1698 Just a Hook(线段树区间更新查询)

    描述 In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes ...

  8. hdu - 1689 Just a Hook (线段树区间更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=1698 n个数初始每个数的价值为1,接下来有m个更新,每次x,y,z 把x,y区间的数的价值更新为z(1<= ...

  9. 2014多校第四场1006 || HDU 4902 Nice boat (线段树 区间更新)

    题目链接 题意 : 给你n个初值,然后进行两种操作,第一种操作是将(L,R)这一区间上所有的数变成x,第二种操作是将(L,R)这一区间上所有大于x的数a[i]变成gcd(x,a[i]).输出最后n个数 ...

  10. HDU 1698 Just a Hook 线段树区间更新、

    来谈谈自己对延迟标记(lazy标记)的理解吧. lazy标记的主要作用是尽可能的降低时间复杂度. 这样说吧. 如果你不用lazy标记,那么你对于一个区间更新的话是要对其所有的子区间都更新一次,但如果用 ...

随机推荐

  1. [转]ps/2键盘线序识别方法

    from: http://www.360doc.com/content/11/0816/19/844619_140875056.shtml 经常看到有人询问ps/2线坏了,更换的时候如何测线序连线,或 ...

  2. linux卸载挂载点显示device is busy

    在做umount设备时, device is busy是令人头痛的提示: [root@delphi /]# umount /dev/cdrom umount: /mnt/cdrom: device i ...

  3. OpenCV之响应鼠标(二):函数cvSetMouseCallback()和其副程式onMouse()的使用(OpenCV2.4.5)

    每當滑鼠在視訊視窗介面點擊一下的時候,都會有固定三個動作 1.點擊(Click) 2.放開(Down)3.滑動(move) 因此,程式執行滑鼠在點擊的時候onMouse()都會連續跑三次,代表滑鼠在點 ...

  4. js中获取项目路径的小插件

    //立即执行的js (function() { //获取contextPath var contextPath = getContextPath(); //获取basePath var basePat ...

  5. 转:HashMap深度解析(一)

      HashMap哈希码hashCodeequals 本文来自:高爽|Coder,原文地址:http://blog.csdn.net/ghsau/article/details/16843543,转载 ...

  6. python3爬虫再探之EXCEL

    在爬取数据之后,数据的保存就成为一个新的问题,一般不太大的的数据存储到EXCEL就可以了.这里介绍一个python的第三方库——xlsxwriter. 这个库的安装就不介绍了,pip就可以,不用FQ. ...

  7. Eclipse的maven构建一个web项目,以构建SpringMVC项目为例

    http://www.cnblogs.com/javaTest/archive/2012/04/28/2589574.html springmvc demo实例教程源代码下载:http://zuida ...

  8. simple grammer

    <?phpecho strlen("Hello world!"); // outputs 12?> <?phpecho str_word_count(" ...

  9. java作业7

    (1)阅读以下代码(CatchWho.java),写出程序运行结果: (2)写出CatchWho2.java程序运行的结果 (3)请先阅读 EmbedFinally.java示例,再运行它,观察其输出 ...

  10. Excel文件的导出操作

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...