HDU4614-Vases and Flowers(线段树+二分)

错在一个细节上,卡了好久。


题目:
 Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to N-1. When she receive some flowers, she will try to put them in the vases, one flower in one vase. She randomly choose the vase A and try to put a flower in the vase. If the there is no flower in the vase, she will put a flower in it, otherwise she skip this vase. And then she will try put in the vase A+1, A+2, …, N-1, until there is no flower left or she has tried the vase N-1. The left flowers will be discarded. Of course, sometimes she will clean the vases. Because there are too many vases, she randomly choose to clean the vases numbered from A to B(A <= B). The flowers in the cleaned vases will be discarded.
Input
  The first line contains an integer T, indicating the number of test cases.
  For each test case, the first line contains two integers N(1 < N < 50001) and M(1 < M < 50001). N is the number of vases, and M is the operations of Alice. Each of the next M lines contains three integers. The first integer of one line is K(1 or 2). If K is 1, then two integers A and F follow. It means Alice receive F flowers and try to put a flower in the vase A first. If K is 2, then two integers A and B follow. It means the owner would like to clean the vases numbered from A to B(A <= B).
Output
  For each operation of which K is 1, output the position of the vase in which Alice put the first flower and last one, separated by a blank. If she can not put any one, then output ‘Can not put any one.’. For each operation of which K is 2, output the number of discarded flowers.
  Output one blank line after each test case.
Sample Input
2
10 5
1 3 5
2 4 5
1 1 8
2 3 6
1 8 8
10 6
1 2 5
2 3 4
1 0 8
2 2 5
1 4 4
1 2 3
Sample Output
3 7
2
1 9
4
Can not put any one.

2 6
2
0 9
4
4 5
2 3

题目大意:
有n个花瓶,标号0 ~ n−1。m个操作,
‘1 A F′,表示从A位置开始插F朵花,遇到有花的花瓶跳过。到最后一个花瓶都还有花剩余,丢弃剩下的花。
‘2 A B′,表示将区间[A,B]内的花瓶全部清空。(A≤B)
对于每个1操作,输出第一个和最后一个插花的位置,如果一朵花都插不了,输出‘Can not put any one.’;对于每个2操作,输出区间[A,B]内被清空的花瓶的数量。

数据范围:
1<n<5001,1<m<5001

#pragma GCC optimize(2)
#include<bits/stdc++.h> 
using namespace std;
typedef long long ll;
#define ls		i<<1
#define rs		i<<1|1
#define lson	ls,l,m
#define rson	rs,m+1,r
#define ln		m-l+1
#define rn		r-m
const int maxn=5e4+7;
const int mod=1e9+7;
int read(){
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+ch-'0';
        ch=getchar();
    }
    return x*f;
}
int num[maxn<<2],n,p,f[maxn<<2],T,choice,t;

void pushup(int i){	num[i]=num[ls]+num[rs];	}
void pushdown(int i,int l,int r){
	if(f[i]!=-1){
		int m=l+r>>1;
		f[ls]=f[rs]=f[i];
		num[ls]=f[i]*(ln);	num[rs]=f[i]*(rn);		//卡在这好久,没加括号
		f[i]=-1;
	}
}
void build(int i,int l,int r){
	num[i]=r-l+1;	f[i]=-1;
	if(l==r)	return;
	int m=l+r>>1;
	build(lson);	build(rson);
}
void update(int i,int l,int r,int x,int y,int c){
	if(x<=l&&r<=y){
		num[i]=c*(r-l+1);
		f[i]=c;
		return;
	}
	if(l==r)	return;
	pushdown(i,l,r);
	int m=l+r>>1;
	if(x<=m)	update(lson,x,y,c);
	if(y>m)		update(rson,x,y,c);
	pushup(i);
}
int query(int i,int l,int r,int x,int y){
	if(y<l||x>r)	return 0;
	if(x<=l&&r<=y)	return num[i];
	pushdown(i,l,r);
	int m=l+r>>1;
	int ans=0;
	if(x<=m)	ans+=query(lson,x,y);
	if(y>m)		ans+=query(rson,x,y);
	return ans;
}
int search(int x,int k){
	int l=x,r=n,ans=0;
	while(l<=r){
		int m=l+r>>1;
		if(query(1,1,n,x,m)>=k){
			ans=m;
			r=m-1;
		}
		else	l=m+1;
	}
	return ans;
}
int main(){
	T=read();
	while(T--){
		cin>>n>>p;
		build(1,1,n);
		int x,y;
		while(p--){
			choice=read();	x=read();	y=read();
			if(choice==1){
				x++;
				t=query(1,1,n,x,n);
//				cout<<"kkkkkkkkkk        "<<t<<endl;
				if(t==0){
					printf("Can not put any one.\n");
					continue;
				}
				int L=search(x,1);
				int R=search(x,min(t,y));
				update(1,1,n,L,R,0);
				printf("%d %d\n",L-1,R-1);
			}
			else if(choice==2){
				x++,y++;
				t=query(1,1,n,x,y);
//				cout<<"lllllllllllll     "<<t<<endl;
				printf("%d\n",y-x+1-t);
				update(1,1,n,x,y,1);
			}
		}
		putchar('\n');
	}
}

/*
2
10 5
1 3 5
2 4 5
1 1 8
2 3 6
1 8 8
10 6
1 2 5
2 3 4
1 0 8
2 2 5
1 4 4
1 2 3

3 7
2
1 9
4
Can not put any one.

2 6
2
0 9
4
4 5
2 3
*/
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值