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.

题意:有 0~n-1 共 n 个花瓶,一个花瓶可以插一束花,现在有两个操作,一个是从 A 号花瓶开始插 k 束花,若花瓶有花就顺延到下一个花瓶,直到插完 k 束花或插完 n-1 号花瓶,问她插的第一个和最后一个花瓶编号。第二个操作是清空一段区间的花,问共丢掉多少花。

线段树保存区间空瓶数、首个空瓶和末个空瓶。树上二分查找 k 个空瓶,并更新最大最小空瓶位置。区间清除。

 #include<stdio.h>
#include<string.h>
const int maxm=;
const int INF=0x3f3f3f3f; int st[maxm<<],ma[maxm<<],mi[maxm<<];
int ch[maxm<<];
int maxx,minn,k; inline int max(int a,int b){return a>b?a:b;}
inline int min(int a,int b){return a<b?a:b;} void build(int o,int l,int r){
ma[o]=r;
mi[o]=l;
st[o]=r-l+;
ch[o]=;
if(l==r)return;
int m=l+((r-l)>>);
build(o<<,l,m);
build(o<<|,m+,r);
} void pushdown(int o,int l,int r){
if(ch[o]==){
ch[o<<]=ch[o<<|]=;
int m=l+((r-l)>>);
st[o<<]=st[o<<|]=;
ma[o<<]=ma[o<<|]=-;
mi[o<<]=mi[o<<|]=INF;
ch[o]=;
}
else if(ch[o]==){
ch[o<<]=ch[o<<|]=;
int m=l+((r-l)>>);
st[o<<]=m-l+;
st[o<<|]=r-m;
ma[o<<]=m;
ma[o<<|]=r;
mi[o<<]=l;
mi[o<<|]=m+;
ch[o]=;
}
} void update1(int o,int l,int r,int ql,int qr){
if(ql<=l&&qr>=r){
if(k>=st[o]){
k-=st[o];
maxx=max(maxx,ma[o]);
minn=min(minn,mi[o]);
ma[o]=-;
mi[o]=INF;
st[o]=;
ch[o]=;
return;
}
else if(l==r)return;
}
pushdown(o,l,r);
int m=l+((r-l)>>);
if(ql<=m&&k)update1(o<<,l,m,ql,qr);
if(qr>=m+&&k)update1(o<<|,m+,r,ql,qr);
ma[o]=max(ma[o<<],ma[o<<|]);
mi[o]=min(mi[o<<],mi[o<<|]);
st[o]=st[o<<]+st[o<<|];
} int update2(int o,int l,int r,int ql,int qr){
if(ql<=l&&qr>=r){
int ans=r-l+-st[o];
st[o]=r-l+;
ma[o]=r;
mi[o]=l;
ch[o]=;
return ans;
}
pushdown(o,l,r);
int m=l+((r-l)>>);
int ans=;
if(ql<=m)ans+=update2(o<<,l,m,ql,qr);
if(qr>=m+)ans+=update2(o<<|,m+,r,ql,qr);
ma[o]=max(ma[o<<],ma[o<<|]);
mi[o]=min(mi[o<<],mi[o<<|]);
st[o]=st[o<<]+st[o<<|];
return ans;
} int main(){
int T;
scanf("%d",&T);
while(T--){
int n,m;
scanf("%d%d",&n,&m);
build(,,n-);
for(int i=;i<=m;++i){
int t;
scanf("%d",&t);
if(t==){
int a;
scanf("%d%d",&a,&k);
maxx=-;
minn=INF;
update1(,,n-,a,n-);
if(maxx==-&&minn==INF)printf("Can not put any one.\n");
else printf("%d %d\n",minn,maxx);
}
else if(t==){
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",update2(,,n-,a,b));
}
}
printf("\n");
}
return ;
}

hdu4614 Vases and Flowers 线段树的更多相关文章

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

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

  2. hdu4614 Vases and Flowers 线段树+二分

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4614 题意: 给你N个花瓶,编号是0  到 N - 1 ,初始状态花瓶是空的,每个花瓶最多插一朵花. ...

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

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

  4. hdu 4614 Vases and Flowers 线段树

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

  5. hdu4614 Vases and Flowers【线段树】【二分】

    Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to N ...

  6. HDU-4614 Vases and Flowers(线段树区间更新+二分查找)

    http://acm.hdu.edu.cn/showproblem.php?pid=4614 Time Limit: 4000/2000 MS (Java/Others)    Memory Limi ...

  7. HDU-4614 Vases and Flowers (线段树区间更新)

    题目大意:有n个花瓶,每个花瓶中只能放一朵花.两种操作,一种是从A开始放F朵花,如果有的花瓶中已经有花则跳过这个花瓶,往下一个花瓶放:第二种是将区间[A,B]之间花瓶中的花清空.如果是第一种操作,输出 ...

  8. HDU4614 Vases and Flowers 二分+线段树

    分析:感觉一看就是二分+线段树,没啥好想的,唯一注意,当开始摆花时,注意和最多能放的比大小 #include<iostream> #include<cmath> #includ ...

  9. HDU4614 Vases and Flowers

    http://acm.hdu.edu.cn/showproblem.php?pid=4614 HDU 4614 Vases and Flowers (2013多校第二场线段树) // #pragma ...

随机推荐

  1. shell 文件条件判断

    按照文件类型进行判断 '-b 文件' 判断该文件是否存在,并且是否为块设备文件(是块设备文件为真) '-c 文件' 判断该文件是否存在,并且是否为字符设备文件(是字符设备文件为真) '-d 文件' 判 ...

  2. go中for循环使用多个变量避坑

    go for循环语法为: for expression1, expression2, expression3 { // ... } 使用多个变量时,使用平行赋值,需要留意的是expression3处的 ...

  3. hdu3377

    题解: 简单的插头dp 加上一个代价即可 代码: #include<cstdio> #include<cmath> #include<cstring> #inclu ...

  4. day02 运算符和编码

    今日所学 主要是运算符和编码的初认识, 1   还有比较运算 ==,!=,<>,>,<,>=,<=等 2  .  赋值运算 =,+=,-=等 还有今天的难点逻辑运算 ...

  5. 根据访问ip的地区跳转到指定地址

    <script type="text/javascript" src="http://ip.ws.126.net/ipquery"></scr ...

  6. 5.10 C++内存管理操作符重载

    参考:http://www.weixueyuan.net/view/6388.html 注意: 内存管理操作符new.new[].delete和delete[]同样也可以进行操作符重载,其重载形式既可 ...

  7. SQL-31 获取select * from employees对应的执行计划

    题目描述 获取select * from employees对应的执行计划 explain select * from employees explain  用于获得表的所有细节

  8. 关于macroblaze的一些理解(更新中)

    (1)添加*.elf文件: 在Design Sources工作目录中右键选择添加源文件,找到SDK目录中对应的文件夹下的Debug内*.elf文件,将其添加.然后,源文件目录更新,多出一个ELF文件夹 ...

  9. android 自定义命名空间 http://schemas.android.com/apk/res-auto

    XML中用 xmlns="http://schemas.android.com/apk/res-auto" 获取自定义属性值: public static String NAMES ...

  10. nginx配置文服

    修改nginx.conf 添加如下内容 autoindex on; # 显示目录 autoindex_exact_size on; # 显示文件大小 autoindex_localtime on; # ...