Cod sursa(job #1289341)

Utilizator catalincraciunCraciun Catalin catalincraciun Data 9 decembrie 2014 20:06:21
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.73 kb
#include <iostream>
#include <fstream>

#define NMax 100005

using namespace std;

ifstream f("hotel.in");
ofstream g("hotel.out");

int x, y;
int n, m, type;
int a[6*NMax], b[6*NMax], c[6*NMax];

inline int maxim (int a, int b) { return (a > b)?a:b; };

void update(int nod, int l, int r, int val) {

    if (x <= l && r <= y) {
        c[nod] = a[nod] = b[nod] = val * (r - l + 1);
        return;
    }

    int mij = (l+r)/2;
    if (c[nod] == 0) {
        c[2*nod] = a[2*nod] = b[2*nod] = 0;
        c[2*nod+1] = a[2*nod+1] = b[2*nod+1] = 0;
    } else if (c[nod] == r - l + 1) {
        c[2*nod]=a[2*nod]=b[2*nod]=mij-l+1;
        c[2*nod+1]=a[2*nod+1]=b[2*nod+1]=r-mij;
    }

    /// Updating sons
    if (x <= mij)
        update(2*nod, l, mij, val);
    if (y > mij)
        update(2*nod+1, mij+1, r, val);

    if (a[2*nod] == mij-l+1)
        a[nod] = a[2*nod]+a[2*nod+1];
    else
        a[nod] = a[2*nod];

    if (b[2*nod+1] == r - mij)
        b[nod] = b[2*nod] + b[2*nod+1];
    else
        b[nod] = b[2*nod+1];

    c[nod] = maxim(maxim(b[nod], a[nod]), maxim(b[2*nod] + a[2*nod+1], maxim(c[2*nod], c[2*nod+1])));
}

void read() {

    f>>n>>m;
    x=1;
    y=n;
    update(1, 1, n, 1);
    for (int i=1;i<=m;i++) {
        int index, len;
        f>>type;
        if(type==3)
            g<<c[1]<<"\n";
        else if(type==1) {
            f>>index>>len;
            x = index;
            y = index + len - 1;
            update(1, 1, n, 0);
        } else {
            f>>index>>len;
            x = index;
            y = index + len - 1;
            update(1, 1, n, 1);
        }
    }
}

int main() {

    read();

    f.close(); g.close();
    return 0;
}