Cod sursa(job #1525787)

Utilizator alexandra_udristoiuUdristoiu Alexandra Maria alexandra_udristoiu Data 15 noiembrie 2015 16:22:40
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.41 kb
#include<fstream>
#define DIM 100005
using namespace std;
int n, p, i, st, dr, t;
struct snod{
    int sst, sdr, smax, rest;
};
snod a[4 * DIM];
ifstream fin("hotel.in");
ofstream fout("hotel.out");
void update(int nod, int st, int dr, int p, int u, int val){
    if(p <= st && dr <= u){
        a[nod].rest = val;
        if(val == -1){
            a[nod].sst = a[nod].sdr = a[nod].smax = dr - st + 1;
        }
        else{
            a[nod].sst = a[nod].sdr = a[nod].smax = 0;
        }
    }
    else{
        int mid = (st + dr) / 2;

        if(a[nod].rest != 0){
            a[2 * nod].rest = a[2 * nod + 1].rest = a[nod].rest;
            if(a[nod].rest == 1){
                a[2 * nod].sst = a[2 * nod].sdr = a[2 * nod].smax = 0;
                a[2 * nod + 1].sst = a[2 * nod + 1].sdr = a[2 * nod + 1].smax = 0;
            }
            else{
                a[2 * nod].sst = a[2 * nod].sdr = a[2 * nod].smax = mid - st + 1;
                a[2 * nod + 1].sst = a[2 * nod + 1].sdr = a[2 * nod + 1].smax = dr - (mid + 1) + 1;
            }
            a[nod].rest = 0;
        }

        if(p <= mid){
            update(2 * nod, st, mid, p, u, val);

        }
        if(u > mid){
            update(2 * nod + 1, mid + 1, dr, p, u, val);
        }


        a[nod].smax = max(a[2 * nod].sdr + a[2 * nod + 1].sst, max(a[2 * nod].smax, a[2 * nod + 1].smax));
        a[nod].sst = a[2* nod].sst;
        if(a[nod].sst == mid - st + 1){
            a[nod].sst += a[2 * nod + 1].sst;
        }
        a[nod].sdr = a[2 * nod + 1].sdr;
         if(a[nod].sdr == dr - (mid + 1) + 1){
            a[nod].sdr += a[2 * nod].sdr;
        }
    }
}
void build(int nod, int st, int dr){
    if(st == dr){
        a[nod].smax = a[nod].sst = a[nod].sdr = 1;
    }
    else{
        int mid = (st + dr) / 2;
        build(2 * nod, st, mid);
        build(2 * nod + 1, mid + 1, dr);
        a[nod].smax = a[nod].sst = a[nod].sdr = dr - st + 1;
    }
}
int main(){
    fin>> n >> p;
    build(1, 1, n);
    for(i = 1; i <= p; i++){
        fin>> t;
        if(t == 3){
            fout<< a[1].smax <<"\n";
        }
        else{
            fin>> st >> dr;
            dr = st + dr - 1;
            if(t == 1){
                update(1, 1, n, st, dr, 1);
            }
            else{
                update(1, 1, n, st, dr, -1);
            }
        }
    }
    return 0;
}