poj 3667


线段树区间合并什么的。。。


#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值