Vases and Flowers
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 4851 Accepted Submission(s): 1999
Problem Description
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
[pre]
3 7
2
1 9
4
Can not put any one.
2 6
2
0 9
4
4 5
2 3
[/pre]
Author
SYSU
Source
2013 Multi-University Training Contest 2
Recommend
zhuyuanchen520 | We have carefully selected several similar problems for you: 6447 6446 6445 6444 6443
可能是自己线段树太弱了吧,所以这道题敲完了一直找WA点,没想到是在query的地方写错了,所以A的这一刻都有点激动的想哭。。。
题意:
两种操作,1:把c朵花,从b开始一个位置放一瓶,若该位置有花则跳过,每次输出插入花朵的起点的终点。若没有位置可以插花,则输出Cannotputanyone.。如果插不了c支,不用管。
2:把a到b位置的花瓶清空,并输出之前这个区间内有多少朵花。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define lson id*2,l,(l+r)/2
#define rson id*2+1,(l+r)/2+1,r
const int inf=0x3f3f3f3f;
const int maxn=50000+100;
int lazy[maxn*4];///lazy==1表示都可以插。lazy==0表示都不能插。lazy=-1,表示未被更新
int sum[maxn*4];
int n,m;
void build(int id,int l,int r){
sum[id]=r-l+1;
lazy[id]=-1;
if(l==r){
sum[id]=1;
lazy[id]=1;
return ;
}
build(lson);
build(rson);
sum[id]=sum[id*2]+sum[id*2+1];
}
void pushdown(int id,int m){
if(lazy[id]!=-1){
lazy[id*2]=lazy[id*2+1]=lazy[id];
sum[id*2]=lazy[id]*(m-m/2);
sum[id*2+1]=lazy[id]*(m/2);
lazy[id]=-1;
}
}
void update(int id,int l,int r,int w,int L,int R){
if(L<=l && R>=r){
lazy[id]=w;
sum[id]=w*(r-l+1);
return ;
}
pushdown(id,r-l+1);
if(L<=(l+r)/2){
update(lson,w,L,R);
}
if(R>(l+r)/2){
update(rson,w,L,R);
}
sum[id]=sum[id*2]+sum[id*2+1];
}
int query(int id,int l,int r,int L,int R){
if(L<=l && R>=r){
return sum[id];
}
pushdown(id,r-l+1);
if(L<=l && R>=r){
return sum[id];
}
int ans=0;
if(L<=(l+r)/2){
ans+=query(lson,L,R);
}
if(R>(l+r)/2)
ans+=query(rson,L,R);
return ans;
}
int main()
{
int t,a,b,c;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
build(1,1,n);
for(int i=1;i<=m;i++){
scanf("%d%d%d",&a,&b,&c);
int ans1,ans2;
if(a==1){///从b开始放c朵花
b++;
if(query(1,1,n,b,n)==0){
printf("Can not put any one.\n");
}
else{
int mid;
int high=n;
int low=b;
ans1;///二分法求小的
while(low<=high){
mid=(high+low)/2;
int tmp=query(1,1,n,b,mid);
/// cout<<"n:"<<n<<" b:"<<b<<" mid:"<<mid<<endl;
/// cout<<"tmp:"<<tmp<<endl;
if(tmp>0){///说明取值靠右了,偏大.而且必须要大于0才能插花
ans1=mid;
high=mid-1;
/// cout<<"ans1:"<<ans1<<endl;
}
else
low=mid+1;
}
ans2=n;
low=b;high=n;
int tmp=query(1,1,n,b,n);
/// cout<<"TMP:"<<tmp<<endl;
/// cout<<"b:"<<b<<" n:"<<n<<endl;
if(tmp<c){
c=tmp;
}
/// cout<<"c:"<<c<<endl;
///二分法求大的
while(low<=high){
mid=(high+low)/2;
int tmp=query(1,1,n,b,mid);
if(tmp>=c){///说明tmp取大了,但是等于c的时候是可以取值的,所以是>=
ans2=mid;
high=mid-1;
}
else
low=mid+1;
}
update(1,1,n,0,ans1,ans2);
printf("%d %d\n",ans1-1,ans2-1);
}
}
else{///看清空几个
b++,c++;
/// cout<<"查询:"<<query(1,1,n,b,c)<<endl;
int tmp=c-b+1-query(1,1,n,b,c);
printf("%d\n",tmp);
update(1,1,n,1,b,c);///将b-c中的值变成1,可以插1
}
}
cout<<endl;
}
}
/*
2
10 10
1 0 9
1 0 9
1 0 9
1 1 1
1 2 2
2 0 9
1 1 3
1 1 2
*/
开始错误点:
query里写错了:
int query(int id,int l,int r,int L,int R){
if(lazy[id]!=-1){
if(lazy[id]==1){
return r-l+1;
}
else return 0;
}
pushdown(id,r-l+1);
if(L<=l && R>=r){
return sum[id];
}
int ans=0;
if(L<=(l+r)/2){
ans+=query(lson,L,R);
}
if(R>(l+r)/2)
ans+=query(rson,L,R);
return ans;
}
应该判断当前区间在不在所查区间里的,而不是直接这样。。。
正确:
int query(int id,int l,int r,int L,int R){
if(L<=l && R>=r){
return sum[id];
}
pushdown(id,r-l+1);
if(L<=l && R>=r){
return sum[id];
}
int ans=0;
if(L<=(l+r)/2){
ans+=query(lson,L,R);
}
if(R>(l+r)/2)
ans+=query(rson,L,R);
return ans;
}