Cod sursa(job #1606575)

Utilizator algebristulFilip Berila algebristul Data 20 februarie 2016 12:59:06
Problema Hotel Scor 100
Compilator cpp Status done
Runda contest_7 Marime 2.49 kb
#include <cstdio>
#include <algorithm>

using namespace std;

const int nmax = 100005;

struct nod {
    int cntLeft;
    int longest;
    int cntRight;
} AINT[nmax * 6 + 66];
int n, p;

void update(int n, int l, int r, int a, int b, bool action) {
    if (a <= l && r <= b) {
        if (action) {
            AINT[n].longest = r - l + 1;
            AINT[n].cntLeft = r - l + 1;
            AINT[n].cntRight = r - l + 1;
        } else {
            AINT[n].longest = 0;
            AINT[n].cntLeft = 0;
            AINT[n].cntRight = 0;
        }
        return;
    }

    int mid = l + (r - l) / 2;

    if (action && AINT[n].longest == 0) {
        AINT[2*n].longest = 0;
        AINT[2*n].cntLeft = 0;
        AINT[2*n].cntRight = 0;

        AINT[2*n+1].longest = 0;
        AINT[2*n+1].cntLeft = 0;
        AINT[2*n+1].cntRight = 0;

        AINT[n].longest = r - l + 1;
    }

    if (!action && AINT[n].longest == r - l + 1) {
        AINT[2*n].longest = mid - l + 1;
        AINT[2*n].cntLeft = mid - l + 1;
        AINT[2*n].cntRight = mid - l + 1;

        AINT[2*n+1].longest = r - mid;
        AINT[2*n+1].cntLeft = r - mid;
        AINT[2*n+1].cntRight = r - mid;

        AINT[n].longest = 0;
        AINT[n].cntLeft = 0;
        AINT[n].cntRight = 0;
    }

    if (a <= mid)
        update(2*n, l, mid, a, b, action);
    if (mid < b)
        update(2*n+1, mid + 1, r, a, b, action);

    if (AINT[2*n].longest == mid - l + 1)
        AINT[n].cntLeft = AINT[2*n].longest + AINT[2*n+1].cntLeft;
    else
        AINT[n].cntLeft = AINT[2*n].cntLeft;

    if (AINT[2*n+1].longest == r - mid)
        AINT[n].cntRight = AINT[2*n+1].longest + AINT[2*n].cntRight;
    else
        AINT[n].cntRight = AINT[2*n+1].cntRight;

    AINT[n].longest = max(AINT[2*n].longest, AINT[2*n+1].longest);
    AINT[n].longest = max(AINT[n].longest, AINT[2*n].cntLeft);
    AINT[n].longest = max(AINT[n].longest, AINT[2*n+1].cntRight);
    AINT[n].longest = max(AINT[n].longest, AINT[2*n].cntRight + AINT[2*n+1].cntLeft);
}

int main() {
    freopen("hotel.in", "r", stdin);
    freopen("hotel.out", "w", stdout);

    scanf("%d %d", &n, &p);
    update(1, 1, n, 1, n, true);

    for (int i = 1, t, x, y; i <= p; i++) {
        scanf("%d", &t);

        if (t == 3) {
            printf("%d\n", AINT[1].longest);
            continue;
        }

        scanf("%d %d", &x, &y);

        update(1, 1, n, x, x + y - 1, t == 2);
    }

    return 0;
}