#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);
}
}
}
poj3911
最新推荐文章于 2016-09-20 19:36:06 发布