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

现在要你插花,有n个花瓶,m次操作,初始花瓶中无花,操作有两种方式

操作1:1 a b,从编号为a的花瓶开始插花,共插b朵花,花只能插到无花的花瓶中,如果最后插不完b朵花,剩下的花舍弃掉 
操作2:1 a b,把从编号a到编号b的所有花瓶里的花全部清理掉

对于操作1,需要输出开始插花的瓶子编号,和最后插花的瓶子编号 
对于操作2,需要输出在a~b中总共清理了多少个花瓶中的花

题解:线段树+二分,分析一下这道题,如果是情理,那是十分容易的,只需要lazy,tag就可以了,但是如何去找区间第一个可以插的位置呢,

   线段树可以记录该位置里的有多少个空位置,那么我们可以二分,来查找判断是多了,还是少了,这样就可以了,时间复杂度就控制了

   下来,然后整个区间填满,什么区间?就是第一支花和最后一枝花那个区间,hh。

 #include<iostream>
#include<stdio.h>
#include<map>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include<string>
#include<cstring>
using namespace std;
struct node
{
int l,r;
int tag;//如果为-1则为初始状态,如果为0意思将子区间内的花清空,为1为插满花
int v;//记录区间内插了多少花
}t[*];
int n;
void Build(int l,int r,int k)
{
t[k].l=l;
t[k].r=r;
t[k].v=;
t[k].tag=-;
if(l==r)
return;
int mid=(l+r)/;
Build(l,mid,k*);
Build(mid+,r,k*+);
}
void pushdown(int k)//向下传递状态
{
t[k*].tag=t[k*+].tag=t[k].tag;
t[k*].v=t[k].tag*(t[k*].r-t[k*].l+);
t[k*+].v=t[k].tag*(t[k*+].r-t[k*+].l+);
t[k].tag=-;
}
void update(int l,int r,int v,int k)
{
if(t[k].l==l&&t[k].r==r)
{
t[k].tag=v;
t[k].v=v*(r-l+);//插满或者清空
return;
}
if(t[k].tag!=-)//要传递状态
pushdown(k);
int mid=(t[k].l+t[k].r)/;
if(r<=mid)
update(l,r,v,k*);
else if(l>mid)
update(l,r,v,k*+);
else
{
update(l,mid,v,k*);
update(mid+,r,v,k*+);
}
t[k].v=t[k*].v+t[k*+].v;//向上传递区间信息
}
int query(int l,int r,int k)//查询区间内插花的数目
{
if(t[k].l==l&&t[k].r==r)
{
return t[k].v;
}
if(t[k].tag!=-)//同样lazy tag
pushdown(k);
int mid=(t[k].l+t[k].r)/;
if(r<=mid)
return query(l,r,k*);
else if(l>mid)
return query(l,r,k*+);
else
{
return query(l,mid,k*)+query(mid+,r,k*+);
}
t[k].v=t[k*].v+t[k*+].v;
}
int dive(int s,int num)//二分查找,第一个为开始的位置,第二个参数为要插多少个
{
int temp=query(s,n,);
if(temp==n-s+)//如果一个都不能插
return -;
if(n-s+-temp<num)//如果空位小于要插的数目,改下要插的数目
num=n-s+-temp;
int l=s;
int r=n;
int mid,d;
int f=-;
while(r>=l)//二分
{
mid=(l+r)/;
d=mid-s+-query(s,mid,);//d为从s到mid的空位数目
if(d>num)
{
r=mid-;
}
else if(d<num)
l=mid+;
else//如果空位的数目正好要一个个减
{
f=mid;//为了保存第一个能够满足该数目空位的位置
r=mid-;
}
}
return f;
}
int main()
{
int i,j,test,x,y,d,m;
scanf("%d",&test);
while(test--)
{
scanf("%d%d",&n,&m);
n--;
Build(,n,);
for(i=;i<m;i++)
{
scanf("%d%d%d",&d,&x,&y);
if(d==)
{
int temp=dive(x,);//查找插的第一个位置
if(temp==-)
{
printf("Can not put any one.\n");
}
else
{
int en=dive(x,y);//查找插的第二个位置
printf("%d %d\n",temp,en);
update(temp,en,,);//更新插满区间
}
}
else
{
printf("%d\n",query(x,y,));
update(x,y,,);//清空区间
}
}
printf("\n");
}
return ;
}

hdu4614 线段树+二分 插花的更多相关文章

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

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

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

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

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

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

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

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

  5. bzoj4399 魔法少女LJJ 线段树合并+线段树二分+并查集

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4399 题解 毒瘤题 \(9\) 种操作还有支持动态图的连通性 仔细读题 $ c<=7$. ...

  6. [BZOJ 2653] middle(可持久化线段树+二分答案)

    [BZOJ 2653] middle(可持久化线段树+二分答案) 题面 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整. 给你一个长度为n的序 ...

  7. Educational Codeforces Round 64 (Rated for Div. 2) (线段树二分)

    题目:http://codeforces.com/contest/1156/problem/E 题意:给你1-n  n个数,然后求有多少个区间[l,r] 满足    a[l]+a[r]=max([l, ...

  8. 洛谷$P2824\ [HEOI2016/TJOI2016]$ 排序 线段树+二分

    正解:线段树+二分 解题报告: 传送门$QwQ$ 昂着题好神噢我$jio$得$QwQQQQQ$,,, 开始看到长得很像之前考试题的亚子,,,然后仔细康康发现不一样昂$kk$,就这里范围是$[1,n]$ ...

  9. 2021.12.09 [HEOI2016/TJOI2016]排序(线段树+二分,把一个序列转换为01串)

    2021.12.09 [HEOI2016/TJOI2016]排序(线段树+二分,把一个序列转换为01串) https://www.luogu.com.cn/problem/P2824 题意: 在 20 ...

随机推荐

  1. Apache2 httpd.conf 配置详解(一)

    常用配置指令说明 ServerRoot:服务器的基础目录,一般来说它将包含conf/和logs/子目录,其它配置文件的相对路径即基于此目录.默认为安装目录,不需更改. 语法:ServerRoot di ...

  2. MySQL(十)之视图

    前言 前面给大家介绍了查询语句,感觉写的还不错的,喜欢的可以去查看.今天给大家分享的是MySQL中的视图. 视图(View):视图是由查询结果形成一张虚拟的表.非临时表,只要不删除的话就会一直存放在磁 ...

  3. java读取txt文件内容

    package read; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; public ...

  4. 后台方庄List razor 循环

    后台: //1.查询所有年卡类型                StringBuilder sqlStr = new StringBuilder();                sqlStr.Ap ...

  5. 集美大学网络1413第十次作业成绩(团队六) -- 展示博客(Alpha版本)

    题目 团队作业6--展示博客(Alpha版本) 团队作业6成绩  团队/分值 简介& 项目地址 项目目标 (典型用户. 功能描述. 预期用户数量) 如何满足 用户需求 已完成目标 团队分工 团 ...

  6. 团队项目汇总beta

    一.Daily Scrum Meeting[Alpha] 4.23-第一天 4.24-第二天 4.25-第三天 4.26-第四天 4.27-第五天 4.28-第六天 4.29-第七天 二.Daily ...

  7. Beta版本冲刺计划及安排(附七天冲刺的博客链接)

    Beta版本冲刺计划及安排(附七天冲刺的博客链接) 新增组员 本次换人加入我们团队的新成员是原"爸爸说的都队"的队长念其锋同学,经过我们小组严格的两轮面试,他从几个同样前来面试的同 ...

  8. JAVA课程设计个人博客 学生成绩管理 201521145048 林健

    1. 团队课程设计博客链接 http://www.cnblogs.com/kawajiang/p/7062407.html 2.个人负责模块或任务说明 本人主要负责支持用户登录.验证操作,显示设计界面 ...

  9. 201521123008《Java程序设计》第十三周学习总结

    1. 本周学习总结 2. 书面作业 1. 网络基础 1.1 比较ping www.baidu.com与ping cec.jmu.edu.cn,分析返回结果有何不同?为什么会有这样的不同? ping w ...

  10. 201521123024 《Java程序设计》第13周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容.