subject
N (n < = 1E5) number sequence, each number can only be 0 or 1
M (m < = 1E5) two operations
1 x y means to negate all numbers in [x,y]
The number of longest consecutive 1 in 0 x y query [x,y]
Source of thought
https://ac.nowcoder.com/discuss/206415?type=101&order=0&pos=1&page=1 Jishou University Competition K-baishan tea and red rose
Title Solution
If there is no negation, it is the interval merging problem.
After adding the negation, maintain the quantity of 0 and 1, and exchange when the negation is taken.
Note that when the query spans two segments, ensure that the sum of the maximum values of left and right children does not exceed the bounds of [ql,qr] of the query.
if(ql<=mid&&qr>mid)ans=max(ans,min(mid-ql+1,e[p<<1].r1)+min(qr-mid,e[p<<1|1].l1));
Code
#include<bits/stdc++.h> using namespace std; const int N=1e5+10; int n,m,a[N]; int op,x,y; struct node { int l0,r0,s0,l1,r1,s1; bool tag; }e[5*N]; void pushup(int p,int l,int r) { int mid=(l+r)/2; e[p].l0=e[p<<1].l0; if(e[p<<1].l0==mid-l+1)e[p].l0+=e[p<<1|1].l0; e[p].r0=e[p<<1|1].r0; if(e[p<<1|1].r0==r-mid)e[p].r0+=e[p<<1].r0; e[p].s0=max(e[p<<1].s0,e[p<<1|1].s0); e[p].s0=max(e[p].s0,e[p<<1].r0+e[p<<1|1].l0); e[p].l1=e[p<<1].l1; if(e[p<<1].l1==mid-l+1)e[p].l1+=e[p<<1|1].l1; e[p].r1=e[p<<1|1].r1; if(e[p<<1|1].r1==r-mid)e[p].r1+=e[p<<1].r1; e[p].s1=max(e[p<<1].s1,e[p<<1|1].s1); e[p].s1=max(e[p].s1,e[p<<1].r1+e[p<<1|1].l1); } void build(int p,int l,int r) { e[p].l1=e[p].r1=e[p].s1=0; e[p].l0=e[p].r0=e[p].s0=0; e[p].tag=0; if(l==r) { if(a[l]==1)e[p].l1=e[p].r1=e[p].s1=1; else e[p].l0=e[p].r0=e[p].s0=1; return; } int mid=(l+r)/2; build(p<<1,l,mid); build(p<<1|1,mid+1,r); pushup(p,l,r); } void pushdown(int p,int l,int r) { if(e[p].tag) { swap(e[p<<1].l0,e[p<<1].l1); swap(e[p<<1].r0,e[p<<1].r1); swap(e[p<<1].s0,e[p<<1].s1); swap(e[p<<1|1].l0,e[p<<1|1].l1); swap(e[p<<1|1].r0,e[p<<1|1].r1); swap(e[p<<1|1].s0,e[p<<1|1].s1); e[p<<1].tag^=1; e[p<<1|1].tag^=1; e[p].tag=0; } } void upd(int p,int l,int r,int ql,int qr) { if(ql<=l&&r<=qr) { e[p].tag^=1; swap(e[p].l0,e[p].l1); swap(e[p].r0,e[p].r1); swap(e[p].s0,e[p].s1); return; } int mid=(l+r)/2; pushdown(p,l,r); if(ql<=mid)upd(p<<1,l,mid,ql,qr); if(qr>mid)upd(p<<1|1,mid+1,r,ql,qr); pushup(p,l,r); } int ask(int p,int l,int r,int ql,int qr) { if(ql<=l&&r<=qr)return e[p].s1; pushdown(p,l,r); int ans=0,mid=(l+r)/2; if(ql<=mid)ans=max(ans,ask(p<<1,l,mid,ql,qr)); if(qr>mid)ans=max(ans,ask(p<<1|1,mid+1,r,ql,qr)); if(ql<=mid&&qr>mid)ans=max(ans,min(mid-ql+1,e[p<<1].r1)+min(qr-mid,e[p<<1|1].l1)); return ans; } int main() { while(~scanf("%d",&n)) { for(int i=1;i<=n;++i) scanf("%d",&a[i]); build(1,1,n); scanf("%d",&m); while(m--) { scanf("%d%d%d",&op,&x,&y); if(op==1)upd(1,1,n,x,y); else printf("%d\n",ask(1,1,n,x,y)); } } return 0; }