Cod sursa(job #3362672)

Utilizator pkseVlad Bondoc pkse Data 11 august 2026 13:02:16
Problema Hotel Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.52 kb
#include <bits/stdc++.h>
using namespace std;

const int nmax = 100'000;

struct aint {
    aint(int n) : n(n) {
        for(int i = n; i < (n << 1); i ++) {
            a[i] = node(1);
        }
        for(int i = n - 1; i; i --) {
            a[i] = a[i << 1] + a[i << 1 | 1];
        }
    }
    int n;
    struct node {
        node() {}
        node(int v) : ans(v), pans(v), sans(v), lg(v) {}
        int ans, pans, sans, lg;
        int lazy = -1;
        
    } a[nmax << 1];

    friend node operator+(node x, node y) {
        node z;
        z.ans = max(max(x.ans, y.ans), x.sans + y.pans);
        if(x.ans == x.lg)
            z.pans = x.ans + y.pans;
        else
            z.pans = x.pans;
        if(y.ans == y.lg)
            z.sans = x.sans + y.ans;
        else
            z.sans = y.sans;
        z.lg = x.lg + y.lg;
        return z;
    }

    void apply(int u, int v) {
        a[u].ans = a[u].pans = a[u].sans = v * a[u].lg;
        a[u].lazy = v;
    }

    void prop(int u) {
        if(a[u].lazy == -1) return;
        apply(u << 1, a[u].lazy);
        apply(u << 1 | 1, a[u].lazy);
        a[u].lazy = -1;
    }

    void down(int u) {
        for(int i = 20; i; i --) {
            if(u >> i) prop(u >> i);
        }
    }

    void up(int u) {
        for(u >>= 1; u; u >>= 1) {
            if(a[u].lazy == -1)
                a[u] = a[u << 1] + a[u << 1 | 1];
        }
    }

    void update(int l, int r, int v) {
        int l0 = l += n, r0 = r += n;
        down(l0); down(r0);
        for(r ++; l < r; l >>= 1, r >>= 1) {
            if(l & 1) apply(l ++, v);
            if(r & 1) apply(-- r, v);
        }
        up(l0); up(r0);
    }

    int query(int l, int r) {
        node ansl(0), ansr(0);
        down(l += n); down(r += n);
        for(r ++; l < r; l >>= 1, r >>= 1) {
            if(l & 1) ansl = ansl + a[l ++];
            if(r & 1) ansr = a[-- r] + ansr;
        }
        return (ansl + ansr).ans;
    }
};

int main() {
    ifstream cin("hotel.in");
    ofstream cout("hotel.out");
    int n, q; cin >> n >> q;
    unique_ptr<aint> str = make_unique<aint>(n);
    for(int t, l, r; q --;) {
        cin >> t;
        if(t == 3)
            cout << str->query(0, n - 1) << '\n';
        else if(t == 1) {
            cin >> l >> r; l --;
            r += l; r --;
            str->update(l, r, 0);
        } else {
            cin >> l >> r; l --;
            r += l; r --;
            str->update(l, r, 1);
        }
    }
}