hdu4614 Vases and Flowers 线段树
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 线段树的更多相关文章
- HDU-4614 Vases and Flowers 线段树区间更新
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4614 线段树保存区间是否被覆盖以及区间的和即可,在询问的时候在线段树上二分查找就可以了...代码写得比 ...
- hdu4614 Vases and Flowers 线段树+二分
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4614 题意: 给你N个花瓶,编号是0 到 N - 1 ,初始状态花瓶是空的,每个花瓶最多插一朵花. ...
- HDU 4614 Vases and Flowers(线段树+二分)
题目链接 比赛的时候一直想用树状数组,但是树状数组区间更新之后,功能有局限性.线段树中的lz标记很强大,这个题的题意也挺纠结的. k = 1时,从a开始,插b个花,输出第一个插的位置,最后一个的位置, ...
- hdu 4614 Vases and Flowers 线段树
题目链接 一共n个盒子, 两种操作, 第一种是给出两个数x, y, 从第x个盒子开始放y朵花, 一个盒子只能放一朵, 如果某个盒子已经有了, 那么就跳过这个盒子放下面的盒子. 直到花放完了或者到了最后 ...
- hdu4614 Vases and Flowers【线段树】【二分】
Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to N ...
- HDU-4614 Vases and Flowers(线段树区间更新+二分查找)
http://acm.hdu.edu.cn/showproblem.php?pid=4614 Time Limit: 4000/2000 MS (Java/Others) Memory Limi ...
- HDU-4614 Vases and Flowers (线段树区间更新)
题目大意:有n个花瓶,每个花瓶中只能放一朵花.两种操作,一种是从A开始放F朵花,如果有的花瓶中已经有花则跳过这个花瓶,往下一个花瓶放:第二种是将区间[A,B]之间花瓶中的花清空.如果是第一种操作,输出 ...
- HDU4614 Vases and Flowers 二分+线段树
分析:感觉一看就是二分+线段树,没啥好想的,唯一注意,当开始摆花时,注意和最多能放的比大小 #include<iostream> #include<cmath> #includ ...
- HDU4614 Vases and Flowers
http://acm.hdu.edu.cn/showproblem.php?pid=4614 HDU 4614 Vases and Flowers (2013多校第二场线段树) // #pragma ...
随机推荐
- [Codeforces721E]Road to Home
Problem 有一条长为l的公路(可看为数轴),n盏路灯,每盏路灯有照射区间且互不重叠. 有个人要走过这条公路,他只敢在路灯照射的地方唱歌,固定走p唱完一首歌,歌曲必须连续唱否则就要至少走t才能继续 ...
- [POJ3378]Crazy Thairs
Problem 给你一个数列,让你求由五个元素组成的顺序对的个数. Solution DP:用DP[i][j]表示把第j个作为五元组中第i个的方案数 则DP[i][j]=sum{DP[k][j-1]} ...
- Xcode清理存储空间
文章来自 枣泥布丁 http://www.cocoachina.com/ios/20170711/19814.html 请针对性的选择删除 移除 Xcode 运行安装 APP 产生的缓存文件(Deri ...
- 福大软工1816 · 第三次作业 - 结对项目Salty Fish原型图
SALTY FISH原型图 LINKS IMPORT to LIST FOCUS TRENDS ANALYSE NIGHT
- VS2010编译Unigine_2010源码
VS2010编译Unigine_2010源码[Debug版本] 1.Laucher工程属性改为控制台项目 2.Unigine工程编译时的Warnning LNK2019 a.属性--常规-目标文件名改 ...
- 深入理解java虚拟机---虚拟机工具jinfo(十五)
作用: 实时查看和调整虚拟机参数. jinfo 是jdk自带的一个工具,它可以用来查看正在运行的java应用程序的扩展参数(JVM中-X标示的参数):甚至支持在运行时修改部分参数. 1.通过以下的命令 ...
- Linux7 下重新安装YUM
所有操作均在ROOT用户下,系统版本是Linux7.0 X86_64: 一.删除原有YUM # rpm -aq|grep yum|xargs rpm -e --nodeps 二.下载yum,注意自己的 ...
- shell中环境变量
Linux中环境变量包括系统级和用户级,系统级的环境变量是每个登录到系统的用户都要读取的系统变量,而用户级的环境变量则是该用户使用系统时加载的环境变量. 所以管理环境变量的文件也分为系统级和用户级的, ...
- netty源码理解补充 之 DefaultChannelPipeline到底是个啥
- SpringSecurity自定义登陆页面和跳转页面
如果我们不用form-login说明登陆界面,springsecurity框架将自动为我们生成登陆界面 现在我们不想用自动生成的登陆界面了,而想使用自定义的漂亮的登陆界面 则需要使用<secur ...