Cod sursa(job #2327640)

Utilizator MAXIMILLIANMUSOHYEAHYEAH MAXIMILLIANMUS Data 24 ianuarie 2019 19:43:36
Problema Hotel Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.23 kb
#include <bits/stdc++.h>
 
using namespace std;
	
ifstream in("hotel.in");
ofstream out("hotel.out");
	
const int DIM = 4e5 + 7;
 
int best[DIM];
int st[DIM];
int dr[DIM];
bool all[DIM];
int lazy[DIM];
 
void down(int nod, int l, int r)
{
	if(lazy[nod] == 1)
	{
		st[nod] = dr[nod] = best[nod] = r - l + 1;
		all[nod] = 1;
	}
	else
	{
		st[nod] = dr[nod] = best[nod] = 0;
		all[nod] = 0;
	}
	
	if(l != r)
	{
		lazy[2 * nod] = lazy[nod];
		lazy[2 * nod + 1] = lazy[nod];
	}
	
	lazy[nod] = -1;
}
 
void update(int nod, int l, int r, int tl, int tr, int val)
{
	if(lazy[nod] != -1)
	{
		down(nod, l, r);
	}
	
	if(tl <= l && r <= tr)
	{
		lazy[nod] = val;
		down(nod, l, r);
		return ;
	}
	
	int mid = (l + r) / 2;
	
	if(tl <= mid)
		update(nod * 2, l, mid, tl, tr, val);
	
	if(tr > mid)
		update(nod * 2 + 1, mid + 1, r, tl, tr, val);
	
	if(lazy[nod * 2] != -1)
		down(nod * 2, l, mid);
	
	if(lazy[nod * 2 + 1] != -1)
		down(nod * 2 + 1, mid + 1, r);
	
	st[nod] = st[2 * nod];
	dr[nod] = dr[2 * nod + 1];
	
	if(all[2 * nod] == 1)
		st[nod] += st[2 * nod + 1];
	
	if(all[2 * nod + 1] == 1)
		dr[nod] += dr[2 * nod];
	
	if(all[2 * nod] == 1 && all[2 * nod + 1] == 1)
		all[nod] = 1;
	else
		all[nod] = 0;
	
	best[nod] = max(best[2 * nod], best[2 * nod + 1]);
	best[nod] = max(best[nod], st[nod * 2 + 1] + dr[nod * 2]);
	best[nod] = max(best[nod], st[nod]);
	best[nod] = max(best[nod], dr[nod]);
}
 
void init(int nod, int l, int r)
{
	if(l == r)
	{
		lazy[nod] = -1;
		st[nod] = 1;
		dr[nod] = 1;
		best[nod] = 1;
		all[nod] = 1;
		return ;
	}
	
	int mid = (l + r) / 2;
	
	init(nod * 2, l, mid);
	init(nod * 2 + 1, mid + 1, r);
	
	all[nod] = 1;
	
	st[nod] = st[nod * 2] + st[nod * 2 + 1];
	dr[nod] = st[nod];
	best[nod] = st[nod];
	lazy[nod] = -1;
}
 
int main()
{
	int n, p;
	in >> n >> p;
	
	init(1, 1, n);
	
	while(p--)
	{
		int op;
		in >> op;
		
		if(op == 1)
		{
			int l, nr;
			in >> l >> nr;
			
			int r = l + nr - 1;
			
			update(1, 1, n, l, r, 0);
		}
		else
			if(op == 2)
			{
				int l, nr;
				in >> l >> nr;
				
				int r = l + nr - 1;
				
				update(1, 1, n, l, r, 1);
			}
			else
			{
				out << best[1] << '\n';
			}
	}
}