hdu4614(线段树+二分)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4614
题意:给定一个区间[0,N-1],初始时每个位置上的数字都是0,可以对其进行以下两种操作:
1、在位置A开始寻找F(如果没有这么多,则有多少个就找多少个)个数值为0的位置,把位置上的数修改为1,并返回第一个和最后一个修改的位置
2、查询区间[a,b]内1的个数,并把区间[a,b]每个位置上的数修改为0
线段树功能:区间更替,区间求和。
分析:sum[rt]表示区间0的个数,二分找出0~L的num个0,再找出0~R的num+y个0,则区间里刚好有y个0种植,将L~R更替为1;
查询含有1的个数:区间长度len(r-l+1)-query(l,r)(0的个数),更替区间(l,r)为0.
#pragma comment(linker,"/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 10007
#define inf 0x3f3f3f3f
#define N 50010
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
int sum[N<<],col[N<<];
void Pushup(int rt)
{
sum[rt]=sum[rt<<]+sum[rt<<|];
}
void build(int l,int r,int rt)
{
col[rt]=-;
if(l==r)
{
sum[rt]=;
return;
}
int m=(l+r)>>;
build(lson);
build(rson);
Pushup(rt);
}
void Pushdown(int rt,int len)
{
if(col[rt]!=-)
{
col[rt<<]=col[rt<<|]=col[rt];
sum[rt<<]=(len-(len>>))*col[rt];
sum[rt<<|]=(len>>)*col[rt];
col[rt]=-;
}
}
void update(int L,int R,int c,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
col[rt]=c;
sum[rt]=(r-l+)*c;
return;
}
Pushdown(rt,r-l+);
int m=(l+r)>>;
if(L<=m)update(L,R,c,lson);
if(m<R)update(L,R,c,rson);
Pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
return sum[rt];
}
Pushdown(rt,r-l+);
int m=(l+r)>>;
int res=;
if(L<=m)res+=query(L,R,lson);
if(m<R)res+=query(L,R,rson);
return res;
}
int bin(int x,int y,int num,int cnt)
{
int l=x,r=y,ans;
while(l<=r)
{
int mid=(l+r)>>;
if(query(,mid,,y,)-num>=cnt)
r=mid-,ans=mid;
else l=mid+;
}
return ans;
}
int main()
{
int t,n,m,num;
int op,x,y;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
build(,n-,);
while(m--)
{
scanf("%d%d%d",&op,&x,&y);
if(op==)
{
int sum=query(x,n-,,n-,);
if(sum==)
{
puts("Can not put any one.");
continue;
}
if(sum<y)y=sum;
if(x->=)num=query(,x-,,n-,);
else num=;
int left,right;
left=bin(x,n-,num,);
right=bin(x,n-,num,y);
printf("%d %d\n",left,right);
update(left,right,,,n-,);
}
else
{
printf("%d\n",y-x+-query(x,y,,n-,));
update(x,y,,,n-,);
}
}
puts("");
}
}
hdu4614(线段树+二分)的更多相关文章
- hdu4614 线段树+二分 插花
Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to N ...
- Codeforces Gym 100803G Flipping Parentheses 线段树+二分
Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...
- Codeforces Gym 100231B Intervals 线段树+二分+贪心
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...
- 洛谷P4344 脑洞治疗仪 [SHOI2015] 线段树+二分答案/分块
!!!一道巨恶心的数据结构题,做完当场爆炸:) 首先,如果你用位运算的时候不小心<<打成>>了,你就可以像我一样陷入疯狂的死循环改半个小时 然后,如果你改出来之后忘记把陷入死循 ...
- luogu4422 [COCI2017-2018#1] Deda[线段树二分]
讨论帖:线段树二分的题..我还考场切过..白学 这题我一年前的模拟赛考场还切过,现在就不会了..好菜啊. 显然直接线段树拆成$\log n$个区间,然后每个区间在进行线段树二分即可. UPD:复杂度分 ...
- bzoj4399 魔法少女LJJ 线段树合并+线段树二分+并查集
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4399 题解 毒瘤题 \(9\) 种操作还有支持动态图的连通性 仔细读题 $ c<=7$. ...
- [BZOJ 2653] middle(可持久化线段树+二分答案)
[BZOJ 2653] middle(可持久化线段树+二分答案) 题面 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整. 给你一个长度为n的序 ...
- 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, ...
- 洛谷$P2824\ [HEOI2016/TJOI2016]$ 排序 线段树+二分
正解:线段树+二分 解题报告: 传送门$QwQ$ 昂着题好神噢我$jio$得$QwQQQQQ$,,, 开始看到长得很像之前考试题的亚子,,,然后仔细康康发现不一样昂$kk$,就这里范围是$[1,n]$ ...
- 2021.12.09 [HEOI2016/TJOI2016]排序(线段树+二分,把一个序列转换为01串)
2021.12.09 [HEOI2016/TJOI2016]排序(线段树+二分,把一个序列转换为01串) https://www.luogu.com.cn/problem/P2824 题意: 在 20 ...
随机推荐
- 基于visual Studio2013解决C语言竞赛题之1083人机博弈
题目 解决代码及点评 /************************************************************************/ /* ...
- 六款常用的linux C/C++ IDE
摘要: 一.AnjutaAnjuta是一个多语言的IDE,它最大的特色是灵活,同时打开多个文件,内嵌代码级的调试器(调用gdb),应用程序向导(Application wizards)可以方便的帮助你 ...
- linxu select 返回值
#include <sys/types.h>#include <sys/socket.h>#include <string.h>#include <netin ...
- js监听滚动条 回到顶端
效果:当出现滚动条,且滚动条出现移动时,把回到顶端按钮 显示出来:当滚动条回到顶部时,将回到顶端按钮隐藏. <script type="text/javascript"> ...
- 控件编写:增强 TMEMO (一)(增加对WM_HSCROLL消息的处理)
相信没有什么人对 MEMO 陌生了吧.尽管其组件的功能不错.但是,对它进行一些功能的改进,可以更好的使用. 有的时候,我们想要知道,当前的坐标是什么?甚至,想要在 滚动条滚动时触发一些事件. 但,TM ...
- TComponent,TControl,TWinControl,TGraphic的DefineProperties赏析与说明(不懂)
先观赏一下最后的实现效果: object Form1: TForm1 Left = Top = Width = Height = Caption = 'Form1' Color = clBtnFace ...
- 【ASP.NET Web API教程】3.4 HttpClient消息处理器
原文:[ASP.NET Web API教程]3.4 HttpClient消息处理器 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的内容. 3.4 ...
- IDFA的值什么时候会发生改变
在何种情况下 , 应用的IDFA值会发生改变? 近期工作中须要获得一个能够唯一地标示每个不同应用的ID,之前的苹果UDID已经不让使用了. 那么我们须要使用新的IDFA来引用.可是在某些情况下这个ID ...
- HOJ 2245 浮游三角胞(数学啊 )
题目链接:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=2245 Time Limi ...
- Delphi经典网站收藏
http://delphi.icm.edu.pl/ 波兰的Delphi控件网站 http://dev.rdxx.com/Delphi/ 国内的编程网站 非常全面 http://oracle.ch ...