Cod sursa(job #480528)

Utilizator darrenRares Buhai darren Data 28 august 2010 12:35:40
Problema Hotel Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.46 kb
#include <cstdio>
#include <fstream>
#include <algorithm>

using namespace std;

struct nod
{
	int left, right, tot;
};

int n, p;
nod ai[4 * 100000 + 1];

#define left1 ((md - st + 1) - ai[x * 2].left)
#define right1 ((md - st + 1) - ai[x * 2].right)
#define tot1 ((md - st + 1) - ai[x * 2].tot)
#define left2 ((dr - md) - ai[x * 2 + 1].left)
#define right2 ((dr - md) - ai[x * 2 + 1].right)
#define tot2 ((dr - md) - ai[x * 2 + 1].tot)
#define tott ((dr - st + 1) - ai[x].tot)

void update(int x, int st, int dr, int l1, int l2, int ch)
{
	if (st == dr)
	{
		ai[x].left = ai[x].right = ai[x].tot = 1 - ch;
		return;
	}
	
	int md = (st + dr) / 2;
	if (l1 <= md) update(x * 2, st, md, l1, l2, ch);
	if (l2 > md) update(x * 2 + 1, md + 1, dr, l1, l2, ch);
	
	ai[x].tot = (dr - st + 1) - max(right1 + left2, max(left1, right2));
	ai[x].tot = (dr - st + 1) - max(tott, max(tot1, tot2));
	
	ai[x].left = (dr - st + 1) - ((left1 == (md - st + 1)) ? left1 + left2 : left1);
	ai[x].right = (dr - st + 1) - ((right2 == (dr - md)) ? right2 + right1 : right2);
}

int main()
{
	ifstream fin("hotel.in");
	ofstream fout("hotel.out");
	
	fin >> n >> p;
	
	int c, l1, l2;
	while (p--)
	{
		fin >> c;
		switch (c)
		{
			case 1:
				fin >> l1 >> l2;
				l2 += l1 - 1;
				update(1, 1, n, l1, l2, 0); 
				break;
			case 2:
				fin >> l1 >> l2;
				l2 += l1 - 1;
				update(1, 1, n, l1, l2, 1);
				break;
			case 3:
				fout << n - ai[1].tot << '\n';
				break;
		}
	}
	
	fin.close();
	fout.close();
}