Cod sursa(job #678466)

Utilizator balakraz94abcd efgh balakraz94 Data 11 februarie 2012 19:49:37
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.7 kb
#include <cstdio>
#include <algorithm>
#define infile "hotel.in"
#define outfile "hotel.out"
#define n_max 100005
#define modify(nod, val) AI[nod].l = AI[nod].r = AI[nod].n = val
using namespace std;

int N, P;

struct NOD
{
   int n, l, r;
}  AI[3*n_max];


void build( int nod, int st, int dr)
{
	modify (nod, dr-st+1);
	
	if(st == dr)
		return;
	
	int mij = st + (dr - st) /2;
	
	build(2*nod, st, mij);
	build(2*nod+1, mij+1, dr);	
}


void update (int nod, int st, int dr, int a, int b, int val)
{
	if(a<=st && dr<=b)
	{
		if(val == 1)
			modify(nod,0);
		else
			modify(nod, dr - st + 1);
		return;
	}
	
    if(st == dr)
		return;
	
	int mij = st + (dr - st)/2;
	
	if(AI[nod].n == dr-st+1)
	{
		modify(2*nod, mij-st+1);
		modify(2*nod+1, dr-mij);
	}

    if(AI[nod].n == 0)
	{
		modify(2*nod, 0);
		modify(2*nod+1, 0);
	}		
	
	if(a <= mij)
		update(2*nod, st, mij, a, b, val);
	if(mij < b)
		update(2*nod+1, mij+1, dr, a, b, val);
	
	
	if(AI[2*nod].l == mij - st + 1)
		AI[nod].l = AI[2*nod].l + AI[2*nod+1].l;
	else
		AI[nod].l = AI[2*nod].l;
	
	
	if(AI[2*nod+1].r == dr - mij)
		AI[nod].r = AI[2*nod+1].r + AI[2*nod].r;
	else
		AI[nod].r = AI[2*nod+1].r;
	
	
	AI[nod].n = max(AI[2*nod].n, AI[2*nod+1].n);
	
	AI[nod].n = max(AI[nod].n, AI[2*nod].r + AI[2*nod+1].l);
}



int main(void)
{
    freopen(infile, "r", stdin);
	freopen(outfile, "w", stdout);
    
	int op, x, y;
	
	scanf("%d %d", &N, &P);
	
	build(1, 1, N);
	
	while(P--)
	{
		scanf("%d",&op);
		
		if(op <= 2)
		{
			scanf("%d %d",&x, &y);
			
			update(1, 1, N, x, x+y-1, op);
			
			continue;
		}
		
	    printf("%d\n", AI[1].n);  
	}

    fclose(stdin);
	fclose(stdout);
    
    return 0;
}