Vases and Flowers

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 347    Accepted Submission(s): 108

Problem Description
Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to N-1. When she receive some flowers, she will try to put them in the vases, one flower in one vase. She randomly choose the vase A and try to put a flower in the vase. If the there is no flower in the vase, she will put a flower in it, otherwise she skip this vase. And then she will try put in the vase A+1, A+2, ..., N-1, until there is no flower left or she has tried the vase N-1. The left flowers will be discarded. Of course, sometimes she will clean the vases. Because there are too many vases, she randomly choose to clean the vases numbered from A to B(A <= B). The flowers in the cleaned vases will be discarded.
 
Input
The first line contains an integer T, indicating the number of test cases.

For each test case, the first line contains two integers N(1 < N < 50001) and M(1 < M < 50001). N is the number of vases, and M is the operations of Alice. Each of the next M lines contains three integers. The first integer of one line is K(1 or 2). If K is 1, then two integers A and F follow. It means Alice receive F flowers and try to put a flower in the vase A first. If K is 2, then two integers A and B follow. It means the owner would like to clean the vases numbered from A to B(A <= B).
 
Output
For each operation of which K is 1, output the position of the vase in which Alice put the first flower and last one, separated by a blank. If she can not put any one, then output 'Can not put any one.'. For each operation of which K is 2, output the number of discarded flowers.
 


Output one blank line after each test case.

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

思路:很基本的线段树问题,我们可以用线段树维护每一个区间的剩余空间数量(也就是可以插画的地方),对于第一个询问,我们计算区间[a,n-1]所剩余的空间数,假设为num,若为0,则输出 “Can not put any one.”,否则若小于F,则将F设为num,接下来可以二分区间[a,mid]来确定插入的第一只花和最后一只花的位置l,r,然后将区间[l,r]全赋值为1即可,对于询问2,则计算出区间[a,b]所剩余的空间num,然后 b-a+1-num即为答案。最后将区间[a,b]赋值为1.以上,下面是代码.

  1. #include <iostream>
  2. #include <string.h>
  3. #include <algorithm>
  4. #include <stdio.h>
  5. #define maxn 50010
  6. #define mid ((t[p].l+t[p].r)>>1)
  7. #define ls (p<<1)
  8. #define rs (ls|1)
  9. using namespace std;
  10. struct tree
  11. {
  12. int l,r;
  13. int sum;
  14. int lazy;
  15. }t[maxn<<2];
  16. void pushup(int p)
  17. {
  18. t[p].sum=t[ls].sum+t[rs].sum;
  19. }
  20. void pushdown(int p)
  21. {
  22. if(t[p].lazy==0)
  23. {
  24. t[ls].lazy=0;
  25. t[rs].lazy=0;
  26. t[ls].sum=0;
  27. t[rs].sum=0;
  28. t[p].lazy=-1;
  29. }
  30. else if(t[p].lazy==1)
  31. {
  32. t[ls].lazy=1;
  33. t[rs].lazy=1;
  34. t[ls].sum=t[ls].r-t[ls].l+1;
  35. t[rs].sum=t[rs].r-t[rs].l+1;
  36. t[p].lazy=-1;
  37. }
  38. }
  39. void build(int p,int l,int r)
  40. {
  41. t[p].l=l,t[p].r=r,t[p].sum=1,t[p].lazy=-1;
  42. if(l==r)
  43. return;
  44. build(ls,l,mid);
  45. build(rs,mid+1,r);
  46. pushup(p);
  47. }
  48. void change(int p,int l,int r,int val)
  49. {
  50. if(t[p].l==l&&t[p].r==r)
  51. {
  52. t[p].lazy=val;
  53. if(val)
  54. {
  55. t[p].sum=r-l+1;
  56. }
  57. else
  58. t[p].sum=0;
  59. return;
  60. }
  61. pushdown(p);
  62. if(l>mid)
  63. change(rs,l,r,val);
  64. else if(r<=mid)
  65. change(ls,l,r,val);
  66. else
  67. {
  68. change(ls,l,mid,val);
  69. change(rs,mid+1,r,val);
  70. }
  71. pushup(p);
  72. }
  73. int query(int p,int l,int r)
  74. {
  75. if(t[p].l==l&&t[p].r==r)
  76. {
  77. return t[p].sum;
  78. }
  79. pushdown(p);
  80. if(l>mid)
  81. return query(rs,l,r);
  82. else if(r<=mid)
  83. return query(ls,l,r);
  84. else
  85. return query(ls,l,mid)+query(rs,mid+1,r);
  86. }
  87. int main()
  88. {
  89. //freopen("dd.txt","r",stdin);
  90. int ncase;
  91. scanf("%d",&ncase);
  92. while(ncase--)
  93. {
  94. int n,m,i;
  95. scanf("%d%d",&n,&m);
  96. build(1,0,n-1);
  97. int k,a,b;
  98. for(i=1;i<=m;i++)
  99. {
  100. scanf("%d%d%d",&k,&a,&b);
  101. if(k==1)
  102. {
  103. int ttmp=query(1,a,n-1);
  104. if(ttmp==0)
  105. {
  106. printf("Can not put any one.\n");
  107. }
  108. else
  109. {
  110. if(ttmp<b)
  111. b=ttmp;
  112. int mi=a,ma=n-1,Mid;
  113. int l,r;
  114. while(mi<=ma)
  115. {
  116. Mid=(mi+ma)>>1;
  117. if(query(1,a,Mid)>0)
  118. {
  119. l=Mid;
  120. ma=Mid-1;
  121. }
  122. else
  123. mi=Mid+1;
  124. }
  125. mi=a,ma=n-1;
  126. while(mi<=ma)
  127. {
  128. Mid=(mi+ma)>>1;
  129. if(query(1,a,Mid)>=b)
  130. {
  131. r=Mid;
  132. ma=Mid-1;
  133. }
  134. else
  135. mi=Mid+1;
  136. }
  137. change(1,l,r,0);
  138. printf("%d %d\n",l,r);
  139. }
  140. }
  141. else
  142. {
  143. printf("%d\n",b-a+1-query(1,a,b));
  144. change(1,a,b,1);
  145. }
  146.  
  147. }
  148. printf("\n");
  149. }
  150. return 0;
  151. }

2013 多校联合2 D Vases and Flowers (hdu 4614)的更多相关文章

  1. 2013 多校联合 F Magic Ball Game (hdu 4605)

    http://acm.hdu.edu.cn/showproblem.php?pid=4605 Magic Ball Game Time Limit: 10000/5000 MS (Java/Other ...

  2. L - Vases and Flowers - hdu 4614(区间操作)

    题意:有两种操作,第一种从A开始插花,如果有花就跳到下一个,然后输出最后一个花瓶的编号,如果花瓶不够把多余的花丢掉.操作2把区间清空 分析:很明显的线段树操作,就是插花的时候麻烦一下,需要先找出来他剩 ...

  3. L - Vases and Flowers HDU - 4614 线段树+二分

    题意 给出一排空花瓶 有两种操作  1是 从A花瓶开始放F朵花 如果当前瓶有花就跳过前往下一个 直到花用完或者 瓶子到了最后一个为止 输出 成功放花的第一个和最后一个  如果没有输出 can not. ...

  4. 2013多校联合2 I Warm up 2(hdu 4619)

    Warm up 2 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total ...

  5. 2013 多校联合 2 A Balls Rearrangement (hdu 4611)

    Balls Rearrangement Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Othe ...

  6. 2013多校联合3 G The Unsolvable Problem(hdu 4627)

    2013-07-30 20:35 388人阅读 评论(0) 收藏 举报 http://acm.hdu.edu.cn/showproblem.php?pid=4627 The Unsolvable Pr ...

  7. HDU 4614 Vases and Flowers (2013多校2 1004 线段树)

    Vases and Flowers Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others ...

  8. 2016暑假多校联合---Rikka with Sequence (线段树)

    2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...

  9. 2016暑假多校联合---Windows 10

    2016暑假多校联合---Windows 10(HDU:5802) Problem Description Long long ago, there was an old monk living on ...

随机推荐

  1. 创建局域网内远程git仓库,并将本地仓库push推到远程仓库中

    转载请注明出处 http://www.goteny.com/articles/2014/06/136.html http://www.cnblogs.com/zjjne/p/3778640.html ...

  2. C51 库函数(3)

    3.3 STRING.H:串函数 串函数通常将指针串作输入值.一个串就包括2个或多个字符.串结以空字符表示.在函数memcmp,memcpy,memchr,memccpy,memmove和memset ...

  3. MFC浅析(4) CObject浅析

    MFC CObject浅析 1.CObject简要声明 2.CRuntimeClass结构 3.RUNTIME_CLASS 4.DYNAMIC支持 5.DYNCREATE支持 6.SERIAL支持 C ...

  4. LD1-K(求差值最小的生成树)

    题目链接 /* *题目大意: *一个简单图,n个点,m条边; *要求一颗生成树,使得其最大边与最小边的差值是所有生成树中最小的,输出最小的那个差值; *算法分析: *枚举最小边,用kruskal求生成 ...

  5. Linux操作系统以及各大发行版介绍——Linux operating system and major distribution is introduced

    什么是Linux? 也许很多人会不屑的说,Linux不就是个操作系统么.错!Linux不是一个操作系统,严格来讲,Linux只是一个操作系统中的内核.内核是什么?内核建立了计算机软件与硬件之间通讯的平 ...

  6. VLC网页插件添加对火狐浏览器的支持

    原文转自:http://blog.csdn.net/gsls200808/article/details/25536113 1.用<embed>标签 下面这段代码只支持火狐,不支持IE & ...

  7. 完整版的OpenLDAP搭建全过程

    总结:          先写总结,再写正文,嘿嘿嘿.这还是第一次认真的写个文档,写个总结,哈哈.大概在一个月前,第一次听说这个东西,完全没有概念,刚开始的时候看理论的知识,看了几次之后就没看了,看不 ...

  8. lamp安装指南(转)

    主要软件包, 1. httpd-2.2.6.tar.gz 2. mysql-5.0.45-linux-i686-glibc23.tar.gz ( 这个版本是已编译好的压缩包,解压后稍做配置即可使用 ) ...

  9. 1629 - Cake slicing(DP)

    花了近2个小时终于AC,好爽.. 一道类似于最优矩阵链乘的题目,受<切木棍>那道题的启示,该题的原理也是一样的,仅仅只是变成了且面积.那么对应的也要添加维度 . 显然要完整的表示状态,最少 ...

  10. 【iOS知识学习】_iOS沙盒机制

    IOS中的沙盒机制(SandBox)是一种安全体系,它规定了应用程序仅仅能在为该应用创建的目录内读取文件,不能够訪问其它地方的内容.全部的非代码文件都保存在这个地方.比方图片.声音.属性列表和文本文件 ...