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 ...
随机推荐
- centos查看命令
1.查看 CPU 物理个数 grep 'physical id' /proc/cpuinfo | sort -u | wc -l 2.查看 CPU 核心数量 grep 'core id' /proc/ ...
- PT 转 PX
pt (point,磅):是一个物理长度单位,指的是72分之一英寸. px (pixel,像素):是一个虚拟长度单位,是计算机系统的数字化图像长度单位,如果px要换算成物理长度,需要指定精度DPI(D ...
- MySQL 创建自定义函数
语法:Create function function_name(参数列表)returns返回值类型 函数体 函数名,应合法的标识符,不应与系统关键字冲突. 一个函数应该属于某个数据库,可以使用db_ ...
- Python运算符,基本数据类型
1,基本的运算符: 加,减,乘,除 取余(%) 取商(//) **(幂) in not in (判断是否在里面) 1.运算符 结果是值 算数运算 ...
- https 学习总结
最近看了点https 做下总结 面的博客如果没有错误的话,理解起来绝对是醍醐灌顶!让人信服,如果我的理解有问题,请及时指正! 参考博客: http://www.ruanyifeng.com/b ...
- Spark 调优(转)
Spark 调优 返回原文英文原文:Tuning Spark Because of the in-memory nature of most Spark computations, Spark pro ...
- 虚拟机centos NAT模式 配置静态ip
主要的设置有1.配置ip地址段,2.配置NAT(网关.ip地址端.子网掩码),3.修改网卡配置文件(/etc/sysconfig/network-scripts/ifcfg-eth0 ),4.重启网卡 ...
- OGNL表达式(转载)
OGNL表达式(转载) 1.什么是OGNL OGNL:Object Graphic Navigation Language(对象图导航语言) 它是Struts2中默认的表达式语言.使用表达式需要借 ...
- App.js实现使用js开发app的应用,此文是中文文档
在阅读前,在此说明下,本人英文一直不好,所以该文档是借助翻译工具翻译的,阅读起来可能有点不好,请各位谅解,哪位大神有标准的中文文档请分享下 Github下载地址:https://github.com/ ...
- form表单 获取与赋值
form表单中使用频繁的组件: 文本框.单选框.多选框.下拉框.文本域form通过getValues()获取表单中所有name的值 通过setValues({key:values})给对应的name值 ...