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

[pre]3 7
2
1 9
4
Can not put any one. 2 6
2
0 9
4
4 5
2 3 [/pre]

要和二分结合一下 线段树方面没有什么特别的

但是一时想不到要和二分结合

查询就是查区间的和 更新也就是全变0或全变1 关键就是怎么找到最开始可以插花的位子和最后一个位子

这里就需要二分了

先二分找到可以插花的位子 pos

再二分找到可以插花的最远的位子ans 这个位子可能已经有花了 反正就是pos到ans可以把花都插进去

最后二分找到实际的最后插花的位子 realans


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
#define inf 1e18
using namespace std; const int maxn = 50005;
int flower[maxn << 2];
int same[maxn << 2];
int m, n; void pushup(int rt)
{
flower[rt] = flower[rt << 1] + flower[rt<<1|1];
//same[rt] = same[rt<<1] && same[rt<<1|1];
} void build(int l, int r, int rt)
{
flower[rt] = 0;
same[rt] = 0;
if(l == r) return;
int m = (l + r) >> 1;
build(l, m, rt << 1);
build(m + 1, r, rt << 1 | 1);
pushup(rt);
} void pushdown(int l, int r, int rt)
{
if(same[rt]){
int m = (l + r) / 2;
same[rt<<1] = same[rt<<1|1] = same[rt];
flower[rt<<1] = (m - l + 1) * same[rt];
flower[rt<<1|1] = (r - m) * same[rt];
if(same[rt] == -1){
flower[rt<<1] = flower[rt<<1|1] = 0;
}
same[rt] = 0;
} } void update(int L, int R, int x, int l, int r, int rt)
{
if(l >= L && r <= R){
if(x == 1){
flower[rt] = r - l + 1;
same[rt] = true;
}
else{
flower[rt] = 0;
same[rt] = -1;
}
return;
}
pushdown(l, r, rt);
int m = (l + r) / 2;
if(L <= m) update(L, R, x, l, m, rt<<1);
if(R > m) update(L, R, x, m + 1, r, rt<<1|1);
pushup(rt);
} int query(int L, int R, int l, int r, int rt)
{
if(L <= l && R >= r){
return flower[rt];
}
pushdown(l, r, rt);
int m = (l + r) / 2, ans = 0;
if(L <= m) ans += query(L, R, l, m, rt<<1);
if(R > m) ans += query(L, R, m + 1, r, rt<<1|1);
pushup(rt);
return ans;
} int main()
{
int t;
cin>>t;
while(t--){
scanf("%d%d", &n, &m);
build(1, n, 1);
while(m--){
int l, r, op;
scanf("%d%d%d", &op, &l, &r);
l++;r++;
if(op == 1){
r--;
int left = l, right = n, pos = -1;
while(right - left >= 0){//找第一个可插花点
int mid = (right + left) / 2;
if(query(l, mid, 1, n, 1) == mid - l + 1){
left = mid + 1;
}
else{
right = mid - 1;
pos = mid;
}
}
if(pos == -1)
printf("Can not put any one.\n");
else{
int ans = -1;
left = pos;
right = n;
while(right - left >= 0){//找最远可插花位子 得到的ans可能原来已经被插上花
int mid = (left + right) / 2;
if(mid - pos + 1 - query(pos, mid, 1, n, 1) <= r){
left = mid + 1;
ans = mid;
}
else{
right = mid - 1;
}
}
int realans = -1;
left = pos;
right = ans;
while(right - left >= 0){//找到实际插最后一朵花的位子,可能花没被插完
//要在pos到ans中找到最远的空位
int mid = (left + right) / 2;
if(ans - mid + 1 - query(mid, ans, 1, n, 1) == 0){
right = mid - 1;
realans = mid;
}
else left = mid + 1;
}
if(realans == -1){
printf("%d %d\n", pos - 1, ans - 1);
}
else{
printf("%d %d\n", pos - 1, realans - 2);
}
update(pos, ans, 1, 1, n, 1);
}
}
if(op == 2){
printf("%d\n", query(l, r, 1, n, 1));
update(l, r, -1, 1, n, 1);
}
}
printf("\n");
}
return 0;
}

hdu4614 Vases and Flowers【线段树】【二分】的更多相关文章

  1. hdu4614 Vases and Flowers 线段树+二分

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4614 题意: 给你N个花瓶,编号是0  到 N - 1 ,初始状态花瓶是空的,每个花瓶最多插一朵花. ...

  2. hdu4614 Vases and Flowers 线段树

    Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to N ...

  3. HDU-4614 Vases and Flowers 线段树区间更新

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4614 线段树保存区间是否被覆盖以及区间的和即可,在询问的时候在线段树上二分查找就可以了...代码写得比 ...

  4. HDU 4614 Vases and Flowers(线段树+二分)

    题目链接 比赛的时候一直想用树状数组,但是树状数组区间更新之后,功能有局限性.线段树中的lz标记很强大,这个题的题意也挺纠结的. k = 1时,从a开始,插b个花,输出第一个插的位置,最后一个的位置, ...

  5. hdu 4614 Vases and Flowers 线段树

    题目链接 一共n个盒子, 两种操作, 第一种是给出两个数x, y, 从第x个盒子开始放y朵花, 一个盒子只能放一朵, 如果某个盒子已经有了, 那么就跳过这个盒子放下面的盒子. 直到花放完了或者到了最后 ...

  6. hdu4614 线段树+二分 插花

    Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to N ...

  7. Codeforces Gym 100803G Flipping Parentheses 线段树+二分

    Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...

  8. Codeforces Gym 100231B Intervals 线段树+二分+贪心

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...

  9. 洛谷P4344 脑洞治疗仪 [SHOI2015] 线段树+二分答案/分块

    !!!一道巨恶心的数据结构题,做完当场爆炸:) 首先,如果你用位运算的时候不小心<<打成>>了,你就可以像我一样陷入疯狂的死循环改半个小时 然后,如果你改出来之后忘记把陷入死循 ...

  10. luogu4422 [COCI2017-2018#1] Deda[线段树二分]

    讨论帖:线段树二分的题..我还考场切过..白学 这题我一年前的模拟赛考场还切过,现在就不会了..好菜啊. 显然直接线段树拆成$\log n$个区间,然后每个区间在进行线段树二分即可. UPD:复杂度分 ...

随机推荐

  1. AndroidManifest详解

    一,重要性AndroidManifest.xml是Android应用程序中最重要的文件之一.它是Android程序的全局配置文件,是每个 android程序中必须的文件.它位于我们开发的应用程序的根目 ...

  2. OpenGL资料

    苹果官方文档:OpenGL ES for iOS苹果官方文档:OpenGL for OS X OpenGL是源自SGI IRIS GL library,并不是SUN开发的.SGI提供了一个OPENGL ...

  3. Popupwindow全屏问题

    //sdk > 21 解决 标题栏没有办法遮罩的问题 popupWindow.setClippingEnabled(false);

  4. 使用IEDA新建jsp项目以后使用javax.servlet.*报错

    新建一个jsp项目,然后再里面配置完了一切写了一个servlet的文件: 点击运行的时候出现了javax程序包不存在的错误,百度了许多都在说是tomcat的事情,吧tomcat/lib下面的servl ...

  5. 查看sql server日志

    如果是查询当前已经连接到服务器的用户 select loginame, * from master.dbo.sysprocesses 查看sql 的操作日志记录 SELECT * From ::fn_ ...

  6. 关于RabbitMQ交换机的理解

    RabbitMQ是实现AMQP(高级消息队列协议)的消息中间件的一种,最初起源于金融系统,用于在分布式系统中存储转发消息,在易用性.扩展性.高可用性等方面表现不俗.消息中间件主要用于组件之间的解耦,消 ...

  7. php计算两个日期时间差(返回年、月、日)

    在PHP程序中,很多时候都会遇到处理时间的问题,比如:判断用户在线了多长时间,共登录了多少天,两个帖子发布的时间差或者是不同操作之间的日志记录等等.在文章中,简单地举例介绍了PHP中如何计算两个日期相 ...

  8. 使用API函数EnumWindows()枚举顶层窗口

      http://simpleease.blog.163.com/blog/static/1596085820052770290/ 要枚举Windows当前所有打开的顶层窗口,可使用Windows A ...

  9. Trie树的分析与实现

    字典树 又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优点是:利用字符串的 ...

  10. 微信内置浏览器浏览H5页面弹出的键盘遮盖文本框的解决办法(转)

    最近在做微信公众号的内嵌页面,发现点击输入框时键盘盖住文本框,找到一段代码解决了这个问题. iOS和android手机都已亲测,需要的可以直接拷贝到代码中使用. js代码如下: $(function ...