hdu 4614线段树+二分Vases and Flowers
来源:互联网 发布:不同后缀域名的区别 编辑:程序博客网 时间:2024/06/05 10:16
Vases and Flowers
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 3340 Accepted Submission(s): 1337
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).
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.
Output one blank line after each test case.
Sample Input
210 51 3 52 4 51 1 82 3 61 8 810 61 2 52 3 41 0 82 2 51 4 41 2 3
Sample Output
[pre]3 721 94Can not put any one.2 620 944 52 3题意就是给一个编号为0~N-1的花瓶,给一系列操作:1、从起始点S开始插入F朵花,插入时连续地从左到右扫描过去若花瓶有空且花没用完就一定要插到花瓶里,最后要么花用完了要么没位置可以放置了;2、清空编号从L到R花瓶。
这题第二问明显可以用线段树做,而且由于前缀和的缘故,花的前缀和数量在编号序列上具有单调性,因此第一问可以用二分做,但是二分开头简单,二分结尾就比较恶心,调试了很久,仔细考虑可以发现会有这样一个容易错误的地方:若从头到尾空位比花少,则终点为第一个达到最大空位的位置,而不是一直到结尾,因此要仅当当前位置的前缀空位和等于最大空位数的时候才记录终点T。如果二分结尾不正确估计第二组样例会对不上
#define maxn 100001using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1int sum[maxn<<2],lazy[maxn<<2];void pushup(int rt){ sum[rt]=sum[rt<<1]+sum[rt<<1|1]; return;}void pushdown(int rt,int len){ if(lazy[rt]>=0) { lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt]; sum[rt<<1]=(len-(len>>1))*lazy[rt]; sum[rt<<1|1]=(len>>1)*lazy[rt]; lazy[rt]=-1; } return;}void update(int L,int R,int c,int l,int r,int rt){ if(L<=l&&R>=r) { lazy[rt]=c; sum[rt]=(r-l+1)*c; return; } pushdown(rt,r-l+1); int m=(l+r)>>1; if(L<=m)update(L,R,c,lson); if(R>m)update(L,R,c,rson); pushup(rt); return;}void build(int l,int r,int rt){ lazy[rt]=-1; if(l==r){sum[rt]=0;return;} int m=(l+r)>>1; build(lson);build(rson); pushup(rt);}long long query(int L,int R,int l,int r,int rt){ if(L<=l&&R>=r)return sum[rt]; long long ans=0; pushdown(rt,r-l+1); int m=(l+r)>>1; if(L<=m)ans+=query(L,R,lson); if(R>m)ans+=query(L,R,rson); return ans;}int main(){ int T; scanf("%d",&T); while(T--) { memset(sum,0,sizeof(sum)); memset(lazy,0,sizeof(lazy)); int len,q; scanf("%d%d",&len,&q); build(1,len,1); int k,a,b; while(q--) { scanf("%d%d%d",&k,&a,&b); a++;b++; if(k==1) { b--; int t; if((t=query(a,len,1,len,1))==len-a+1) { puts("Can not put any one."); continue; } int l=a,r=len,mid; b=min(b,len-a+1-t); while(l<r) { mid=(l+r)>>1; if(query(a,mid,1,len,1)==mid-a+1)l=mid+1; else r=mid; } printf("%d ",l-1); l=a;r=len; while(l<r) { mid=(l+r)>>1; if((mid-a+1-query(a,mid,1,len,1))>=b)r=mid; else l=mid+1; } printf("%d\n",r-1); update(a,l,1,1,len,1); } else { printf("%d\n",query(a,b,1,len,1)); update(a,b,0,1,len,1); } } puts(""); }}
0 0
- hdu 4614线段树+二分Vases and Flowers
- hdu 4614 Vases and Flowers(线段树+二分)
- hdu 4614 Vases and Flowers (线段树+二分)
- hdu 4614 Vases and Flowers (二分 线段树)
- hdu 4614 Vases and Flowers 线段树+二分
- HDU 4614 Vases and Flowers(线段树+二分)
- HDU 4614 Vases and Flowers [二分 + 线段树]
- HDU - 4614 Vases and Flowers(线段树 区间修改 二分)
- HDU 4614 Vases and Flowers(线段树、二分)
- HDU 4614 Vases and Flowers (线段树 + 二分)
- 【解题报告】HDU-4614 Vases and Flowers 线段树+二分
- hdu 4614 Vases and Flowers(线段树+二分)
- HDU-4614:Vases and Flowers(线段树+二分)
- HDU-4614 Vases and Flowers(线段树+区间修改+二分)
- Hdu 4614 Vases and Flowers【二分+线段树】
- HDU 4614 Vases and Flowers——线段树+二分
- HDU-4614 Vases and Flowers (线段树+二分)
- hdu 4614 Vases and Flowers 线段树
- RactiveJS-05 Conditional sections
- 团体程序设计天梯赛-练习集L1-001
- RactiveJS-06 List sections
- Android扫雷:JSON 解析java.lang.IllegalStateException: Expected a name but was NUMBER 原因及解决方案
- 操作系统之抽烟者问题
- hdu 4614线段树+二分Vases and Flowers
- 杭电oj(java版)——1052 Tian Ji -- The Horse Racing
- 名企笔试:4399游戏2015校园(计算天数) 2017-04-01 算法爱好者
- RactiveJS-07 Triples (embedded HTML)
- 一个游戏程序员的代码书写观(一)
- BZOJ2436: [Noi2011]Noi嘉年华
- 设置Button的边框线的方法
- NEW RDSP MODE I (模拟,循环节)
- RactiveJS-08 Extending Ractive