线段树
线段树太渣了,看别人代码恶补下。http://www.cnblogs.com/aukle/archive/2013/07/26/3217639.html
#include
#include
using namespace std;
#define maxn 50010
#define ls (rt<<1)
#define rs (rt<<1|1)
#define mid ((t[rt].l+t[rt].r)>>1)
//注意加括号!!
int n,m;
struct tree
{
int l,r;
int sum;
int lazy;
}t[maxn<<2];
void pushup(int rt)
{
t[rt].sum=t[ls].sum+t[rs].sum;
}
void pushdown(int rt)
{
if(t[rt].lazy==0)
{
t[rt].lazy=-1;
t[ls].sum=0;
t[rs].sum=0;
t[ls].lazy=0;
t[rs].lazy=0;
}
else if(t[rt].lazy==1)
{
t[rt].lazy=-1;
t[ls].sum=t[ls].r-t[ls].l+1;
t[rs].sum=t[rs].r-t[rs].l+1;
t[ls].lazy=1;
t[rs].lazy=1;
}
}
void build(int rt,int l,int r)
{
t[rt].sum=1,t[rt].l=l,t[rt].r=r,t[rt].lazy=-1;
if(l==r)
return;
build(ls,l,mid);
build(rs,mid+1,r);
pushup(rt);
}
int query(int rt,int l,int r)
{
if(t[rt].l==l&&t[rt].r==r)
return t[rt].sum;
pushdown(rt);
if(l>mid)
return query(rs,l,r);
else
if(r<=mid)
return query(ls,l,r);
else
return query(ls,l,mid)+query(rs,mid+1,r);
}
void change(int rt,int l,int r,int val)
{
if(t[rt].l==l&&t[rt].r==r)
{
if(val==0)
t[rt].sum=0;
else
t[rt].sum=r-l+1;
t[rt].lazy=val;
return ;
}
pushdown(rt);
if(l>mid)
change(rs,l,r,val);
else
if(r<=mid)
change(ls,l,r,val);
else
change(ls,l,mid,val),change(rs,mid+1,r,val);
pushup(rt);
}
int main()
{
int t;
int x,a,b;
cin>>t;
while(t--)
{
scanf("%d%d",&n,&m);
build(1,0,n-1);
while(m--)
{
scanf("%d%d%d",&x,&a,&b);
if(x==1)
{
int sum=query(1,a,n-1);
if(sum==0)
printf("Can not put any one.\n");
else
{
if(sum>1;
if(query(1,a,m)>=1)
{
s=m;
r=m-1;
}
else
l=m+1;
}
l=a,r=n-1;
while(l<=r)
{
m=(l+r)>>1;
if(query(1,a,m)>=b)
{
e=m;
r=m-1;
}
else
l=m+1;
}
change(1,s,e,0);
printf("%d %d\n",s,e);
}
}
else
{
printf("%d\n",b-a+1-query(1,a,b));
change(1,a,b,1);
}
}
printf("\n");
}
return 0;
}