Cod sursa(job #2965484)

Utilizator ezluciPirtac Eduard ezluci Data 15 ianuarie 2023 13:51:24
Problema Hotel Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.38 kb
using namespace std;
#ifdef EZ
   #include "./ez/ez.h"
   const string FILE_NAME = "test";
#else
   #include <bits/stdc++.h>
   const string FILE_NAME = "hotel";
#endif
#define mp make_pair
#define mt make_tuple
#define ll long long
#define pb push_back
#define fi first
#define se second
#define cin fin
#define cout fout
ifstream fin (FILE_NAME + ".in");
ofstream fout (FILE_NAME + ".out");

const int nMAX = 100e3;
struct Nod {
   int stmax, max, drmax, lazy;
};

int n, q;
Nod aint[4*nMAX + 1];


void buildAint(int nod = 1, int st = 1, int dr = n)
{
   aint[nod] = {dr-st+1, dr-st+1, dr-st+1, -1};

   if (st == dr)
      return;
   
   int mj = st+dr >> 1;
   buildAint(nod<<1, st, mj);
   buildAint(nod<<1|1, mj+1, dr);
}


void propagare(int nod, int st, int dr)
{
   if (aint[nod].lazy == -1)
      return;
   
   if (st != dr)
      aint[nod<<1].lazy = aint[nod<<1|1].lazy = aint[nod].lazy;
   
   if (aint[nod].lazy)
      aint[nod] = {0, 0, 0, -1};
   else
      aint[nod] = {dr-st+1, dr-st+1, dr-st+1, -1};
}


void update(int qst, int qdr, int MODE, int nod = 1, int st = 1, int dr = n)
{
   if (qst <= st && dr <= qdr)
   {
      aint[nod].lazy = MODE;
      propagare(nod, st, dr);
      return;
   }
   
   propagare(nod, st, dr);

   int mj = st+dr >> 1;
   if (qst <= mj && mj+1 <= qdr)
   {
      update(qst, qdr, MODE, nod<<1, st, mj);
      update(qst, qdr, MODE, nod<<1|1, mj+1, dr);
   }
   else if (qst <= mj)
   {
      update(qst, qdr, MODE, nod<<1, st, mj);
      propagare(nod<<1|1, mj+1, dr);
   }
   else if (mj+1 <= qdr)
   {
      update(qst, qdr, MODE, nod<<1|1, mj+1, dr);
      propagare(nod<<1, st, mj);
   }
   
   if (aint[nod<<1].max == mj-st+1)
      aint[nod].stmax = mj-st+1 + aint[nod<<1|1].stmax;
   else
      aint[nod].stmax = aint[nod<<1].stmax;
   
   if (aint[nod<<1|1].max == dr-(mj+1)+1)
      aint[nod].drmax = dr-(mj+1)+1 + aint[nod<<1].drmax;
   else
      aint[nod].drmax = aint[nod<<1|1].drmax;
   
   aint[nod].max = std::max({aint[nod].stmax, aint[nod<<1].drmax + aint[nod<<1|1].stmax, aint[nod].drmax});
}


int main()
{
   cin >> n >> q;
   buildAint();

   while (q--)
   {
      int op, a, b;
      cin >> op;

      if (op == 1)
      {
         cin >> a >> b;
         update(a, a+b-1, 1);
      }
      else if (op == 2)
      {
         cin >> a >> b;
         update(a, a+b-1, 0);
      }
      else if (op == 3)
      {
         cout << aint[1].max << '\n';
      }
   }
}