Vases and Flowers
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 3744 Accepted Submission(s): 1516
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).
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.
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
题目大意:
现在有N个花瓶,一开始每个花瓶都是空的,现在有两种操作:
①1 x y,表示以x号花瓶作为起点,让我们往右插y朵花,如果一个瓶子已经有花了,我们跳过这个花瓶,输出第一个插花的位子,和最后一个插花的位子。
②2 x y,让我们先输出从x到y号花瓶中,一共有多少朵花,然后再清空所有花瓶。
思路:
对于第二种操作,我们直接区间查询然后区间更新即可。
对于第一种操作:
①我们首先二分出第一个插花的位子
②然后再二分最远能够插到的花的位子
③最后再二分最后一个插花的位子即可。
题目不难,用于练习线段树区间更新。
Ac代码:
#include<stdio.h>
#include<string.h>
using namespace std;
/****************************/
#define lson l,m,rt*2
#define rson m+1,r,rt*2+1
int tree[50050*4];
int flag[50050*4];
void pushup(int rt)
{
tree[rt]=tree[rt*2]+tree[rt*2+1];
}
void pushdown(int l,int r,int rt)
{
if(flag[rt])
{
int m=(l+r)/2;
flag[rt*2]=flag[rt];
flag[rt*2+1]=flag[rt];
tree[rt*2]=(m-l+1)*flag[rt];
tree[rt*2+1]=(r-(m+1)+1)*flag[rt];
if(flag[rt]==-1)tree[rt*2]=tree[rt*2+1]=0;
flag[rt]=0;
}
}
void build(int l,int r,int rt)
{
tree[rt]=flag[rt]=0;
if(l==r)return ;
int m=(l+r)/2;
build(lson);build(rson);pushup(rt);
}
void update(int L,int R,int c,int l,int r,int rt)
{
if(l>=L&&r<=R)
{
if(c==1)tree[rt]=(r-l+1),flag[rt]=1;
else tree[rt]=0,flag[rt]=-1;
return ;
}
pushdown(l,r,rt);
int m=(l+r)/2;
if(L<=m)update(L,R,c,lson);
if(R>m)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 tree[rt];
}
int ans=0;
pushdown(l,r,rt);
int m=(l+r)/2;
if(L<=m)ans+=query(L,R,lson);
if(R>m)ans+=query(L,R,rson);
// printf("%d %d %d\n",l,r,ans);
pushup(rt);
return ans;
}
/****************************/
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m;scanf("%d%d",&n,&m);
build(1,n,1);
while(m--)
{
int op;
int l,r;
scanf("%d%d%d",&op,&l,&r);
l++,r++;
if(op==1)
{
r--;
int left=l;
int right=n;
int pos=-1;
while(right-left>=0)
{
int mid=(left+right)/2;
if(query(l,mid,1,n,1)==mid-l+1)left=mid+1;
else
{
right=mid-1;
pos=mid;
}
}
if(pos==-1)printf("Can not put any one.\n");
else
{
int ans=-1;
left=pos;
right=n;
while(right-left>=0)
{
int mid=(left+right)/2;
if(mid-pos+1-query(pos,mid,1,n,1)<=r)
{
left=mid+1;
ans=mid;
}
else right=mid-1;
}
int realans=-1;
left=pos;
right=ans;
while(right-left>=0)
{
int mid=(left+right)/2;
if(ans-mid+1-query(mid,ans,1,n,1)==0)
{
right=mid-1;
realans=mid;
}
else left=mid+1;
}
if(realans==-1)
printf("%d %d\n",pos-1,ans-1);
else printf("%d %d\n",pos-1,realans-2);
update(pos,ans,1,1,n,1);
}
}
if(op==2)
{
printf("%d\n",query(l,r,1,n,1));
update(l,r,-1,1,n,1);
}
}
printf("\n");
}
}