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.
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
3 7 2 1 9 4 Can not put any one. 2 6 2 0 9 4 4 5 2 3
题解:
题意就是有n个花瓶,一开始全空,然后又m次操作,如果第一个输入的为1,然后输入两个数x,y,就是将x,y区间内插花,如果有花了就跳掉往后插,结束的条件是插到末尾了或者插够了数量,如果一个都不能插就骂人qwq,如果输入2,输入x,y就是将x,y内的花清空
这题思路就是普通的lazy tag区间更新+二分查找合适的位置插
代码:
#include<iostream>
#include<stdio.h>
#include<map>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include<string>
#include<cstring>
using namespace std;
struct node
{
int l,r;
int tag;//如果为-1则为初始状态,如果为0意思将子区间内的花清空,为1为插满花
int v;//记录区间内插了多少花
}t[50005*4];
int n;
void Build(int l,int r,int k)
{
t[k].l=l;
t[k].r=r;
t[k].v=0;
t[k].tag=-1;
if(l==r)
return;
int mid=(l+r)/2;
Build(l,mid,k*2);
Build(mid+1,r,k*2+1);
}
void pushdown(int k)//向下传递状态
{
t[k*2].tag=t[k*2+1].tag=t[k].tag;
t[k*2].v=t[k].tag*(t[k*2].r-t[k*2].l+1);
t[k*2+1].v=t[k].tag*(t[k*2+1].r-t[k*2+1].l+1);
t[k].tag=-1;
}
void update(int l,int r,int v,int k)
{
if(t[k].l==l&&t[k].r==r)
{
t[k].tag=v;
t[k].v=v*(r-l+1);//插满或者清空
return;
}
if(t[k].tag!=-1)//要传递状态
pushdown(k);
int mid=(t[k].l+t[k].r)/2;
if(r<=mid)
update(l,r,v,k*2);
else if(l>mid)
update(l,r,v,k*2+1);
else
{
update(l,mid,v,k*2);
update(mid+1,r,v,k*2+1);
}
t[k].v=t[k*2].v+t[k*2+1].v;//向上传递区间信息
}
int query(int l,int r,int k)//查询区间内插花的数目
{
if(t[k].l==l&&t[k].r==r)
{
return t[k].v;
}
if(t[k].tag!=-1)//同样lazy tag
pushdown(k);
int mid=(t[k].l+t[k].r)/2;
if(r<=mid)
return query(l,r,k*2);
else if(l>mid)
return query(l,r,k*2+1);
else
{
return query(l,mid,k*2)+query(mid+1,r,k*2+1);
}
t[k].v=t[k*2].v+t[k*2+1].v;
}
int dive(int s,int num)//二分查找,第一个为开始的位置,第二个参数为要插多少个
{
int temp=query(s,n,1);
if(temp==n-s+1)//如果一个都不能插
return -1;
if(n-s+1-temp<num)//如果空位小于要插的数目,改下要插的数目
num=n-s+1-temp;
int l=s;
int r=n;
int mid,d;
int f=-1;
while(r>=l)//二分
{
mid=(l+r)/2;
d=mid-s+1-query(s,mid,1);//d为从s到mid的空位数目
if(d>num)
{
r=mid-1;
}
else if(d<num)
l=mid+1;
else//如果空位的数目正好要一个个减
{
f=mid;//为了保存第一个能够满足该数目空位的位置
r=mid-1;
}
}
return f;
}
int main()
{
int i,j,test,x,y,d,m;
scanf("%d",&test);
while(test--)
{
scanf("%d%d",&n,&m);
n--;
Build(0,n,1);
for(i=0;i<m;i++)
{
scanf("%d%d%d",&d,&x,&y);
if(d==1)
{
int temp=dive(x,1);//查找插的第一个位置
if(temp==-1)
{
printf("Can not put any one.\n");
}
else
{
int en=dive(x,y);//查找插的第二个位置
printf("%d %d\n",temp,en);
update(temp,en,1,1);//更新插满区间
}
}
else
{
printf("%d\n",query(x,y,1));
update(x,y,0,1);//清空区间
}
}
printf("\n");
}
return 0;
}