Cod sursa(job #301848)

Utilizator rethosPaicu Alexandru rethos Data 8 aprilie 2009 14:47:16
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.52 kb
#include <stdio.h>
#define NM 2000000
#define max(x,y) ((x)>(y)?(x):(y))
struct arb{int st,dr,t;} a[NM];
int n;
void update(int x,int y,int st,int dr,int nod,int tip);
void build(int st,int dr,int nod);
int main()
{freopen("hotel.in","r",stdin);
 freopen("hotel.out","w",stdout);
 int p;
 int tip,x,y;
 scanf("%d %d",&n,&p);
 build(1,n,1);
 while (p--)
	 {scanf("%d",&tip);
	  if (tip==3)
		  {printf("%d\n",a[1].t);
		  }
		  else
		  {scanf("%d %d",&x,&y);
		   update(x,x+y-1,1,n,1,tip);
		  } 
	 }
 return 0;
}
void build(int st,int dr,int nod)
{a[nod].st=a[nod].dr=a[nod].t=dr-st+1;
 if (st==dr) return;
 int mij=(st+dr)/2;
 build(st,mij,nod<<1);
 build(mij+1,dr,(nod<<1)+1);
}
void update(int x,int y,int st,int dr,int nod,int tip)
{if (x<=st&&dr<=y)
	{if (tip==2)
		{a[nod].st=a[nod].dr=a[nod].t=dr-st+1;
		}
		else
		{a[nod].st=a[nod].dr=a[nod].t=0;
		}
	 return;
	}
 int mij=(st+dr)/2;
 int f1=nod<<1;
 int f2=f1+1;
 if (a[nod].t==dr-st+1)
	 {a[f1].t=a[f1].st=a[f1].dr=mij-st+1;
      a[f2].t=a[f2].st=a[f2].dr=dr-mij;	 
	 }
 if (a[nod].t==0)
	 {a[f1].t=a[f1].st=a[f1].dr=0;
	  a[f2].t=a[f2].st=a[f2].dr=0;
	 }
 if (x<=mij) update(x,y,st,mij,f1,tip);
 if (mij<y) update(x,y,mij+1,dr,f2,tip);
 if (a[f1].st==mij-st+1)
	 {a[nod].st=a[f1].t+a[f2].st;
	 }
	 else
	 {a[nod].st=a[f1].st;
	 }
 if (a[f2].dr==dr-mij)
	 {a[nod].dr=a[f2].t+a[f1].dr;
	 }
	 else
	 {a[nod].dr=a[f2].dr;
	 }
 a[nod].t=a[f1].dr+a[f2].st;	 
 a[nod].t=max(a[nod].t,a[f1].t);
 a[nod].t=max(a[nod].t,a[f2].t);
}