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. Win10系列:UWP界面布局进阶1

    全新的Windows 10 操作系统支持多种视图模式,用户可以根据需要选择不同的视图模式显示应用.当用户同时浏览或操作多个应用程序时,可以将应用视图调整为辅屏视图或填充视图,这样在一个屏幕中可以同时对 ...

  2. 为什么IT运维工程师要学习Linux系统

    不论你是否知道,其实你每天都在使用Linux.每次你访问微博.百度甚至是一些小电影网站,你的客户端(浏览器)都在与运行在Linux系统上的服务端程序进行通讯,大多数的电子设备,例如数位录像机.飞机.自 ...

  3. Could not load driverClass ${driverClassName} 的解决方案

          对项目进行ssm整合的过程中,发现报这个错误:Could not load driverClass ${driverClassName} 不明所以,在网上找了半天,各种答案都有,最后终于找 ...

  4. TNetHTTPClient 使用

    unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...

  5. SQL-14 从titles表获取按照title进行分组,每组个数大于等于2,给出title以及对应的数目t。 注意对于重复的emp_no进行忽略。

    题目描述 从titles表获取按照title进行分组,每组个数大于等于2,给出title以及对应的数目t.注意对于重复的emp_no进行忽略.CREATE TABLE IF NOT EXISTS &q ...

  6. kubernetes 环境搭建(ubuntu16.04)

    通过kubeadm安装kubernetes的教程:1: 首先在每台机器上安装: docker(1.12), kubeadm(1.6), kubectl, kubelet, kubernetes-cni ...

  7. Java不同类型字符转换String/int/Float/////

    1.int & String int i=5678;String s=""; int->String: s=i+"";或 s=String.val ...

  8. jquery 正则表达式

  9. JAVA Clone复制对象

    谈到了对象的克隆,就不得不说为什么要对对象进行克隆.Java中所有的对象都是保存在堆中,而堆是供全局共享的.也就是说,如果同一个Java程序的不同方法,只要能拿到某个对象的引用,引用者就可以随意的修改 ...

  10. Django之模板层-自定义过滤器以及标签

    自定义标签与过滤器 在settings中的INSTALLED_APPS配置当前app,不然django无法找到自定义的simple_tag. 在app中创建templatetags模块(模块名只能是t ...