"bzoj4592" "SHOI2015" "brain Hole therapeutic instrument" "Line Tree" _ Segment Tree

Source: Internet
Author: User
Description, an inventor who invented the automatic brush machine, shtsc his new invention: a brain-hole therapeutic instrument-a mystical device that would 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 working principle of repairing a certain brain cavity 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 the 4th. We will get: 1 1 1 1 0 0 1 0 0 0 if we use position 1th to 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 the 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 compared to the anterior brain hole. Assuming that the initial SHTSC does not have a brain cavity, and gives some sequence of procedures for the treatment of brain and brain holes, you need to answer the SHTSC question immediately: How big is the largest contiguous area of the brain in a certain interval.

Input First line two integer n,m. Indicates that the brain of SHTSC can be divided into n contiguous regions numbered from 1 to N. There are M operations. Each of the following line m lines is one of the following three formats. 0 L R:SHTSC dug a hole in the brain from L to R. 1 l0 r0 L1 R2:SHTSC had a brain-hole treatment, which was used to repair l0-r0 brain holes from L1 to R1 brain tissue. 2 L R:SHTSC asks L to r how big is the largest brain hole in this section. N,m <=200000,1<=l<=r<=n

Output an integer for each query, indicating how large the maximum contiguous area of the brain hole is in the interval.

Sample Input 10 10
0 2 2
0 4 6
0 10 10
2 1 10
1 8 10 1 4
2 1 10
1 1 4 8 10
2 1 10
1 7 10 1 6
2 1 10
Sample Output 3
3
6

6

Exercises

The number of maintenance interval 1, the number of 0, interval length, the longest continuous interval of 0, the longest continuous 0 on the left, the longest continuous 0 on the right.

This resolves the 0,2 operation.

For 1 operations first find that interval, and then two points in the line tree.

Code:

#include <iostream> #include <cstdio> #include <cstring> #define N 200010 using namespace std; struct Use{int s0,s1,sum,len,ls,rs;}
t[n<<2];
int n,m,k,l,r,ll,rr,p[n<<2],num;
  int read () {int x (0);
  Char Ch=getchar (); while (ch< ' 0 ' | |
  Ch> ' 9 ') Ch=getchar ();
  while (ch>= ' 0 ' &&ch<= ' 9 ') x=x*10+ch-' 0 ', Ch=getchar (); 
return x;
  Use Update (use A,use b) {use C;
  c.s0=c.s1=c.sum=c.len=c.ls=c.rs=0;
  C.S0=A.S0+B.S0;C.S1=A.S1+B.S1;
  C.len=a.len+b.len;
  C.sum=max (Max (a.sum,b.sum), a.rs+b.ls);
  if (A.s0==a.len) c.ls=a.len+b.ls;
  else C.ls=a.ls;
  if (B.s0==b.len) c.rs=b.len+a.rs;
  else c.rs=b.rs;
return C;
  } void Build (int k,int l,int r) {int mid= (L+R) >>1;
  if (l==r) {T[k].s1=t[k].len=1;return;}
  Build (K<<1,l,mid);
  Build (K<<1|1,mid+1,r);
T[k]=update (t[k<<1],t[k<<1|1]);
    } void Paint (int k,int l,int R,int v) {if (v==0) {T[k].ls=t[k].rs=t[k].s0=t[k].sum=t[k].len;
  t[k].s1=0; } if (v==1) {T[K].ls=t[k].rs=t[k].s0=t[k].sum=0;
  T[k].s1=t[k].len;
} p[k]=v;
  } void pushdown (int k,int l,int r) {int mid= (L+R) >>1;
  Paint (K<<1,l,mid,p[k]);
  Paint (K<<1|1,mid+1,r,p[k]); 
P[k]=-1;
  } void Change (int k,int l,int r,int ll,int rr,int v) {int mid= (L+R) >>1;
  if (LL&LT;=L&AMP;&AMP;R&LT;=RR) {paint (k,l,r,v); return;}
  if (p[k]!=-1) pushdown (k,l,r);
  if (ll<=mid) change (K&LT;&LT;1,L,MID,LL,RR,V);
  if (MID&LT;RR) change (K&LT;&LT;1|1,MID+1,R,LL,RR,V); 
T[k]=update (t[k<<1],t[k<<1|1]);
   Use Query (int k,int l,int r,int ll,int rr) {int mid= (L+R) >>1;
   if (LL==L&AMP;&AMP;R==RR) return t[k];
   if (p[k]!=-1) pushdown (k,l,r);
   if (rr<=mid) return query (K&LT;&LT;1,L,MID,LL,RR);
   else if (MID&LT;LL) return query (K&LT;&LT;1|1,MID+1,R,LL,RR); 
else return update (query (K&LT;&LT;1,L,MID,LL,MID), query (K&LT;&LT;1|1,MID+1,R,MID+1,RR));
  } void Find (int k,int l,int r) {int mid= (L+R) >>1; if (t[k].s0<=num) {num-=t[k].s0;pAint (k,l,r,1);
  if (p[k]!=-1) pushdown (k,l,r);
  if (t[k<<1].s0>=num) find (K<<1,l,mid);
    else{Num-=t[k<<1].s0;paint (k<<1,l,mid,1);
  Find (K&LT;&LT;1|1,MID+1,R);
} t[k]=update (t[k<<1],t[k<<1|1]);
  } void Solve (int k,int l,int r,int ll,int rr) {int mid= (L+R) >>1; 
     if (LL&LT;=L&AMP;&AMP;R&LT;=RR) {if (t[k].s0<=num) num-=t[k].s0,paint (k,l,r,1);
     else find (K,L,R);
  Return
  } if (P[k]!=-1) pushdown (k,l,r);
  if (ll<=mid&&num) solve (K&LT;&LT;1,L,MID,LL,RR); 
  if (rr>mid&&num) solve (K&LT;&LT;1|1,MID+1,R,LL,RR);
T[k]=update (t[k<<1],t[k<<1|1]);
  int main () {n=read (); M=read ();
  for (int i=1;i<= (N&LT;&LT;2); i++) P[i]=-1;
  Build (1,1,n);
    for (int i=1;i<=m;i++) {k=read ();
      if (k==0) {l=read (); R=read ();
    Change (1,1,n,l,r,0);
      } if (k==1) {ll=read (); Rr=read (); L=read (); R=read ();
      Use T=query (1,1,N,LL,RR); Change (1,1,n,ll,rr,0);
      NUM=T.S1;
    Solve (1,1,N,L,R);
      } if (k==2) {l=read (); R=read ();
      Use T=query (1,1,N,L,R);
    printf ("%d\n", t.sum); }
  } 
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.