Cod sursa(job #2443281)

Utilizator IoanaDraganescuIoana Draganescu IoanaDraganescu Data 27 iulie 2019 12:06:19
Problema Hotel Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.11 kb
#include <iostream>
#include <fstream>

using namespace std;

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

struct arbore{
    int lmax, st, dr;
}a[400005];
int n, p, v[100005];

void start(int nod, int st, int dr){
    int l = dr - st + 1;
    a[nod].lmax = l;
    a[nod].st = l;
    a[nod].dr = l;
    if (st == dr)
        return;
    int mij = (st + dr) >> 1;
    int ls = 2 * nod, rs = ls + 1;
    start(ls, st, mij);
    start(rs, mij + 1, dr);
}

void update(int nod, int st, int dr){
    if (!v[nod])
        return;
    int ls = 2 * nod, rs = ls + 1, l = dr - st + 1;
    if (v[nod] == 1){
        a[nod].st = 0;
        a[nod].dr = 0;
        a[nod].lmax = 0;
        v[ls] = 1;
        v[rs] = 1;
        v[nod] = 0;
    }
    else if (v[nod] == 2){
        a[nod].st = l;
        a[nod].dr = l;
        a[nod].lmax = l;
        v[ls] = 2;
        v[rs] = 2;
        v[nod] = 0;
    }
}

void make(int nod, int st, int dr, int qs, int qd, int cer){
    if (qs <= st && dr <= qd){
        v[nod] = cer;
        update(nod, st, dr);
        return;
    }
    update(nod, st, dr);
    int mij = (st + dr) >> 1;
    int ls = 2 * nod, rs = ls + 1;
    if (qs <= mij)
        make(ls, st, mij, qs, qd, cer);
    else
        update(ls, st, mij);
    if (qd > mij)
        make(rs, mij + 1, dr, qs, qd, cer);
    else
        update(rs, mij + 1, dr);
    a[nod].lmax = max(max(a[ls].lmax, a[rs].lmax), a[ls].dr + a[rs].st);
    if (a[ls].lmax == mij - st + 1)
        a[nod].st = a[ls].lmax + a[rs].st;
    else
        a[nod].st = a[ls].st;
    if (a[rs].lmax == dr - mij)
        a[nod].dr = a[rs].lmax + a[ls].dr;
    else
        a[nod].dr = a[rs].dr;
}

int main(){
    fin >> n >> p;
    start(1, 1, n);
    while (p--){
        int c, i, m;
        fin >> c;
        if (c == 1){
            fin >> i >> m;
            make(1, 1, n, i, m + i - 1, 1);
        }
        else if (c == 2){
            fin >> i >> m;
            make(1, 1, n, i, m + i - 1, 2);
        }
        else
            fout << a[1].lmax << '\n';
    }
    return 0;
}