Cod sursa(job #1426951)

Utilizator theprdvtheprdv theprdv Data 1 mai 2015 02:51:33
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.47 kb
#include <fstream>
#include <algorithm>

using namespace std;

fstream fin("hotel.in", ios::in);
fstream fout("hotel.out", ios::out);
struct tree
{
	int l, max, r;
};
#define MAXN 100000
int N, M, P, val, a, b, lazy[4 * MAXN + 10];
tree T[4 * MAXN + 10];

inline int max_node_val(int left_val, int right_val)
{
	if (left_val == -1 && right_val == -1) return -1;
	return (left_val == -1 ? 0 : left_val) + (right_val == -1 ? 0 : right_val);
}
inline void lazy_update(int node, int left, int right)
{
	T[node].max = T[node].l = T[node].r = lazy[node] == 1 ? right - left + 1 : -1;
}
inline void get_max(int node, int left, int right)
{
	bool l_exc, r_exc;
	int mid = (left + right) / 2;
	if(lazy[node * 2]) lazy_update(node * 2, left, mid);
	if(lazy[node * 2 + 1]) lazy_update(node * 2 + 1, mid + 1, right);

	int left_max = !T[node * 2].max ? mid - left + 1 : T[node * 2].max,
		left_l = !T[node * 2].l ? left_max : T[node * 2].l,
		left_r = !T[node * 2].r ? left_max : T[node * 2].r,
		right_max = !T[node * 2 + 1].max ? right - mid : T[node * 2 + 1].max,
		right_l = !T[node * 2 + 1].l ? right_max : T[node * 2 + 1].l,
		right_r = !T[node * 2 + 1].r ? right_max : T[node * 2 + 1].r;
	l_exc = left_l == mid - left + 1, r_exc = right_r == right - mid;

	T[node].l = l_exc ? left_l + (right_l == -1 ? 0 : right_l) : left_l;
	T[node].r = r_exc ? right_r + (left_r == -1 ? 0 : left_r) : right_r;
	T[node].max = max(max(left_max, right_max), max_node_val(left_r, right_l));
}

void update(int node, int left, int right)
{
	if (lazy[node]){
		lazy_update(node, left, right);
		if (left != right)
			lazy[node * 2] = lazy[node],
			lazy[node * 2 + 1] = lazy[node];
		lazy[node] = 0;
	}
	if (a <= left && right <= b){
		T[node].max = T[node].l = T[node].r = val == 1 ? right - left + 1 : -1;
		if (right != left)
			lazy[node * 2] = val,
			lazy[node * 2 + 1] = val;
		return;
	}
	int mid = (left + right) / 2;
	if (a <= mid) update(node * 2, left, mid);
	if (b >= mid + 1) update(node * 2 + 1, mid + 1, right);
	get_max(node, left, right);
}

int main()
{
	fin >> N >> P;
	for (int i = 1, x; i <= P; i++){
		fin >> x;
		if (x == 1)
			fin >> a >> b,
			b = a + b - 1, val = -1,
			update(1, 1, N);
		else if (x == 2)
			fin >> a >> b,
			b = a + b - 1, val = 1,
			update(1, 1, N);
		else {
			int max_room = !T[1].max ? N : T[1].max;
			if (max_room == -1) max_room = 0;
			fout << max_room << "\n";
		}
	}

	fout.close();
	fin.close();
	return 0;
}