Vases and Flowers
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1466 Accepted Submission(s): 579
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]
Source
2013 Multi-University Training Contest 2
很好的一个线段树,区间更新,区间查询,主要一点,要二分找到第一个插的瓶子也就是第一个为0的瓶子,和最后一个插的瓶子也就是第k个为0的瓶子,这样,就可以了!
#include
#include
#include
using namespace std;
#define MAXN 50050
int l[MAXN*20],color[MAXN*20],upc[MAXN*20];
void pushup(int num,int s,int e)
{
if(color[num])
{
color[num<<1]=color[num<<1|1]=color[num];
upc[num<<1]=upc[num<<1|1]=upc[num];
color[num]=0;
l[num<<1]=upc[num]*(((s+e)>>1)-s+1);
l[num<<1|1]=upc[num]*(e-((s+e)>>1));
}
}
int update(int num,int s,int e,int a,int b,int c)
{
pushup(num,s,e);
if(a<=s&&b>=e)
{
color[num]=1;
upc[num]=c;
l[num]=c*(e-s+1);
return 1;
}
int mid=(s+e)>>1;
if(a<=mid)update(num<<1,s,mid,a,b,c);
if(b>mid)update(num<<1|1,mid+1,e,a,b,c);
l[num]=l[num<<1]+l[num<<1|1];
}
int query(int num,int s,int e,int a,int b)
{
pushup(num,s,e);
if(a<=s&&b>=e)
return l[num];
int mid=(s+e)>>1,ans=0;
if(a<=mid)ans+=query(num<<1,s,mid,a,b);
if(b>mid)ans+=query(num<<1|1,mid+1,e,a,b);
l[num]=l[num<<1]+l[num<<1|1];
return ans;
}
int twocut(int index,int f,int n)
{
int s,e,k,mid;
s=index,e=n;
k=e-s+1-query(1,1,n,s,e);
if(k==0)
{
printf("Can not put any one.\n");
return -1;
}
if(f>k)
f=k;
s=index,e=n;
while(s>1;
k=mid-index+1-query(1,1,n,index,mid);
if(k>=1)
e=mid;
else
s=mid+1;
}
printf("%d ",s-1);
int ss=s;
s=index,e=n;
while(s>1;
k=mid-index+1-query(1,1,n,index,mid);
if(k>=f)
e=mid;
else
s=mid+1;
}
printf("%d\n",s-1);
update(1,1,n,ss,s,1);
return -1;
}
int main()
{
int tcase,n,m,i,temp,a,b;
scanf("%d",&tcase);
while(tcase--)
{
scanf("%d%d",&n,&m);
memset(l,0,sizeof(l));
memset(color,0,sizeof(color));
for(i=0;i