题目连接: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(线段树+二分)的更多相关文章

  1. hdu4614 线段树+二分 插花

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

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

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

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

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

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

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

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

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

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

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

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

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

  8. 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, ...

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

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

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

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

随机推荐

  1. 基于visual Studio2013解决面试题之0409判断一个栈是否另外一个栈的弹出序列

     题目

  2. 联系人数据库设计之ContactsTransaction

    不当之处,请雅正. 请自行下载android源代码 package com.android.providers.contacts; import com.google.android.collect. ...

  3. Oracle Autonomous Transactions(自治事务)

    Oracle Autonomous Transactions Autonomous transactions allow you to leave the context of the calling ...

  4. 使用ServletFileUpload实现上传

    1.首先我们应该为上传的文件建一个存放的位置,一般位置分为暂时和真是目录,那我们就须要获取这俩个目录的绝对路径,在servlet中我们能够这样做 ServletContext application ...

  5. 新建表维护程序SM30

    1.先新建一个客制表 2.创建一个函数组 3.SE11中该表->实用程序->表维护生成器->权限组填写&NC& ->  函数组填写刚才创建的函数组->维护 ...

  6. Lucene.Net 2.3.1开发介绍——附录一、如何下载Lucene.Net的各种版本

    原文:Lucene.Net 2.3.1开发介绍--附录一.如何下载Lucene.Net的各种版本 首先,你需要一个svn客户端.TortoiseSVN非常好用,可以从官方网站下载.下载地址:http: ...

  7. 与众不同 windows phone (1) - Hello Windows Phone

    原文:与众不同 windows phone (1) - Hello Windows Phone [索引页] [源码下载] 与众不同 windows phone (1) - Hello Windows ...

  8. ubuntu10.10 tftp安装,配置,测试

    ubuntu10.10 tftp安装,配置,测试 成于坚持,败于止步 虽然ubuntu/centos/redhat都是linux,但是内核其中存在一定的修改,所以对于tftp服务器的安装存在不同的命令 ...

  9. 简单的RPC java实现

    RPC的名声大噪之时是在2003年,那一个“冲击波”病毒(Blaster Worm virus)袭卷全球的一年.而“冲击波”正是用着RPC这把刀来敲开了远程电脑的大门.当然RPC 有更多正面的应用,比 ...

  10. Python语法