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
3 7
2
1 9
4
Can not put any one.
2 6
2
0 9
4
4 5
2 3
题意:
现在要你插花,有n个花瓶,m次操作,初始花瓶中无花,操作有两种方式
操作1:1 a b,从编号为a的花瓶开始插花,共插b朵花,花只能插到无花的花瓶中,如果最后插不完b朵花,剩下的花舍弃掉
操作2:1 a b,把从编号a到编号b的所有花瓶里的花全部清理掉
对于操作1,需要输出开始插花的瓶子编号,和最后插花的瓶子编号
对于操作2,需要输出在a~b中总共清理了多少个花瓶中的花
思路:
1代表未插花,0代表已插花
对于操作2,区间求和,然后总长度-未插花的花瓶数得到答案,更新区间[a,b]
对于操作1,需要先二分找到开始插花的花瓶编号,l=a,r=n,得到st
再二分找到结束插花的花瓶编号,l=st,r=n,得到ed,更新区间[st,ed]
最后提示:看清输入的编号,注意二分的写法
代码:
#include
#include
#include
using namespace std;
const int maxn=50000+10;
struct node
{
int le,ri,sum;//1代表未插花,0代表已插花
int mid()
{
return (le+ri)>>1;
}
} tree[maxn<<2];
int change[maxn<<2];
void Build(int rt,int le,int ri)//建树
{
tree[rt].le=le,tree[rt].ri=ri;
if(le==ri)
{
tree[rt].sum=1;
return ;
}
int mid=tree[rt].mid();
Build(rt<<1,le,mid);
Build(rt<<1|1,mid+1,ri);
tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
}
void Pushdown(int rt)//lazy操作
{
change[rt<<1]=change[rt];
change[rt<<1|1]=change[rt];
tree[rt<<1].sum=change[rt]*(tree[rt<<1].ri-tree[rt<<1].le+1);
tree[rt<<1|1].sum=change[rt]*(tree[rt<<1|1].ri-tree[rt<<1|1].le+1);
change[rt]=-1;
}
void Update(int rt,int le,int ri,int num)//修改
{
if(le<=tree[rt].le&&tree[rt].ri<=ri)
{
change[rt]=num;
tree[rt].sum=num*(tree[rt].ri-tree[rt].le+1);
return ;
}
if(change[rt]!=-1)
Pushdown(rt);
int mid=tree[rt].mid();
if(le<=mid)
Update(rt<<1,le,ri,num);
if(ri>mid)
Update(rt<<1|1,le,ri,num);
tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
}
int Query(int rt,int le,int ri)//查询
{
if(le<=tree[rt].le&&ri>=tree[rt].ri)
return tree[rt].sum;
if(change[rt]!=-1)
Pushdown(rt);
int mid=tree[rt].mid();
if(le>mid)
return Query(rt<<1|1,le,ri);
else if(ri<=mid)
return Query(rt<<1,le,ri);
else
return Query(rt<<1,le,mid)+Query(rt<<1|1,mid+1,ri);
}
int main()
{
int t,n,m;
scanf("%d",&t);
while(t--)
{
memset(change,-1,sizeof(change));
scanf("%d%d",&n,&m);
Build(1,1,n);
int op,x,y;
while(m--)
{
scanf("%d%d%d",&op,&x,&y);
if(op==1)
{
++x;
int l=x,r=n,st=-1,ed;
while(l<=r)
{
int mid=(l+r)>>1;
if(Query(1,x,mid)>=1)
{
st=mid;
r=mid-1;
}
else
l=mid+1;
}
if(st==-1)//一朵花也不能插
{
printf("Can not put any one.\n");
continue;
}
if(Query(1,st,n)//不能插够y朵花
{
l=st,r=n;
while(l<=r)
{
int mid=(l+r)>>1;
if(Query(1,mid,n)>=1)
{
ed=mid;
l=mid+1;
}
else
r=mid-1;
}
}
else//能插够y朵花
{
l=st,r=n,ed=n;
while(l<=r)
{
int mid=(l+r)>>1;
if(Query(1,st,mid)>=y)
{
ed=mid;
r=mid-1;
}
else
l=mid+1;
}
}
printf("%d %d\n",st-1,ed-1);
Update(1,st,ed,0);
}
else
{
++x,++y;
printf("%d\n",y-x+1-Query(1,x,y));
Update(1,x,y,1);
}
}
puts("");
}
return 0;
}