Cod sursa(job #558487)

Utilizator radu_voroneanuVoroneanu Radu Stefan radu_voroneanu Data 17 martie 2011 12:04:35
Problema Hotel Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.53 kb
#include <stdio.h>
#include <set>

using namespace std;

#define MAXN 100010

int L[MAXN];

struct classcomp {
  bool operator() (const int& lhs, const int& rhs) const
  {return L[lhs]>L[rhs];}
};

struct classcomp2 {
  bool operator() (const int& lhs, const int& rhs) const
  {return lhs>rhs;}
};

set<int,classcomp> S;
set<int,classcomp2> P;
int N,tip,i,poz,x,y,M;

int main()
{
	freopen("hotel.in","r",stdin);
	freopen("hotel.out","w",stdout);
	
	scanf("%d %d",&N,&M);
	L[1] = N;
	P.insert(1); S.insert(1);
	for (i=1; i<=M; ++i){
		scanf("%d",&tip);
		if (tip == 3){
			if (S.size()!=0)
				printf("%d\n", L[*S.begin()]);
			else
				printf("%d\n", 0);
		}
		if (tip == 2){
			scanf("%d %d",&x,&y);
			if (y==0) continue;
			L[x] = y;
			P.insert(x); S.insert(x);
			y+=x-1;
			if (L[y+1]){
				S.erase(y+1);
				P.erase(y+1);
				S.erase(x); P.erase(x);
				L[x] += L[y+1];
				L[y+1] = 0;
				S.insert(x);
				P.insert(x);
			}
			if (P.upper_bound(x) == P.end()) continue;
			poz = *P.upper_bound(x);
			if (poz+L[poz] == x){
				S.erase(poz);
				P.erase(poz);
				S.erase(x); P.erase(x);
				L[poz]+=L[x];
				L[x] = 0; x = poz;
				S.insert(x); P.insert(x);
			}
		}
		if (tip == 1){
			scanf("%d %d",&x,&y);
			if (y==0) continue;
			poz = *P.lower_bound(x);
			P.erase(poz); S.erase(poz);
			L[x+y] = (poz+L[poz]-1) - (x+y-1);
			if (L[x+y]!=0)
				P.insert(x+y), S.insert(x+y);
			L[poz] = x-poz;
			if (L[poz]!=0)
				P.insert(poz), S.insert(poz);
		}
	}
	
	return 0;
}