Vases and Flowers
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 728 Accepted Submission(s): 251
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 one blank line after each test case.
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
[pre]3 7 2 1 9 4 Can not put any one. 2 6 2 0 9 4 4 5 2 3 [/pre]
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 50005;
struct node
{
int l,r;
int sum;
int flag;
int mid()
{
return (l+r)>>1;
}
};
node tree[MAXN*4];
inline void pushup(int pos)
{
tree[pos].sum=tree[pos<<1].sum+tree[pos<<1|1].sum;
}
inline void pushdown(int pos)
{
if(tree[pos].flag==1)
{
tree[pos<<1].flag=1;
tree[pos<<1|1].flag=1;
tree[pos<<1].sum=0;
tree[pos<<1|1].sum=0;
tree[pos].flag=-1;
}
else if(tree[pos].flag==0)
{
tree[pos<<1].flag=0;
tree[pos<<1|1].flag=0;
tree[pos<<1].sum=tree[pos<<1].r-tree[pos<<1].l+1;
tree[pos<<1|1].sum=tree[pos<<1|1].r-tree[pos<<1|1].l+1;
tree[pos].flag=-1;
}
}
void build(int l,int r,int pos)
{
tree[pos].l=l;
tree[pos].r=r;
if(l==r)
{
tree[pos].sum=1;
tree[pos].flag=-1;
return ;
}
int mid=tree[pos].mid();
build(l,mid,pos<<1);
build(mid+1,r,pos<<1|1);
pushup(pos);
tree[pos].flag=-1;
}
int query(int l,int r,int pos)
{
if(tree[pos].flag==1)
return 0;
else if(tree[pos].flag==0)
return r-l+1;
if(l==tree[pos].l&&tree[pos].r==r)
{
return tree[pos].sum;
}
int mid=tree[pos].mid();
if(r<=mid)
return query(l,r,pos<<1);
else if(l>mid)
return query(l,r,pos<<1|1);
else
return query(l,mid,pos<<1)+query(mid+1,r,pos<<1|1);
}
void update(int l,int r,int x,int pos)
{
if(tree[pos].l==l&&tree[pos].r==r)
{
tree[pos].flag=x;
if(x==0)
tree[pos].sum=r-l+1;
else
tree[pos].sum=0;
return ;
}
pushdown(pos);
int mid=tree[pos].mid();
if(r<=mid)
update(l,r,x,pos<<1);
else if(l>mid)
update(l,r,x,pos<<1|1);
else
{
update(l,mid,x,pos<<1);
update(mid+1,r,x,pos<<1|1);
}
pushup(pos);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,m;
scanf("%d%d",&n,&m);
build(1,n,1);
int i;
for(i=0;i<m;i++)
{
int k,a,b;
scanf("%d%d%d",&k,&a,&b);
a++;
if(k==2)
b++;
if(k==2)
{
int x=query(a,b,1);
update(a,b,0,1);
printf("%d\n",b-a+1-x);
}
else
{
int l=a,r=n;
int x=query(l,r,1);
//printf("asdf %d\n",x);
if(x==0)
{
printf("Can not put any one.\n");
continue;
}
int m1,m2;
while(l<=r)
{
int mid=(l+r)>>1;
x=query(a,mid,1);
if(x>0)
{
m1=mid;
r=mid-1;
}
else
l=mid+1;
}
l=a;r=n;
x=query(l,r,1);
m2=n;
if(x<b)
b=x;
while(l<=r)
{
int mid=(l+r)>>1;
x=query(a,mid,1);
if(x>=b)
{
m2=mid;
r=mid-1;
}
else
l=mid+1;
}
update(m1,m2,1,1);
printf("%d %d\n",m1-1,m2-1);
}
}
printf("\n");
}
return 0;
}
286



被折叠的 条评论
为什么被折叠?



