HDU 4614 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.
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.
Sample Input
2
10 5
1 3 5
2 4 5
1 1 8
2 3 6
1 8 8
10 6
1 2 5
2 3 4
1 0 8
2 2 5
1 4 4
1 2 3
Sample Output
3 7
2
1 9
4
Can not put any one.
2 6
2
0 9
4
4 5
2 3
题意
爱丽丝有N个花瓶,编号0-N-1,有M个操作
当K=1时,表示从A开始直到插完B朵花后停止,输出第一朵花和最后一朵花插的位置,如果一朵都插不进去输出Can not put any one,如果瓶子不够插输出第一朵插和最后插的位子
当K=2时,表示主人要清空[A,B]花瓶里的花,输出清除了多少朵
题解
线段树区间更新延迟标记,区间查询
当K=1时
如果[A,B]中0的个数为0表示已经插满,输出Can not put any one
如果0的个数>=B朵花,表示可以插B朵
如果0的个数<B朵花,表示只能插0的个数朵花
A点开始二分[A+1,N],区间查询[A,mid]插花的数目,找到能插入B朵花的最左端qr,再二分[A,qr]找到第一个插入的最左端ql,然后得到区间[ql,qr],更新即可
当K=2时
把[A,B]的区间更新
代码
#include<stdio.h>
#include<algorithm>
using namespace std; const int N=5e4+; struct node
{
int l,r,sum,lazy;
}a[N<<];
void PushUp(int rt)
{
a[rt].sum=a[rt<<].sum+a[rt<<|].sum;
}
void PushDown(int rt)
{
if(a[rt].lazy!=-)
{
a[rt<<].lazy=a[rt<<|].lazy=a[rt].lazy;
a[rt<<].sum=a[rt].lazy*(a[rt<<].r-a[rt<<].l+);
a[rt<<|].sum=a[rt].lazy*(a[rt<<|].r-a[rt<<|].l+);
a[rt].lazy=-;
}
}
void Build(int l,int r,int rt)
{
a[rt].l=l;
a[rt].r=r;
a[rt].sum=;
a[rt].lazy=-;
if(l==r)return;
int mid=(l+r)>>;
Build(l,mid,rt<<);
Build(mid+,r,rt<<|);
}
void Update(int L,int R,int C,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
a[rt].lazy=C;
a[rt].sum=C*(r-l+);
return;
}
int mid=(l+r)>>;
PushDown(rt);
if(L<=mid)Update(L,R,C,l,mid,rt<<);
if(R>mid)Update(L,R,C,mid+,r,rt<<|);
PushUp(rt);
}
int Query(int L,int R,int l,int r,int rt)
{ if(L<=l&&r<=R)
return a[rt].sum;
int mid=(l+r)>>,sum=;
PushDown(rt);
if(L<=mid)sum+=Query(L,R,l,mid,rt<<);
if(R>mid)sum+=Query(L,R,mid+,r,rt<<|);
PushUp(rt);
return sum;
}
int main()
{
int t,n,m,x,y,op;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
Build(,n,);
for(int i=;i<=m;i++)
{
scanf("%d%d%d",&op,&x,&y);
x++;
if(op==)
{
int cnt=n-x+-Query(x,n,,n,),ql=x,qr=n;//x--n未插的个数
if(cnt==)//全是花
{
printf("Can not put any one.\n");
continue;
}
else if(cnt>=y)//只能插y朵
{
int l=x,r=n;
while(l<=r)
{
int mid=(l+r)>>;
cnt=mid-x+-Query(x,mid,,n,);
if(cnt>=y)qr=mid,r=mid-;
else l=mid+;
}
}
else//只能插cnt朵
{
int l=x,r=n;
y=cnt;
while(l<=r)
{
int mid=(l+r)>>;
cnt=mid-x+-Query(x,mid,,n,);
if(cnt>=y)qr=mid,r=mid-;
else l=mid+;
}
}
///二分左区间[x,qr]
int l=x,r=qr;
while(l<=r)
{
int mid=(l+r)>>;
cnt=qr-mid+-Query(mid,qr,,n,);
if(cnt>=y)ql=mid,l=mid+;
else r=mid-;
}
Update(ql,qr,,,n,);
printf("%d %d\n",ql-,qr-);
}
if(op==)
{
y++;
printf("%d\n",Query(x,y,,n,));
Update(x,y,,,n,);
}
}
printf("\n");
}
return ;
}
HDU 4614 Vases and Flowers(二分+线段树区间查询修改)的更多相关文章
- HDU 4614 Vases and Flowers(线段树+二分)
Vases and Flowers Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others ...
- HDU 4614 Vases and Flowers 【线段树】+【二分】
<题目链接> 题目大意: 有n个花瓶,每个花瓶中只能放一朵花.两种操作,一种是从A开始放F朵花,如果有的花瓶中已经有花则跳过这个花瓶,往下一个花瓶放:第二种是将区间[A,B]之间花瓶中的花 ...
- HDU 4614 Vases and Flowers(线段树+记录区间始末点或乱搞)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4614 题目大意:有n个空花瓶,有两种操作: 操作①:给出两个数字A,B,表示从第A个花瓶开始插花,插B ...
- hdu 4614 Vases and Flowers(线段树:成段更新)
线段树裸题.自己写复杂了,准确说是没想清楚就敲了. 先是建点为已插花之和,其实和未插花是一个道理,可是开始是小绕,后来滚雪球了,跪了. 重新建图,分解询问1为:找出真正插画的开始点和终止点,做成段更新 ...
- hdu 4614 Vases and Flowers(线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4614 题意: 给你N个花瓶,编号是0 到 N - 1 ,初始状态花瓶是空的,每个花瓶最多插一朵花. ...
- HDU4614 Vases and Flowers 二分+线段树
分析:感觉一看就是二分+线段树,没啥好想的,唯一注意,当开始摆花时,注意和最多能放的比大小 #include<iostream> #include<cmath> #includ ...
- HDU - 4614 Vases and Flowers(二分+区间修改)
https://cn.vjudge.net/problem/HDU-4614 题意 n个花瓶,m个操作,花瓶里面有的有花,有的是空的.1操作是从a开始往右放b朵花,花瓶有了的不放,跳过,直到a右边都放 ...
- POJ_3468 A Simple Problem with Integers 【线段树区间查询+修改】
一.题目 POJ3468 二.分析 裸的线段树区间查询+修改. 三.AC代码 #include <cstdio> #include <iostream> #include &l ...
- 题解报告:hdu 1698 Just a Hook(线段树区间修改+lazy懒标记的运用)
Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for m ...
随机推荐
- day14-函数
1.定义函数 一个函数就是封闭一个功能def 函数名(): 函数代码注意:函数名不要用默认的关键字.否则会将默认关键字函数覆盖掉. 命名规则与变量相同,使用字母.数字.下划线组成,不能以数字开关 2. ...
- Oracle保留两位小数的函数
1.最终保存成字符串类型 使用to_char()函数 // 其9代表:如果存在数字则显示数字,不存在则显示空格 // 其0代表:如果存在数字则显示数字,不存在则显示0,即占位符 // 其FM代表:删除 ...
- CSS 点击事件
:active 伪类向激活(在鼠标点击与释放之间发生的事件)的元素添加特殊的样式. 这个伪类应用于处于激活状态的元素.最常见的例子就是在 HTML 文档中点击一个超链接:在鼠标按钮按下期间,这个链接是 ...
- Flex_布局和容器
1.Halo组件也称MX组件,是Flex3的独有组件(按钮.文本字段.容器等).而Flex4引入了新一代的组件,称为Spark. Flex4同时支持Halo和Spark.但是很多Halo组件都有更 ...
- sql server 2008 数据库可疑的解决步骤
备份并新建同名数据库,并替换原数据文件 1 把问题数据库备份后直接删除 停掉SQLSERVER服务,把服务器上出问题的数据库, 假设名称为 test的数据库文件及日志文件备份到其他目录,然后直接将其删 ...
- How to Pronounce the 50 States
How to Pronounce the 50 States (1/4) Share Tweet Share Tagged With: Places The US state names can be ...
- openwrt用WEB刷固件型号不对问题强行处理
参照这里:https://blog.csdn.net/caoshunxin01/article/details/79355602 原机是一块mt7620A的通板,之前刷了一个叫WE826型号的固件,发 ...
- 关于malloc(0)的返回值问题--这两天的总结与实践篇
就像我在http://www.cnblogs.com/wuyuegb2312/p/3219659.html 文章中评论的那样,我也碰到了被提问这个malloc(0)的返回值问题,虽然感觉这样做在实际中 ...
- Android EventBus3.x 使用详解
♪(^∇^*) 五一假期在家无事,新项目中用的是RxJava2+EventBus感觉还不错,趁这闲暇总结下EventBus. 一.概要简述 EventBus是一个基于观察者模式的Android事件发布 ...
- Android 深入浅出 - manifest文件的使用
AndroidManifest的作用简单举例 Manifest如何解析的?