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 ...
随机推荐
- hdu4709求三角形面积
Herding Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- hibernate解决oracle的id自增?
以前做SSH项目时,涉及到的数据库是mySQL,只需将bean的配置文件id设为native 就可以实现表id的自增. 现在用到了Oracle,当然知道这样是不行的啦,那么用序列自增? 我在网络上搜索 ...
- Oracle Patch Bundle Update
一.相关知识介绍 以前只知道有CPU(Critical Patch Update)和PSU(Patch Set Update),不知道还有个Bundle Patch,由于出现了TNS-12531的BU ...
- 原始的js代码和jquery对比
Even a task as simple as this can be complicated without jQuery at our disposal. In plain JavaScript ...
- C语言sendto()函数-经socket传送数据以及recvfrom函数《转》
相关函数:send, sendmsg, recv, recvfrom, socket 头文件:#include <sys/types.h> #include <sys/socke ...
- Eclipse Maven 插件地址
http://download.eclipse.org/technology/m2e/releases maven 插件安装地址
- delphi实现穿XP防火墙
procedure TForm1.Button1Click(Sender: TObject);var FwMgr,Profile,FwApp: variant;begin FwMgr := C ...
- HDU 3584 三维树状数组
三维树状数组模版.优化不动了. #include <set> #include <map> #include <stack> #include <cmath& ...
- ASIHTTPRequest开源类项目导入问题及解决方法
在静态库project中加入ASIHTTPRequest导出lib.a.放到project里编译出一下错: Undefined symbols for architecture armv7: &quo ...
- Introduction to Probability (三) Independence
两个事件独立性的定义是:事件A的发生对事件B的发生毫无影响,即从A的发生与否.我们不能猜測出B是否发生. 从概率等式的表示来看就是B在A发生的情况下发生的概率等于B发生的概率本身. 进而引出了A与B同 ...