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 ...
随机推荐
- 正则表达式——WPF输入控件TextBox 限定输入特定字符
概念: 正则表达式是对字符串操作的一种逻辑公式, 就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”, 这个“规则字符串”用来表达对字符串的一种过滤逻辑. 目的: 给定一个正 ...
- day40-socket编程
一.socket介绍 看socket之前,先来回顾一下五层通讯流程: 但实际上从传输层开始以及以下,都是操作系统帮咱们完成的 Socket又称为套接字,它是应用层与TCP/IP协议族通信的中间软件抽象 ...
- Tomcat 配置Https
https://www.cnblogs.com/wanghaoyuhappy/p/5267702.html JDK1.8 keytool 生存证书 C:\keys\tomcat.keystore 1: ...
- static,final的用法
static的用法:修鉓符,修鉓属性,方法,代码块a1.修鉓属性:该属性是一个静态的属性,叫类的成员(没有static修鉓的属性叫实例的成员,调用时用:对象名.属性),调用:类名.属性.a2.修鉓方法 ...
- 企业应用--Nginx&web部署
一.Nginx介绍 Nginx (engine x) 是一个高性能的HTTP和反向代理服务,也是一个IMAP/POP3/SMTP服务. Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler ...
- mac+windows下从git上拉取项目及运行
一.Mac下从git拉取项目 1. 拉项目 打开终端,先进入想放置项目的目录.假设进入workfile目录,输入cd workfile. 进入workfile目录后:输入git clone 链接(gi ...
- 在Virtualenv中使用Idle
原创 在Virtualenv中使用Idle 本文适用于Linux系统,在DebianTesting上测试通过. 关于Virtualenv 先看一段Virtualenv这货的官方介绍: virtuale ...
- 漫谈C指针:参数传递的三道题目
漫谈C指针:参数传递的三道题目 2009-07-02 开讲之前,我先请你做三道题目.(嘿嘿,得先把你的头脑搞昏才行……唉呀,谁扔我鸡蛋?) 考题一,程序代码如下: [c] view plaincopy ...
- Hadoop集群(四) Hadoop升级
Hadoop前面安装的集群是2.6版本,现在升级到2.7版本. 注意,这个集群上有运行Hbase,所以,升级前后,需要启停Hbase. 更多安装步骤,请参考: Hadoop集群(一) Zookeepe ...
- 学JS的心路历程 -函式(三)this
this是什么,取决于被呼叫的呼叫地点. 昨天有提到说,呼叫函式时候会传递隐含参数:arguments和this并讲解了arguments,今天我们就来探讨this吧! 什么是this 我们都会呼叫函 ...