题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4614
题意:给你你个花瓶,开始时n个花瓶都是空的,且从0 -> n-1编号,m此操作,操作分两种。
(1)给一个起点a和花的数量f问从a开始知道把f朵花全部插入花瓶中或者到达最后一个,求这个区间的左边界和有边界。
(2)给一个区间x,y问这个区间内一共有多少朵花,并将这些花扔掉。
思路:我们假设花瓶里有花为1,无花为0,那么第二部操作就是线段树的区间查询操作。对于第一步,我们就是要找第一个区间和等于1的位置,以及第一个区间和等于m的位置,所以可以想到二分,判断条件中将左端点固定就好。
代码:
#include<set>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define LL long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int INF=0x3f3f3f3f;
const int maxn = 5e4+7;
int sum[maxn<<2],lazy[maxn<<2];
int L[maxn<<2],R[maxn<<2];
void push_up(int rt)
{
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void push_down(int rt)
{
if(lazy[rt]!=-1)
{
lazy[rt<<1]=lazy[rt],lazy[rt<<1|1]=lazy[rt];
sum[rt<<1]=lazy[rt]*(R[rt<<1]-L[rt<<1]+1);
sum[rt<<1|1]=lazy[rt]*(R[rt<<1|1]-L[rt<<1|1]+1);
lazy[rt]=-1;
}
}
void build(int l,int r,int rt)
{
R[rt]=r,L[rt]=l;
lazy[rt]=-1;
if(l==r)
{
sum[rt]=1;
return ;
}
int m=(l+r)>>1;
build(lson),build(rson);
push_up(rt);
}
void update(int L1,int R1,int val,int l,int r,int rt)
{
if(L1<=l&&r<=R1)
{
sum[rt]=val*(R[rt]-L[rt]+1);
lazy[rt]=val;
return ;
}
push_down(rt);
int m=(l+r)>>1;
if(L1<=m) update(L1,R1,val,lson);
if(R1>m) update(L1,R1,val,rson);
push_up(rt);
}
int query(int L1,int R1,int l,int r,int rt)
{
if(L1<=l&&r<=R1) return sum[rt];
push_down(rt);
int ans=0;
int m=(l+r)>>1;
if(L1<=m) ans+=query(L1,R1,lson);
if(R1>m) ans+=query(L1,R1,rson);
return ans;
}
int n,m;
int Find(int l,int r,int val)
{
int temp=l;
while(l<r)
{
int mid=(l+r)>>1;
if(query(temp,mid,1,n,1)>=val) r=mid;
else l=mid+1;
}
return r;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
build(1,n,1);
while(m--)
{
int opt;
scanf("%d",&opt);
if(opt==1)
{
int x,f;
scanf("%d%d",&x,&f);
x++;
int res=query(x,n,1,n,1);
if(res==0) printf("Can not put any one.\n");
else
{
int l=Find(x,n,1);
int r=Find(l,n,min(f,res));
update(l,r,0,1,n,1);
printf("%d %d\n",--l,--r);
}
}
else
{
int x,y;
scanf("%d%d",&x,&y);
x++,y++;
printf("%d\n",y-x+1-query(x,y,1,n,1));
update(x,y,1,1,n,1);
}
}
printf("\n");
}
return 0;
}