Cod sursa(job #1481190)

Utilizator tamionvTamio Vesa Nakajima tamionv Data 3 septembrie 2015 23:36:29
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.14 kb
#include <set>
#include <iostream>
#include <fstream>
using namespace std;

struct seg{
	int st, dr;
	constexpr seg():
		st(0), dr(0){}
	constexpr seg(const int s, const int d):
		st(s), dr(d){}
	constexpr int marime(){
		return dr-st+1; }
	constexpr bool operator<(const seg& rhs){
		return st > rhs.st; } };

void erase_one_from_multiset(multiset<int>& m, const int x){
	m.erase(m.find(x)); }

class the_structure{
	multiset<seg> segmente;
	multiset<int> marimi;
public:
	void print(){
		for(const auto x : segmente){
			cout << x.st << ' ' << x.dr << '\n'; }
		cout << endl;
		for(const auto x : marimi){
			cout << x << ' '; }
		cout << endl;
		cout << "-------\n\n"; }
	the_structure(const int n){
		segmente.emplace(1, n);
		marimi.insert(n); }
	void adauga(const seg& s){
		const auto it = segmente.lower_bound(s);
		const auto scos = *it;
		segmente.erase(it);
		marimi.erase(marimi.find(scos.marime()));

		if(scos.st <= s.st-1){
			segmente.emplace(scos.st, s.st-1);
			marimi.insert(s.st - scos.st); }
		if(s.dr+1 <= scos.dr){
			segmente.emplace(s.dr+1, scos.dr);
			marimi.insert(scos.dr - s.dr); } }
	void scoate(const seg& s){
		auto it = segmente.insert(s);
		if(it != begin(segmente)){
			auto tmp = it;
			--tmp;
			if(it->dr == tmp->st-1){
				const seg new_seg(it->st, tmp->dr);
				marimi.erase(marimi.find(tmp->marime()));
				segmente.erase(it);
				segmente.erase(tmp);
				it = segmente.insert(new_seg); } }
		auto tmp = it;
		++tmp;
		if(tmp != end(segmente) && tmp->dr == it->st-1){
			const seg new_seg(tmp->st, it->dr);
			marimi.erase(marimi.find(tmp->marime()));
			segmente.erase(it);
			segmente.erase(tmp);
			it = segmente.insert(new_seg); }
		marimi.insert(it->marime()); }
	int query(){
		return marimi.empty() ? 0 : *(marimi.rbegin()); } };

int main(){
	ifstream f("hotel.in");
	ofstream g("hotel.out");
	int n, p;
	f >> n >> p;
	the_structure ts(n);
	for(int i = 0, t, x, m; i < p; ++i){
		//cout << i << ".\n";
		//ts.print();
		f >> t;
		switch(t){
		case 1:
			f >> x >> m;
			ts.adauga({x, x+m-1});
			break;
		case 2:
			f >> x >> m;
			ts.scoate({x, x+m-1});
			break;
		case 3:
			g << ts.query() << '\n';
			break; } }
	ts.print();
	return 0; }