bzoj4592: [Shoi2015] brain hole therapy instrument

Source: Internet
Author: User

Because the sequence of the brain cavity does not change, consider using the line tree to maintain the interval sum, the number of left 0, the number of the right 0, the most brain hole in the interval. For query l~r the most brain hole can divide l~r into logn intervals, the total complexity O (NLOGN).

#include <iostream> #include <cstdio> #include <cstdlib> #define N 800005 using namespace std;
int n,m,p,x,y,l,r;
int Sum[n],l[n],r[n],tg[n],v[n],ans[n],rest,hz,ans;
		void down (int k) {if (tg[k]!=-1) {int l=k<<1,r=l|1;
		TG[L]=TG[R]=TG[K];
		SUM[L]=V[L]*TG[K];
		SUM[R]=V[R]*TG[K];
		if (tg[k]==0) l[l]=r[l]=ans[l]=v[l],l[r]=r[r]=ans[r]=v[r];
		else l[l]=r[l]=l[r]=ans[l]=r[r]=ans[r]=0;
	Tg[k]=-1;
	}} void up (int k) {int l=k<<1,r=l|1;sum[k]=sum[l]+sum[r];
	if (!sum[l]) L[k]=l[l]+l[r];else l[k]=l[l];
	if (!sum[r]) R[k]=r[l]+r[r];else r[k]=r[r];
Ans[k]=max (Max (Ans[l],ans[r]), r[l]+l[r]);
	} void Build (int k,int l,int R) {if (l==r) {V[k]=1;return;}
	int mid= (L+R) >>1;
	Build (K<<1,l,mid);
	Build (K<<1|1,mid+1,r);
v[k]=v[k<<1]+v[k<<1|1];
		} void Add (int k,int l,int r,int x,int y) {if (x<=l&&r<=y) {sum[k]=0;tg[k]=0;
		L[K]=R[K]=V[K];
	Return
	} int mid= (L+R) >>1;
	Down (k); if (x<=mid) Add (k<<1,L,mid,x,y);
	if (y>mid) Add (k<<1|1,mid+1,r,x,y);
Up (k);
	} int Get (int k,int l,int r,int x,int y) {if (x<=l&&r<=y) return sum[k];
	int mid= (L+R) >>1;
	Down (k);
	int ans=0;
	if (x<=mid) ans+=get (k<<1,l,mid,x,y);
	if (y>mid) ans+=get (k<<1|1,mid+1,r,x,y);
return Ans;
	} void fix (int k,int l,int r,int x,int y) {if (!rest) return;
		if (X<=l&&r<=y&&rest>=v[k]-sum[k]) {rest-=v[k]-sum[k]; Tg[k]=1;
		ans[k]=0;
		L[K]=R[K]=0;SUM[K]=V[K];
	Return
	} down (k);
	int mid= (L+R) >>1;
	if (x<=mid) fix (k<<1,l,mid,x,y);
	if (y>mid) fix (k<<1|1,mid+1,r,x,y);
Up (k);
		} void qry (int k,int l,int r,int x,int y) {if (x<=l&&r<=y) {Ans=max (ans,ans[k]);
		Ans=max (Ans,hz+l[k]);
		if (R[k]==v[k]) Hz+=r[k];else hz=r[k];
	Return
	} down (k);
	int mid= (L+R) >>1;
	if (x<=mid) qry (k<<1,l,mid,x,y);
if (y>mid) qry (k<<1|1,mid+1,r,x,y); } int main () {scanf ("%d%d", &n,&m);
	Build (1,1,n);
	Sum[1]=v[1];tg[1]=1;
		for (int i=1;i<=m;i++) {scanf ("%d%d%d", &p,&l,&r);
		if (p==0) Add (1,1,n,l,r);
			else if (p==1) {scanf ("%d%d", &x,&y);
			Rest=get (1,1,N,L,R);
			Add (1,1,N,L,R);
		Fix (1,1,n,x,y); } else {ans=0;
			Hz=0;qry (1,1,N,L,R);
		printf ("%d\n", ans);
}} return 0;
 }


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.