Cod sursa(job #2774541)

Utilizator racsosabeVictor Racso Galvan Oyola racsosabe Data 11 septembrie 2021 20:33:51
Problema Hotel Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.25 kb
#include <bits/stdc++.h>
using namespace std;

void setIO(string name, bool input = true, bool output = true){
  string inputname = name + ".in";
  string outputname = name + ".out";
  if(input) freopen(inputname.c_str(), "r", stdin);
  if(output) freopen(outputname.c_str(), "w", stdout);
}

const int N = 100000 + 5;

int n;
int m;
set<pair<int, int>> ones;
multiset<int> lengths; 

void insert(int l, int r){
  if(ones.empty()){
    ones.emplace(make_pair(l, r));
    lengths.emplace(r - l + 1);
  }
  else{
    set<pair<int, int>>::iterator R = ones.lower_bound(make_pair(l, r));
    bool erase_r = false, erase_l = false;
    if(R != ones.end() and r + 1 == (*R).first){
      lengths.erase(lengths.find((*R).second - (*R).first + 1));
      r = (*R).second;
      erase_r = true;
    }
    set<pair<int, int>>::iterator L = R;
    if(L != ones.begin()){
      L--;
      if((*L).second + 1 == l){
        lengths.erase(lengths.find((*L).second - (*L).first + 1));
        l = (*L).first;
        erase_l = true;
      }
    }
    if(erase_r and ones.find(*R) != ones.end()) ones.erase(R);
    if(erase_l and ones.find(*L) != ones.end()) ones.erase(L);
    ones.emplace(make_pair(l, r));
    lengths.emplace(r - l + 1);
  }
}

void remove(int l, int r){
  set<pair<int, int>>::iterator it = ones.lower_bound(make_pair(l, r));
  if(it == ones.end()) it--;
  while(it != ones.begin() and (*it).first > l){
    it--;
  }
  assert((*it).first <= l and r <= (*it).second);
  int L = (*it).first, R = (*it).second;
  ones.erase(it);
  lengths.erase(lengths.find(R - L + 1));
  if(L <= l - 1){
    ones.emplace(make_pair(L, l - 1));
    lengths.emplace(l - L);
  }
  if(r + 1 <= R){
    ones.emplace(make_pair(r + 1, R));
    lengths.emplace(R - r);
  }
}

int query(){
  return lengths.empty() ? 0 : *lengths.rbegin();
}

int main(){
  setIO("hotel");
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  insert(1, n);
  for(int i = 1; i <= m; i++){
    int op, x, y;
    cin >> op;
    if(op == 1){
      cin >> x >> y;
      remove(x, x + y - 1);
    }
    else if(op == 2){
      cin >> x >> y;
      insert(x, x + y - 1);
    }
    else{
      cout << query() << endl;
    }
  }
  return 0;
}