Meaning
Inventor Shtsc, who once invented the automatic brush machine, also released his new invention: a brain-hole therapeutic instrument-a mystical device that could cure his growing brain hole because of his invention.
For simplicity's sake, we see the brain as a 01 sequence. 1 represents the brain tissue of this position to work normally, 0 represents this is a brain hole.
1 0 1 0 0 0 1 1 1 0
The basic principle of repairing a brain hole with a brain-hole therapeutic instrument is to dig out another contiguous area and fill the brain hole with the normal working brain tissue.
(So the brain-hole therapeutic instrument is a therapeutic instrument for brain cavities.) )
For example, use the 8th position above to the 10th position to repair the brain hole at position 1th to 4th. We will get:
1 1 1 1 0 0 1 0 0 0
If you use position 1th to the 4th position to repair the 8th position to the 10th position:
0 0 0 0 0 0 1 1 1 1
This is because the brain-hole therapeutic apparatus throws away unwanted brain tissue directly.
If you use position 7th to the 10th position to fill the 1th position to the 6th position:
1 1 1 1 0 0 0 0 0 0
This is because if the new brain hole dug out of the brain tissue is not enough, the brain hole therapy will only try to fill the position of the anterior brain hole.
Assuming that the initial SHTSC does not have a brain hole, give a number of brain-digging and brain-hole treatment sequence, you need to answer the SHTSC question immediately:
How large is the largest contiguous area of the brain in a certain interval.
Analysis of N,m <=200000,1<=l<=r<=n
Directly on the segment tree can be. Code
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <
Algorithm> using namespace std;
const int n=200005;
int n,m; struct Tree{int s0,s1,ls,rs,tag,mx;}
T[N*5];
int read () {int X=0,f=1;char ch=getchar (); while (ch< ' 0 ' | |
Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();}
while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();}
return x*f; } void pushdown (int d,int l,int r) {if l==r| |
T[d].tag==-1) return;
int mid= (L+R)/2;
if (!t[d].tag) {t[d*2].tag=t[d*2+1].tag=t[d*2].s1=t[d*2+1].s1=0;
t[d*2].s0=t[d*2].ls=t[d*2].rs=t[d*2].mx=mid-l+1;
T[d*2+1].s0=t[d*2+1].ls=t[d*2+1].rs=t[d*2+1].mx=r-mid;
else {t[d*2].tag=t[d*2+1].tag=1;
T[d*2].s1=mid-l+1;t[d*2+1].s1=r-mid;
t[d*2].s0=t[d*2].ls=t[d*2].rs=t[d*2].mx=0;
t[d*2+1].s0=t[d*2+1].ls=t[d*2+1].rs=t[d*2+1].mx=0;
} t[d].tag=-1; Tree merge (tree X,tree y) {u;u.tag=-1;
U.S0=X.S0+Y.S0;U.S1=X.S1+Y.S1;
U.ls=x.ls==x.s0+x.s1?x.ls+y.ls:x.ls;
u.rs=y.rs==y.s0+y.s1?y.rs+x.rs:y.rs;
U.mx=max (Max (x.mx,y.mx), x.rs+y.ls);
return u;
} void Build (int d,int l,int r) {t[d].s1=r-l+1;t[d].tag=-1;
if (l==r) return;
int mid= (L+R)/2;
Build (D*2,l,mid), build (D*2+1,MID+1,R);
} void Modify (int d,int l,int r,int x,int y,int z) {if (x>y) return;
Pushdown (D,L,R);
if (l==x&&r==y) {if (!z) t[d].tag=0,t[d].mx=t[d].ls=t[d].rs=t[d].s0=r-l+1,t[d].s1=0;
else t[d].tag=1,t[d].mx=t[d].ls=t[d].rs=t[d].s0=0,t[d].s1=r-l+1;
Return
int mid= (L+R)/2;
Modify (D*2,l,mid,x,min (y,mid), z);
Modify (D*2+1,mid+1,r,max (x,mid+1), y,z);
T[d]=merge (t[d*2],t[d*2+1]);
int get_s0 (int d,int l,int r,int x,int y) {if (x>y) return 0;
Pushdown (D,L,R);
if (l==x&&r==y) return t[d].s0;
int mid= (L+R)/2; Return Get_s0 (D*2,l,mid,x,min (Y,mid)) +get_s0 (D*2+1,mid+1,r,max (x,mid+1),y);
int get_s1 (int d,int l,int r,int x,int y) {if (x>y) return 0;
Pushdown (D,L,R);
if (l==x&&r==y) return t[d].s1;
int mid= (L+R)/2;
Return Get_s1 (D*2,l,mid,x,min (Y,mid)) +get_s1 (D*2+1,mid+1,r,max (x,mid+1), y);
int get_p (int d,int l,int r,int k) {if (l==r) return l;
Pushdown (D,L,R);
int mid= (L+R)/2;
if (t[d*2].s0>=k) return get_p (D*2,L,MID,K);
else return get_p (D*2+1,MID+1,R,K-T[D*2].S0);
Tree query (int d,int l,int r,int x,int y) {pushdown (d,l,r);
if (l==x&&r==y) return t[d];
int mid= (L+R)/2;
if (y<=mid) return query (d*2,l,mid,x,y);
else if (x>mid) return query (d*2+1,mid+1,r,x,y);
else return merge (query (D*2,L,MID,X,MID), query (d*2+1,mid+1,r,mid+1,y);
int main () {n=read (); M=read ();
Build (1,1,n);
while (m--) {int op=read ();
if (!op) {int l=read (), R=read ();
Modify (1,1,n,l,r,0);
else if (op==1){int L0=read (), R0=read (), L1=read (), R1=read ();
int s1=get_s1 (1,1,N,L0,R0); modify (1,1,n,l0,r0,0);
int S0=get_s0 (1,1,n,1,l1-1), P=get_p (1,1,n,min (S0+s1,get_s0 (1,1,N,1,R1)));
if (min (S0+s1,get_s0 (1,1,N,1,R1))) modify (1,1,n,l1,p,1);
else {int L=read (), R=read ();
printf ("%d\n", Query (1,1,n,l,r). mx);
} return 0; }