Cod sursa(job #3362985)

Utilizator CorvinJudge0Corvin Judge CorvinJudge0 Data 13 august 2026 12:04:54
Problema Hotel Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.79 kb
#include <bits/stdc++.h>
#define MAXN 100000

using namespace std;

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

int t;
struct nod{
    int pref, suff, best, sum;
} aint[MAXN + 1];

nod combine(nod A, nod B){
    nod rez;

    rez.sum = A.sum + B.sum;
    rez.pref = A.pref;
    if(A.sum == 0){
        rez.pref += B.pref;
    }
    rez.suff = B.suff;
    if(B.sum == 0){
        rez.suff += A.suff;
    }
    rez.best = max(A.best, max(B.best, A.suff + B.pref));

    return rez;
}

void build(int nod, int st, int dr){
    int mij;

    if(st == dr){
        aint[nod].pref = aint[nod].suff = aint[nod].best = 1;
        aint[nod].sum = 0;
    }else{
        mij = (st + dr) / 2;

        build(2 * nod, st, mij);
        build(2 * nod + 1, mij + 1, dr);
        aint[nod] = combine(aint[2 * nod], aint[2 * nod + 1]);
    }
}

void update(int nod, int st, int dr, int a, int b){
    int mij;

    if(st == dr){
        if(t == 1){
            aint[nod].best = aint[nod].suff = aint[nod].pref = 0;
            aint[nod].sum = 1;
        }else{
            aint[nod].best = aint[nod].suff = aint[nod].pref = 1;
            aint[nod].sum = 0;
        }
    }else{
        mij = (st + dr) / 2;

        if(b <= mij){
            update(2 * nod, st, mij, a, b);
        }else if(a > mij){
            update(2 * nod + 1, mij + 1, dr, a, b);
        }else{
            update(2 * nod, st, mij, a, b);
            update(2 * nod + 1, mij + 1, dr, a, b);
        }
        aint[nod] = combine(aint[2 * nod], aint[2 * nod + 1]);
    }
}


int main()
{
    int n, p, a, b;

    fin >> n >> p;

    build(1, 1, n);
    while(p--){
        fin >> t;
        if(t == 3){
            fout << aint[1].best << "\n";
        }else{
            fin >> a >> b;
            update(1, 1, n, a, a + b - 1);
        }
    }
    return 0;
}