Cod sursa(job #2007936)

Utilizator MaligMamaliga cu smantana Malig Data 4 august 2017 16:42:53
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.28 kb
#include <iostream>
#include <fstream>
#include <vector>

#define ll long long
#define pb push_back
using namespace std;
ifstream in("hotel.in");
ofstream out("hotel.out");

const int NMax = 1e5 + 5;
const int arbMax = 262144 + 5;

int N,M;
short lazy[arbMax];

struct elem {
    int free = 0,st = 0,dr = 0;
}arb[arbMax];

void update(int,int,int,int,int,bool);

int main() {
    /*
    int pw = 1;
    while (pw < NMax) {
        pw <<= 1;
    }
    pw <<= 1;
    out<<pw<<'\n';
    */

    in>>N>>M;
    update(1,1,N,1,N,true);

    while (M--) {
        int tip,a,b;
        in>>tip;
        if (tip == 3) {
            out<<arb[1].free<<'\n';
        }
        else {
            in>>a>>b;
            b = a+b-1;

            if (tip == 1) {
                update(1,1,N,a,b,false);
            }
            else {
                update(1,1,N,a,b,true);
            }
        }
    }

    in.close();out.close();
    return 0;
}

void update(int node,int st,int dr,int a,int b,bool golire) {
    //cout<<node<<' '<<st<<' '<<dr<<' '<<a<<' '<<b<<' '<<golire<<'\n';
    int fs = node<<1, ss = fs+1;

    if (lazy[node] != 0) {
        if (lazy[node] == 1) {
            arb[node].free = arb[node].st = arb[node].dr = 0;
        }
        else if (lazy[node] == -1) {
            arb[node].free = arb[node].st = arb[node].dr = (dr-st+1);
        }

        if (st != dr) {
            lazy[fs] = lazy[node];
            lazy[ss] = lazy[node];
        }
        lazy[node] = 0;
    }

    //cout<<arb[node].free<<' '<<arb[node].st<<' '<<arb[node].dr<<"\n";

    if (b < st || dr < a) {
        /*
        cout<<node<<' '<<st<<' '<<dr<<' '<<a<<' '<<b<<' '<<golire<<'\n';
        cout<<arb[node].free<<' '<<arb[node].st<<' '<<arb[node].dr<<"\n\n";
        //*/
        return;
    }

    if (a <= st && dr <= b) {
        if (golire) {
            arb[node].free = arb[node].st = arb[node].dr = (dr-st+1);
            if (st != dr) {
                lazy[fs] = -1;
                lazy[ss] = -1;
            }
        }
        else {
            arb[node].free = arb[node].st = arb[node].dr = 0;
            if (st != dr) {
                lazy[fs] = 1;
                lazy[ss] = 1;
            }
        }
        /*
        cout<<node<<' '<<st<<' '<<dr<<' '<<a<<' '<<b<<' '<<golire<<'\n';
        cout<<arb[node].free<<' '<<arb[node].st<<' '<<arb[node].dr<<"\n\n";
        //*/
        return;
    }

    int mij = (st+dr)>>1;
    //if (a <= mij)
        update(fs,st,mij,a,b,golire);
    //if (mij+1 <= b)
        update(ss,mij+1,dr,a,b,golire);
    arb[node].st = arb[fs].st + ((arb[fs].free == mij-st+1) ? arb[ss].st : 0);
    arb[node].dr = arb[ss].dr + ((arb[ss].free == dr-(mij+1)+1) ? arb[fs].dr : 0);
    arb[node].free = max(arb[fs].dr + arb[ss].st,max(arb[fs].free,arb[ss].free));

    /*
    cout<<node<<' '<<st<<' '<<dr<<' '<<a<<' '<<b<<' '<<golire<<'\n';
    cout<<arb[node].free<<' '<<arb[node].st<<' '<<arb[node].dr<<"\n\n";
    //*/

    /*
    if (arb[node].free == (dr-st+1)) {
        arb[node].st = arb[node].dr = dr-st+1;
    }
    //*/

    /*
    cout<<node<<' '<<st<<' '<<dr<<' '<<a<<' '<<b<<' '<<golire<<'\n';
    cout<<arb[node].free<<' '<<arb[node].st<<' '<<arb[node].dr<<"\n\n";
    //*/
}