Cod sursa(job #1632256)

Utilizator pickleVictor Andrei pickle Data 5 martie 2016 23:26:47
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.58 kb
#include <iostream>
#include <fstream>

using namespace std;
const int Nmax = 100555;

int L[4*Nmax], R[4*Nmax], M[4*Nmax], len[4*Nmax];
int N, P, first, last, pos, num, type;

inline int max3(int x, int y, int z) {
  if (x > y)
    return (x > z ? x : z);
  else
    return (y > z ? y : z);
}

void init(int nod, int l, int r);
void change(int nod, int l, int r);

int main() {
  ifstream fin ("hotel.in");
  ofstream fout ("hotel.out");

  fin >> N >> P;
  init(1, 1, N);
  while(P--) {
    fin >> type;
    if (type == 3) {
      fout << M[1] << "\n";
    } else {
      fin >> pos >> num;
      first = pos, last = pos + num - 1;
      change(1, 1, N);
    }
  }

  return 0;
}

void init(int nod, int l, int r) {
  len[nod] = r-l+1;
  L[nod] = len[nod];
  R[nod] = len[nod];
  M[nod] = len[nod];
  if (l==r)
    return;

  int m = (l+r)/2;
  init(2*nod, l, m);
  init(2*nod+1, m+1, r);
}

void change(int nod, int l, int r) {
  if (first <= l && r <= last) {
    int x = (type-1)*len[nod];
    L[nod] = R[nod] = M[nod] = x;
    return;
  }

  if (M[nod] == len[nod]) {
    M[2*nod] = L[2*nod] = R[2*nod] = len[2*nod];
    M[2*nod+1] = L[2*nod+1] = R[2*nod+1] = len[2*nod+1];
  } else if (M[nod] == 0) {
    M[2*nod] = L[2*nod] = R[2*nod] = 0;
    M[2*nod+1] = L[2*nod+1] = R[2*nod+1] = 0;
  }

  int m = (l+r)/2;
  if (first <= m) change(2*nod, l, m);
  if (last > m)   change(2*nod+1, m+1, r);

  L[nod] = (L[2*nod] == len[2*nod] ? L[2*nod] + L[2*nod+1] : L[2*nod]);
  R[nod] = (R[2*nod+1] == len[2*nod+1] ? R[2*nod] + R[2*nod+1] : R[2*nod+1]);
  M[nod] = max3(M[2*nod], M[2*nod+1], R[2*nod] + L[2*nod+1]);
}