Cod sursa(job #2328598)

Utilizator vlad.ulmeanu30Ulmeanu Vlad vlad.ulmeanu30 Data 25 ianuarie 2019 22:45:31
Problema Hotel Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.27 kb
#include <bits/stdc++.h>
#define maxn 100000
#define maxl2 17
#define fi first
#define se second

using namespace std;

struct nod
{
  int ls, ld, lmax, len;
};

nod aint[maxn*maxl2+5];

inline void setcomp ( int p, int x ) { aint[p].ls = aint[p].ld = aint[p].lmax = x; }

inline void flip ( int id, int p )
{
  if ( id == 1 )
    setcomp ( p, 0 );
  else
    setcomp ( p, aint[p].len );
}

void build ( int p, pair<int,int> itv )
{
  aint[p].len = itv.se - itv.fi + 1;
  setcomp ( p, aint[p].len );
  if ( itv.fi < itv.se )
  {
    int mid = (itv.fi + itv.se) / 2;
    build ( p*2, {itv.fi, mid} );
    build ( p*2+1, {mid+1, itv.se} );
  }
}

void update ( int id, int p, pair<int,int> itv, pair<int,int> ok )
{
  if ( itv.se < ok.fi || ok.se < itv.fi ) return;

  if ( itv.fi >= ok.fi && itv.se <= ok.se )
  {
    flip ( id, p );

    if ( itv.fi < itv.se )
    {
      flip ( id, p * 2 );
      flip ( id, p * 2 + 1 );
    }
    return;
  }

  if ( itv.fi < itv.se )
  {
    if ( id == 1 && aint[p].lmax == aint[p].len )
    {
      setcomp ( p*2, aint[p*2].len );
      setcomp ( p*2+1, aint[p*2+1].len );
    }
    else if ( id == 2 && aint[p].lmax == 0 )
    {
      setcomp ( p*2, 0 );
      setcomp ( p*2+1, 0 );
    }
  }

  if ( itv.fi < itv.se )
  {
    int mid = (itv.fi + itv.se) / 2;
    update ( id, p*2, {itv.fi,mid}, ok );
    update ( id, p*2+1, {mid+1,itv.se}, ok );

    aint[p].ls = aint[2*p].ls;
    aint[p].ld = aint[2*p+1].ld;

    if ( aint[2*p+1].ls == aint[2*p+1].len )
      aint[p].ld = aint[2*p].ld + aint[2*p+1].len;
    if ( aint[2*p].ld == aint[2*p].len )
      aint[p].ls = aint[2*p].len + aint[2*p+1].ls;
    if ( aint[p].ls + aint[p].ld >= aint[p].len )
      aint[p].ls = aint[p].ld = aint[p].len;

    aint[p].lmax = max(aint[2*p].ld + aint[2*p+1].ls, max(aint[2*p].lmax, aint[2*p+1].lmax));
  }
}

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

  int n, t;
  fin >> n >> t;

  build ( 1, {1,n} );

  int id, a, b;
  while (t--)
  {
    fin >> id;
    if ( id <= 2 )
    {
      fin >> a >> b;
      update ( id, 1, {1,n}, {a,a+b-1} );
    }
    else
      fout << aint[1].lmax << '\n';
  }

  fin.close ();
  fout.close ();

  return 0;
}