Cod sursa(job #778826)

Utilizator ionut_blesneagIonut Blesneag ionut_blesneag Data 15 august 2012 22:15:44
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.74 kb
#include<cstdio>
using namespace std;

int n,p,i,x,y,val,links,rechts;
int c;
struct leaf {int max_st; int max_dr; int max_tot;};
leaf v[400001];

void afisare()
{for(int psi=1; psi<=2*n+1; psi++)
   printf(" {%d,%d,%d} ",v[psi].max_st,v[psi].max_tot,v[psi].max_dr);
 printf("\n");  
}

void build(int nod, int left, int right)
{v[nod].max_tot=v[nod].max_st=v[nod].max_dr=right-left+1;
 if(left==right)
    return;
 int mid=(left+right)/2;
 build(2*nod,left,mid);
 build(2*nod+1,mid+1,right);  
}

void update(int nod, int left, int right)
{if(links<=left && right<=rechts)
   {v[nod].max_st=v[nod].max_dr=v[nod].max_tot=0;
   return;}
 int mid=(left+right)/2;
 
  if(v[nod].max_tot==right-left+1)
  {v[2*nod].max_st=v[2*nod].max_dr=v[2*nod].max_tot=mid-left+1;
   v[2*nod+1].max_st=v[2*nod+1].max_dr=v[2*nod+1].max_tot=right-mid;}  
 
 if(links<=mid)
    update(2*nod,left,mid);
 if(rechts>mid)
    update(2*nod+1,mid+1,right);
        
 if(v[2*nod].max_st==mid-left+1)
    v[nod].max_st=v[2*nod].max_st+v[2*nod+1].max_st;
 else
    v[nod].max_st=v[2*nod].max_st;  
    
 if(v[2*nod+1].max_dr==right-mid)
    v[nod].max_dr=v[2*nod+1].max_dr+v[2*nod].max_dr;  
 else
    v[nod].max_dr=v[2*nod+1].max_dr;

 v[nod].max_tot=v[2*nod].max_dr+v[2*nod+1].max_st;
 if(v[nod].max_tot<v[2*nod].max_tot)
    v[nod].max_tot=v[2*nod].max_tot;
 if(v[nod].max_tot<v[2*nod+1].max_tot)
    v[nod].max_tot=v[2*nod+1].max_tot;     
}

void erase(int nod, int left, int right)
{if(links<=left && right<=rechts)
   {v[nod].max_st=v[nod].max_dr=v[nod].max_tot=right-left+1;
   return;}
 int mid=(left+right)/2;

 if(v[nod].max_tot==0)
 {v[2*nod].max_st=v[2*nod].max_dr=v[2*nod].max_tot=0;
  v[2*nod+1].max_st=v[2*nod+1].max_dr=v[2*nod+1].max_tot=0;} 
 
 if(links<=mid)
    erase(2*nod,left,mid);
 if(rechts>mid)
    erase(2*nod+1,mid+1,right);
    
 if(v[2*nod].max_st==mid-left+1)
    v[nod].max_st=v[2*nod].max_st+v[2*nod+1].max_st;
 else
    v[nod].max_st=v[2*nod].max_st;  
    
 if(v[2*nod+1].max_dr==right-mid)
    v[nod].max_dr=v[2*nod+1].max_dr+v[2*nod].max_dr;  
 else
    v[nod].max_dr=v[2*nod+1].max_dr;

 v[nod].max_tot=v[2*nod].max_dr+v[2*nod+1].max_st;
 if(v[nod].max_tot<v[2*nod].max_tot)
    v[nod].max_tot=v[2*nod].max_tot;
 if(v[nod].max_tot<v[2*nod+1].max_tot)
    v[nod].max_tot=v[2*nod+1].max_tot;     
}

int main()
{
freopen("hotel.in","r",stdin);
freopen("hotel.out","w",stdout);
scanf("%d %d",&n,&p);
build(1,1,n);
for(i=1; i<=p; i++)
{scanf("%d",&c);
if(c==1)
{scanf("%d %d",&x,&y);
 links=x;   rechts=x+y-1;  
 update(1,1,n);
 //afisare(); 
 }
if(c==2)
{scanf("%d %d",&x,&y);
 links=x;   rechts=x+y-1;
 erase(1,1,n);
// afisare(); 
 }
if(c==3)
  {printf("%d\n",v[1].max_tot);}
}


return 0;}