poj 3667 分类: poj templates 2015-03-30 22:13 31人阅读 评论(0) 收藏
线段树区间合并什么的。。。
#include<map>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
#include<iostream>
#include<algorithm>
const int MAXN = 50005;
int n , m;
struct treenode
{
int lmax,rmax,mmax;
int col;
}tree[MAXN<<2] = {0};
#define L(x) (x<<1)
#define R(x) ((x<<1)|1)
void pushdown(int x,int ll,int rr)
{
if(tree[x].col != -1)
{
int mid = (ll+rr)>>1;
tree[L(x)].lmax = tree[L(x)].rmax = tree[L(x)].mmax = (mid-ll+1)*(tree[x].col^1);
tree[R(x)].lmax = tree[R(x)].rmax = tree[R(x)].mmax = (rr - mid)*(tree[x].col^1);
tree[L(x)].col = tree[R(x)].col = tree[x].col;
tree[x].col = -1;
}
}
void update(int x,int ll,int rr)
{
int mid = (ll+rr)>>1;
tree[x].mmax = std::max(std::max(tree[L(x)].mmax,tree[R(x)].mmax),tree[L(x)].rmax + tree[R(x)].lmax);
tree[x].lmax = tree[L(x)].lmax + ((tree[L(x)].lmax == mid-ll+1)?tree[R(x)].lmax:0);
tree[x].rmax = tree[R(x)].rmax + ((tree[R(x)].rmax == rr - mid)?tree[L(x)].rmax:0);
}
void color(int l,int r,int ll,int rr,int si,int col)
{
if(ll == l && rr == r)
{
tree[si].col = col;
tree[si].mmax = (rr-ll+1)*(col^1);
tree[si].lmax = tree[si].rmax = tree[si].mmax ;
}
else
{
int mid = (ll+rr)>>1;
pushdown(si,ll,rr);
if(r<=mid) color(l,r,ll,mid,L(si),col);
else if(l>mid)color(l,r,mid+1,rr,R(si),col);
else
{
color(l,mid,ll,mid,L(si),col);
color(mid+1,r,mid+1,rr,R(si),col);
}
update(si,ll,rr);
}
}
int query(int ll,int rr,int si,int len)
{
int mid = (ll+rr)>>1;
pushdown(si,ll,rr);
if(len <= tree[L(si)].mmax)
return query(ll,mid,L(si),len);
else if(len <= tree[L(si)].rmax + tree[R(si)].lmax)
return mid - tree[L(si)].rmax + 1;
else
return query(mid+1,rr,R(si),len);
}
#undef L
#undef R
int main()
{
#ifndef ONLINE_JUDGE
freopen("poj3667.in","r",stdin);
freopen("poj3667.out","w",stdout);
#endif
scanf("%d%d",&n,&m);
tree[1].col = 0 , tree[1].lmax = tree[1].rmax = tree[1].mmax = n;
while(m--)
{
int tag;scanf("%d",&tag);
if(tag == 1)
{
int len; scanf("%d",&len);
if(len<=tree[1].mmax)
{
int st = query(1,n,1,len);
printf("%d\n",st);
color(st,st+len-1,1,n,1,1);
}
else
puts("0");
}
else
{
int a,b;scanf("%d%d",&a,&b);
color(a,a+b-1,1,n,1,0);
}
}
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。