Cod sursa(job #1782507)

Utilizator BlackLordFMI Alex Oprea BlackLord Data 18 octombrie 2016 10:53:19
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3 kb
#include<fstream>

using namespace std;

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

const int N = 400100;

int n, m, i, x, y, v[N], s[N], d[N];

void go(int st, int dr, int nod) {
    if (st == dr) {
        s[nod] = d[nod] = v[nod] = dr - st + 1;
        return;
    }
    int mij = (st + dr) >> 1;
    go(st, mij, nod << 1);
    go(mij + 1, dr, (nod << 1) + 1);
    s[nod] = d[nod] = v[nod] = s[nod << 1] + s[(nod << 1) + 1];
}

void plecare(int st, int dr, int nod) {
    int mij = (st + dr) >> 1;
    if(st != dr) {
        if(v[nod] == 0) {
            v[(nod << 1)] = v[(nod << 1) + 1] = s[(nod << 1)] = s[(nod << 1) + 1] = d[(nod << 1)] = d[(nod << 1) + 1] = 0;
        }
        if (v[nod] == dr - st + 1) {
            v[nod << 1] = d[nod << 1] = s[nod << 1] = mij - st + 1;
            v[(nod << 1) + 1] = d[(nod << 1) + 1] = s[(nod << 1) + 1] = dr - mij;
        }
    }
    if (st >= x && dr <= y) {
        s[nod] = d[nod] = v[nod] = dr - st + 1;
        return;
    }
    if (x <= mij) {
        plecare(st, mij, nod << 1);
    }
    if (y > mij) {
        plecare(mij + 1, dr , (nod << 1) + 1);
    }
    if (s[(nod << 1)] == mij - st + 1) {
        s[nod] = s[(nod << 1)] + s[(nod << 1) + 1];
    } else {
        s[nod] = s[(nod << 1)];
    }
    if (d[(nod << 1) + 1] == dr - mij) {
        d[nod] = d[(nod << 1)] + d[(nod << 1) + 1];
    } else {
        d[nod] = d[(nod << 1) + 1];
    }
    v[nod] = max( v[(nod << 1)], max(v[(nod << 1) + 1], d[(nod << 1)] + s[(nod << 1) + 1]));
}

void sosire(int st, int dr, int nod) {
    int mij = (st + dr) >> 1;
    if (st != dr) {
        if (v[nod] == 0) {
            v[(nod << 1)] = v[(nod << 1) + 1] = s[(nod << 1)] = s[(nod << 1) + 1] = d[(nod << 1)] = d[(nod << 1) + 1] = 0;
        }
        if (v[nod] == dr - st + 1) {
            v[nod << 1] = d[nod << 1] = s[nod << 1] = mij - st + 1;
            v[(nod << 1) + 1] = d[(nod << 1) + 1] = s[(nod << 1) + 1] = dr - mij;
        }
    }
    if (st >= x && dr <= y) {
        s[nod] = d[nod] = v[nod] = 0;
        return;
    }
    if (x <= mij) {
        sosire(st, mij, nod << 1);
    }
    if (y > mij) {
        sosire(mij + 1, dr, (nod << 1) + 1);
    }
    if (s[(nod << 1)] == mij - st + 1) {
        s[nod] = s[(nod << 1)] + s[(nod << 1) + 1];
    } else {
        s[nod]=s[(nod<<1)];
    }
    if (d[(nod << 1) + 1] == dr - mij) {
        d[nod] = d[(nod << 1)] + d[(nod << 1) + 1];
    } else {
        d[nod] = d[(nod << 1) + 1];
    }
    v[nod] = max(v[(nod << 1)], max(v[(nod << 1) + 1], d[(nod << 1)] + s[(nod << 1) + 1]));
}

int main () {
    fin >> n >> m;
    go(1, n, 1);
    for (i = 1; i <= m; ++i) {
        fin >> x;
        if (x == 1) {
            fin >> x >> y;
            y += x - 1;
            sosire(1, n, 1);
        } else if (x == 2) {
            fin >> x >> y;
            y += x - 1;
            plecare(1, n, 1);
        } else {
            fout << v[1] << "\n";
        }
    }
    return 0;
}