HDU 4614 线段树+二分查找
Vases and Flowers
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=4614
Problem Description
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
题意
有一个初始全为零的序列,支持两种操作:
1.1 x y 从x开始往后找y个为0的位置并赋值为1,找不满没关系。输出未赋值时第一个为零位置和最后一个为零的位置,如果没有一个为零的位置输出 Can not put any one.
2.2 x y 输出x到y的和,并将x到y赋值成0
题解
主要操作就是区间覆盖和区间求和,至于操作一,我们可以二分查找,左区间就是x,有区间r二分,sum[x,r]随r单调不减,我们只要求最左边的sum[x,r]=1和sum[x,r]=y的位置即可,还有些细节可以仔细想想。
代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 0x7f7f7f7f
#define N 100050
int n,m;
template<typename T>void read(T&x)
{
ll k=0; char c=getchar();
x=0;
while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
if (c==EOF)exit(0);
while(isdigit(c))x=x*10+c-'0',c=getchar();
x=k?-x:x;
}
void read_char(char &c)
{while(!isalpha(c=getchar())&&c!=EOF);}
struct Node{int l,r,lazy,sum;};
struct segmentTree
{
Node tr[N<<2];
void push_up(int x);
void push_down(int x);
void bt(int x,int l,int r);
void update(int x,int l,int r,int tt);
int query(int x,int l,int r);
int ef(int k,int x,int l,int r);
}seg;
void segmentTree::push_up(int x)
{
int len=tr[x].r-tr[x].l+1;
if (len>1)tr[x].sum=tr[x<<1].sum+tr[x<<1|1].sum;
if (tr[x].lazy!=-1)tr[x].sum=tr[x].lazy*len;
}
void segmentTree::push_down(int x)
{
if (tr[x].lazy==-1)return;
tr[x<<1|1].lazy=tr[x<<1].lazy=tr[x].lazy;
push_up(x<<1);
push_up(x<<1|1);
tr[x].lazy=-1;
}
void segmentTree::bt(int x,int l,int r)
{
tr[x]=Node{l,r,0,0};
if (l==r)return;
int mid=(l+r)>>1;
bt(x<<1,l,mid);
bt(x<<1|1,mid+1,r);
}
void segmentTree::update(int x,int l,int r,int tt)
{
if (l<=tr[x].l&&tr[x].r<=r)
{
tr[x].lazy=tt;
push_up(x);
return;
}
int mid=(tr[x].l+tr[x].r)>>1;
push_down(x);
if (l<=mid)update(x<<1,l,r,tt);
if (mid<r)update(x<<1|1,l,r,tt);
push_up(x);
}
int segmentTree::query(int x,int l,int r)
{
if (l<=tr[x].l&&tr[x].r<=r)return tr[x].sum;
int mid=(tr[x].l+tr[x].r)>>1,ans=0;
push_down(x);
if (l<=mid)ans+=query(x<<1,l,r);
if (mid<r)ans+=query(x<<1|1,l,r);
return ans;
}
int segmentTree::ef(int k,int x,int l,int r)
{
if (l==r)return l;
int mid=(l+r)>>1;
int tp=mid-x+1-query(1,x,mid);
if (tp<k)return ef(k,x,mid+1,r);
if (tp>=k)return ef(k,x,l,mid);
}
void work()
{
read(n); read(m);
seg.bt(1,0,n-1);
for(int i=1;i<=m;i++)
{
int id,x,y;
read(id); read(x); read(y);
if (id==1)
{
int k=seg.query(1,x,n-1);
if (k==n-x){printf("Can not put any one.\n");continue;}
int ds=seg.ef(1,x,x,n-1);
int dw=seg.ef(min(n-x-k,y),x,x,n-1);
seg.update(1,ds,dw,1);
printf("%d %d\n",ds,dw);
}
if (id==2)
{
printf("%d\n",seg.query(1,x,y));
seg.update(1,x,y,0);
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
int T;
read(T);
while(T--)work(),printf("\n");
}
HDU 4614 线段树+二分查找的更多相关文章
- L - Vases and Flowers HDU - 4614 线段树+二分
题意 给出一排空花瓶 有两种操作 1是 从A花瓶开始放F朵花 如果当前瓶有花就跳过前往下一个 直到花用完或者 瓶子到了最后一个为止 输出 成功放花的第一个和最后一个 如果没有输出 can not. ...
- 离散化+线段树/二分查找/尺取法 HDOJ 4325 Flowers
题目传送门 题意:给出一些花开花落的时间,问某个时间花开的有几朵 分析:这题有好几种做法,正解应该是离散化坐标后用线段树成端更新和单点询问.还有排序后二分查找询问点之前总花开数和总花凋谢数,作差是当前 ...
- G - Queue HDU - 5493 线段树+二分
G - Queue HDU - 5493 题目大意:给你n个人的身高和这个人前面或者后面有多少个比他高的人,让你还原这个序列,按字典序输出. 题解: 首先按高度排序. 设每个人在其前面有k个人,设比这 ...
- hdu 4614 线段树
思路:当k为1的时候,用二分法查询包含有f个空瓶的上界r,然后更新会方便很多,直接更新区间(a,r)了. #include<iostream> #include<cstdio> ...
- hdu4614 线段树+二分 插花
Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to N ...
- hdu 3436 线段树 一顿操作
Queue-jumpers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- hdu 4578 线段树(标记处理)
Transformation Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 65535/65536 K (Java/Others) ...
- hdu 4267 线段树间隔更新
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K ...
- hdu 3954 线段树 (标记)
Level up Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
随机推荐
- re匹配 [\s\S][\w\W]的使用.
本来想提取一个字符串写了一堆正则都提取不出来. 因为有特殊字符 后来使用 [\s\S]* 或 [\w\W]* 匹配出来. \s 空白字符 [ \t\n\r\f\v] \S 非空白字符 相当于 [^ \ ...
- ACM之路(12)—— KMP & 扩展KMP & Manacher
最近做完了kuangbin的一套关于kmp的题目(除了一道字典树的不会,因为还没学字典树所以先放放),做个总结.(kuangbin题目的链接:http://acm.hust.edu.cn/vjudge ...
- redhat7.4安装gitlab
1.参考官方安装指南 https://about.gitlab.com/install/#centos-7 2.遇到的问题 2.1.启动postfix出错 错误内容 Job for postfix.s ...
- mlflow ui 启动报错No such file or directory: 'gunicorn': 'gunicorn'
1.mlflow ui 启动报错,信息如下: [root@localhost mlflow]# mlflow ui /usr/local/python3/lib/python3./importlib/ ...
- Qtcreator中printf()/fprintf()不显示问题处理方法
此处只介绍解决办法,有兴趣的朋友可以分析原因. [问题] 使用Qtcreator开发项目中,printf()的诊断信息,在“应用程序输出”窗口不显示. [解决方法] 1.printf()不显示解决示例 ...
- mybatis中foreach参数过多效率很慢的优化
foreach 后面in 传入的参数有1万条,#和$是有效率区别的,$的效率远高于#,上篇文章做了比较. 但没达到我的理想结果. 1. 更改方式,把foreach 去掉,改成拼装方式, 参数直接拼装成 ...
- pwn学习日记Day20 《程序员的自我修养》读书笔记
可执行文件的装载与进程 覆盖装入和页映射是两种典型的动态装载方法 进程建立的三步 1.创建一个独立的虚拟地址空间 2.读取可执行文件头,并且建立虚拟空间与可执行文件的映射关系. 3.将CPU的指令寄存 ...
- Chrome浏览器报错:ERR_UNSAFE_PORT
今天用Chrome浏览器打开一个页面发现报错了:ERR_UNSAFE_PORT. 所以,去搜了一下发现Chrome浏览器是默认一些端口号为非安全端口的. 遇到这个问题建议更换端口号或者更换浏览器打开. ...
- 进程 | 线程 | 当Linux多线程遭遇Linux多进程
背景 本文并不是介绍Linux多进程多线程编程的科普文,如果希望系统学习Linux编程,可以看[<Unix环境高级编程>第3版] 本文是描述多进程多线程编程中遇到过的一个坑,并从内核角度分 ...
- kotlin 类的继承
与Java不同,kotlin 使用冒号,而Java 中使用extends, 注意冒号后面需要调用夫类的构造器.属于单继承,使用open 关键字允许继承class package loaderman.d ...