Cod sursa(job #2973192)

Utilizator AswVwsACamburu Luca AswVwsA Data 31 ianuarie 2023 11:35:54
Problema Hotel Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.24 kb
#include <fstream>
#include <vector>
#include <map>
#include <cmath>
#include <climits>
#define ll long long
using namespace std;

const int NMAX = 1e5;
struct ob
{
    int pref, suf;
    int smax;
    int len;
}aint[4 * NMAX + 1];

bool lazy[4 * NMAX + 1];

ob combina(ob a, ob b)
{
    ob ans;
    ans.len = a.len + b.len;
    ans.smax = max(max(a.smax, b.smax), a.suf + b.pref);
    if (a.len == a.pref)
        ans.pref = a.len + b.pref;
    else
        ans.pref = a.pref;
    if (b.len == b.suf)
        ans.suf = b.len + a.suf;
    else
        ans.suf = b.suf;
    return ans;
}
void lenes(int node, int st, int dr)
{
    if (lazy[node])
    {
        if (aint[node].smax)
            aint[node].smax = aint[node].pref = aint[node].suf = 0;
        else
            aint[node].smax = aint[node].pref = aint[node].suf = aint[node].len;
        if (st != dr)
        {
            lazy[node << 1] ^= lazy[node];
            lazy[node << 1 | 1] ^= lazy[node];
        }
    }
    lazy[node] = 0;
}

void update(int node, int st, int dr, int a, int b)
{
    lenes(node, st, dr);
    if (dr < a or st > b)
        return ;
    if (a <= st and dr <= b)
    {
        lazy[node] = 1;
        lenes(node, st, dr);
        return ;
    }
    int med = ((st + dr) >> 1);
    update(node << 1, st, med, a, b);
    update(node << 1 | 1, med + 1, dr, a, b);
    aint[node] = combina(aint[node << 1], aint[node << 1 | 1]);
}

void build(int node, int st, int dr)
{
    if (st == dr)
    {
        aint[node].len = aint[node].smax = aint[node].pref = aint[node].suf = 1;
        return ;
    }
    int med = ((st + dr) >> 1);
    build(node << 1, st, med);
    build(node << 1 | 1, med + 1, dr);
    aint[node] = combina(aint[node << 1], aint[node << 1 | 1]);
}
int main()
{
    ifstream cin("hotel.in");
    ofstream cout("hotel.out");
   // ios_base :: sync_with_stdio(0);
    //cin.tie(0);
    int n, q;
    cin >> n >> q;
    build(1, 1, n);
    while (q--)
    {
        int op, a, b;
        cin >> op;
        if (op != 3)
        {
            cin >> a >> b;
            update(1, 1, n, a, a + b - 1);
        }
        else
            cout << aint[1].smax << "\n";
    }
    return 0;
}