Cod sursa(job #2015746)

Utilizator Alex18maiAlex Enache Alex18mai Data 27 august 2017 12:37:18
Problema Hotel Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.4 kb
#include <fstream>

using namespace std;

ifstream cin ("hotel.in");
ofstream cout ("hotel.out");

int best[400000];
int stmax[400000];
int drmax[400000];
int Lazy[400000];

void Update (int nod, int st, int dr, int MIN, int MAX, int val){
    if (Lazy[nod] == -1){
        if (Lazy)
        Lazy[nod * 2] = -1;
        Lazy[nod * 2 + 1] = -1;
        Lazy[nod] = 0;
    }
    if (val == -1){
        Lazy[nod] = 0;
    }
    if (MIN <= st && MAX >= dr){
        Lazy[nod] = val;
       // cout<<st<<" "<<dr<<" "<<val<<'\n';
        return;
    }
    int mij = (st + dr) / 2;
    if (MIN <= mij){
        Update (nod * 2, st, mij, MIN, MAX, val);
    }
    if (MAX > mij){
        Update (nod * 2 + 1, mij + 1, dr, MIN, MAX, val);
    }
}

void Querry (int nod, int st, int dr){
    if (Lazy[nod] == 1){
        best[nod] = dr - st + 1;
        stmax[nod] = dr - st + 1;
        drmax[nod] = dr - st + 1;
        return;
    }
    if (Lazy[nod] == -1){
        best[nod] = 0;
        stmax[nod] = 0;
        drmax[nod] = 0;
        return;
    }
    if (st == dr){
        return;
    }
    int mij = (st + dr) / 2;
    Querry (nod * 2, st, mij);
    Querry (nod * 2 + 1, mij + 1, dr);
    if (stmax[nod * 2] == mij - st + 1){
        stmax[nod] = stmax[nod * 2] + stmax[nod * 2 + 1];
    }
    else{
        stmax[nod] = stmax[nod * 2];
    }
    if (drmax[nod * 2 + 1] == dr - (mij + 1) + 1){
        drmax[nod] = drmax[nod * 2  + 1] + drmax[nod * 2];
    }
    else{
        drmax[nod] = drmax[nod * 2 + 1];
    }
    best[nod] = max(max(max(best[nod * 2] , best[nod * 2 + 1]) , drmax[nod * 2] + stmax[nod * 2 + 1]) , max (drmax[nod] , stmax[nod]));
    /*cout<<st<<" "<<dr<<" "<<best[nod]<<'\n';
    cout<<st<<" "<<dr<<" "<<stmax[nod]<<'\n';
    cout<<st<<" "<<dr<<" "<<drmax[nod]<<'\n';
    cout<<'\n';*/
}

int main() {
    int n, p;
    cin>>n>>p;
    for (int i=1; i<=n*4; i++){
        Lazy[i] = 1;
    }
    for (int i=1; i<=p; i++){
        int t;
        cin>>t;
        if (t == 3){
            Querry (1, 1, n);
            cout<<best[1]<<'\n';
        }
        if (t == 1){
            int start, rooms;
            cin>>start>>rooms;
            Update (1, 1, n, start, start + rooms - 1, -1);
            //cout<<'\n'<<'\n';
        }
        if (t == 2){
            int start, rooms;
            cin>>start>>rooms;
            Update (1, 1, n, start, start + rooms - 1, 1);
            //cout<<'\n'<<'\n';
        }
    }
    return 0;
}