poj3911

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
#define L(x) (x<<1)
#define R(x) (x<<1 |1)
#define MAX 100010
using namespace std;
struct node
{
    int l,r,wl,wr,bl,br,blen,wlen;
    bool f;
}a[MAX*3];
int stone[MAX];
void uplen(int t)
{
    int l=a[L(t)].r-a[L(t)].l+1;
    a[t].wl=a[L(t)].wl+(a[L(t)].wl==l?a[R(t)].wl:0);
    a[t].bl=a[L(t)].bl+(a[L(t)].bl==l?a[R(t)].bl:0);
    l=a[R(t)].r-a[R(t)].l+1;
    a[t].wr=a[R(t)].wr+(a[R(t)].wr==l?a[L(t)].wr:0);
    a[t].br=a[R(t)].br+(a[R(t)].br==l?a[L(t)].br:0);
    a[t].blen=max(max(a[L(t)].blen,a[R(t)].blen),a[L(t)].br+a[R(t)].bl);
    a[t].wlen=max(max(a[L(t)].wlen,a[R(t)].wlen),a[L(t)].wr+a[R(t)].wl);
}
void build(int t,int l,int r)
{
    a[t].l=l;
    a[t].r=r;
    a[t].f=0;
    if(l==r)
    {
        a[t].blen=stone[l];
        a[t].wlen=1-a[t].blen;
        a[t].wl=a[t].wlen;
        a[t].wr=a[t].wlen;
        a[t].br=a[t].blen;
        a[t].bl=a[t].blen;
        return ;
    }
    int mid=(l+r)>>1;
    build(L(t),l,mid);
    build(R(t),mid+1,r);
    uplen(t);
}
void sw(int t)
{
    swap(a[t].blen,a[t].wlen);
    swap(a[t].wr,a[t].br);
    swap(a[t].wl,a[t].bl);
}
void setdown(int t)
{
    if(a[t].r==a[t].l)
    {
      a[t].f=!a[t].f;
      return ;
    } 
    if(a[t].f)
    {
        a[L(t)].f=!a[L(t)].f;
        a[R(t)].f=!a[R(t)].f;
        sw(L(t));
        sw(R(t));
        a[t].f=!a[t].f;
    }
}
void update(int t,int l,int r)
{
    setdown(t);
    if(l<=a[t].l&&a[t].r<=r)
    {
        a[t].f=!a[t].f;
        sw(t);
        return ;
    }
    int mid=(a[t].r+a[t].l)>>1;
    if(l<=mid)
      update(L(t),l,r);
    if(r>mid)
      update(R(t),l,r);
    uplen(t);
}
int query(int t,int l,int r)
{
    setdown(t);
    if(l<=a[t].l&&a[t].r<=r)
    {
        return a[t].blen;
    }
    int ans=0;
    int p=0;
    int q=0;
    int mid=(a[t].l+a[t].r)>>1;
    if(l<=mid)
    {
        ans=max(ans,query(L(t),l,r));
        if(r>=a[L(t)].r)
            p=min(a[L(t)].br,a[L(t)].r-l+1);
    }
    if(r>mid)
    {
        ans=max(ans,query(R(t),l,r));
        if(l<=a[R(t)].l)
            q=min(a[R(t)].bl,r-a[R(t)].l+1);
    }
    ans=max(ans,p+q);
    uplen(t);
    return ans;
}
int main()
{
    int n,i,t,x,y,m;
    while(scanf("%d",&n)!=EOF)
    {
        for(i=1;i<=n;i++)
          scanf("%d",&stone[i]);
        build(1,1,n);
        scanf("%d",&m);
        while(m--)
        {
            scanf("%d%d%d",&t,&x,&y);
            if(t==0)
            {
                printf("%d\n",query(1,x,y));
            }
            else
                update(1,x,y);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值