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解决面试题之0602全排列
题目
- C++学习之路—多态性与虚函数(二)纯虚函数与抽象类
(根据<C++程序设计>(谭浩强)整理,整理者:华科小涛,@http://www.cnblogs.com/hust-ghtao转载请注明) 1 纯虚函数 在前面的博客中已经提到:有时 ...
- JS - 侧边导航收缩伸展
下载地址:http://www.lanrentuku.com/js/daohang-1162.html
- android 请求网络 和 httpclient的使用上传下载
访问网络最主要的也就是 http协议了. http协议很简单,但是很重要. 直接上代码了,里面都是1个代码块 代码块的,用哪一部分直接拷出去用就好了. 1.访问网络用 get 和 post 自己组拼 ...
- 轻量级工具网站SimpleTools
[解释]本来这篇文章是在前天发出来的,可是当时是刚申请的域名,现在都要域名实名认证,导致我发的项目网址打不开,惹来了很多博友的吐槽,在此说声抱歉,今天一大早就把实名认证提交了,现在网站已经可以正常访问 ...
- mysql5.6 主从配置
参考网址:http://www.cnblogs.com/zhoujie/p/mysql1.html http://kerry.blog.51cto.com/172631/277414/ 1.配置主库: ...
- 与众不同 windows phone (6) - Isolated Storage(独立存储)
原文:与众不同 windows phone (6) - Isolated Storage(独立存储) [索引页][源码下载] 与众不同 windows phone (6) - Isolated Sto ...
- LeetCode——Count and Say
The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221 ...
- Android平均分布的布局图像的下一行
Android下一行平均分布图片的布局 这是一个非经常见的需求,比方有三个图片button,须要在底部三个平均,比方下个样例: 下面是布局文件 <LinearLayout android:lay ...
- 11 款最好 CSS 框架 让你的网站独领风骚
网页设计和发展领域已经成为竞争激烈的虚拟世界.想要在网络的虚拟世界中生存,仅有一堆静止的在线网络应用是远远不够的,网页必须要有很多功能,配以让人无法抗拒的设计.网页编码一定要合适.精确,才能保证不发生 ...