Vases and Flowers
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)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]
题目大意:1~n区间,1表示花瓶为空,0表示花瓶有花,:给定一个区间[0,N-1],初始时每个位置上的数字都是0,有两种操作,1.在位置A开始寻找F(如果没有这么多,则有多少个就找多少个)个数值为0的位置,把位置上的数修改为1,并返回第一个和最后一个修改的位置。2.查询区间[A,B]内1的个数,并把区间[A,B]每个位置上的数修改为0。
解题思路:用线段树维护区间和,二分查找区间最后一个1
Ps: kuangbin 的代码好工整,实在需要好好学学;
代码:
#include <iostream>
#include <stdio.h>
using namespace std;
const int maxn=50010;
struct node
{
int sum;
int first,last;
int lazy;
int l,r;
}tree[maxn*3];
void pushup(int root)
{
if(tree[root].l==tree[root].r) return;
tree[root].sum=tree[root<<1].sum+tree[root<<1|1].sum;
if(tree[root<<1].first!=-1) tree[root].first=tree[root<<1].first;
else tree[root].first=tree[root<<1|1].first;
if(tree[root<<1|1].last!=-1) tree[root].last=tree[root<<1|1].last;
else tree[root].last=tree[root<<1].last;
}
void pushdown(int root)
{
if(tree[root].l==tree[root].r) return;
if(tree[root].lazy==1)
{
tree[root<<1].first=tree[root<<1].l;
tree[root<<1].last=tree[root<<1].r;
tree[root<<1].sum=tree[root<<1].r-tree[root<<1].l+1;
tree[root<<1].lazy=1;
tree[root<<1|1].first=tree[root<<1|1].l;
tree[root<<1|1].last=tree[root<<1|1].r;
tree[root<<1|1].sum=tree[root<<1|1].r-tree[root<<1|1].l+1;
tree[root<<1|1].lazy=1;
}
if(tree[root].lazy==-1)
{
tree[root<<1].first=-1;
tree[root<<1].last=-1;
tree[root<<1].sum=0;
tree[root<<1].lazy=-1;
tree[root<<1|1].first=-1;
tree[root<<1|1].last=-1;
tree[root<<1|1].sum=0;
tree[root<<1|1].lazy=-1;
}
tree[root].lazy=0;
}
void build(int root,int l,int r)
{
tree[root].l=l;
tree[root].r=r;
tree[root].sum=r-l+1;
tree[root].lazy=0;
tree[root].first=l;
tree[root].last=r;
if(l==r) return;
int mid=(l+r)/2;
build(root<<1,l,mid);
build(root<<1|1,mid+1,r);
}
void update(int root,int l,int r,int type)
{
if(tree[root].l==l&&tree[root].r==r)
{
if(type==0)
{
if(tree[root].sum==0) return;
tree[root].first=-1;
tree[root].last=-1;
tree[root].sum=0;
tree[root].lazy=-1;
return;
}
else if(type==1)
{
if(tree[root].sum==tree[root].r-tree[root].l+1) return;
tree[root].first=tree[root].l;
tree[root].last=tree[root].r;
tree[root].sum=tree[root].r-tree[root].l+1;
tree[root].lazy=1;
return;
}
}
pushdown(root);
int mid=(tree[root].l+tree[root].r)>>1;
if(r<=mid) update(root<<1,l,r,type);
else if(mid<l) update(root<<1|1,l,r,type);
else
{
update(root<<1,l,mid,type);
update(root<<1|1,mid+1,r,type);
}
pushup(root);
}
int n,m;
int query1(int root,int l,int r)
{
if(tree[root].l==l&&tree[root].r==r)
{
return tree[root].first;
}
pushdown(root);
int mid=(tree[root].l+tree[root].r)>>1;
if(mid>=r) return query1(root<<1,l,r);
else if(mid<l) return query1(root<<1|1,l,r);
else
{
int ans1;
ans1=query1(root<<1,l,mid);
if(ans1!=-1) return ans1;
return query1(root<<1|1,mid+1,r);
}
}
int query2(int root,int l,int r)
{
if(tree[root].l==l&&tree[root].r==r)
return tree[root].last;
pushdown(root);
int mid=(tree[root].l+tree[root].r)>>1;
if(mid>=r) return query2(root<<1,l,r);
else if(mid<l) return query2(root<<1|1,l,r);
else
{
int ans=query2(root<<1|1,mid+1,r);
if(ans!=-1) return ans;
return query2(root<<1,l,mid);
}
}
int sum(int root,int l,int r)
{
if(tree[root].l==l&&tree[root].r==r)
return tree[root].sum;
pushdown(root);
int mid=(tree[root].l+tree[root].r)>>1;
if(mid>=r) return sum(root<<1,l,r);
else if(mid<l) return sum(root<<1|1,l,r);
else return sum(root<<1,l,mid)+sum(root<<1|1,mid+1,r);
}
int search(int A,int F)
{
if(sum(1,A,n)==0) return -1;
if(sum(1,A,n)<F) return n;
int l=A;
int r=n;
int ans=A;
while(l<=r)
{
int mid=(l+r)>>1;
if(sum(1,A,mid)>=F)
{
ans=mid;
r=mid-1;
}
else l=mid+1;
}
return ans;
}
int main()
{
int T;
cin>>T;
while(T--)
{
scanf("%d%d",&n,&m);
build(1,1,n);
int op,u,v;
while(m--)
{
scanf("%d%d%d",&op,&u,&v);
if(op==1)
{
u++;
int t=search(u,v);
//cout<<t<<endl;
if(t==-1)
{
printf("Can not put any one.\n");
continue;
}
printf("%d %d\n",query1(1,u,t)-1,query2(1,u,t)-1);
update(1,u,t,0);
}
else
{
u++,v++;
printf("%d\n",v-u+1-sum(1,u,v));
update(1,u,v,1);
}
}
printf("\n");
}
return 0;
}