Cod sursa(job #1654487)

Utilizator horiainfoTurcuman Horia horiainfo Data 17 martie 2016 08:52:53
Problema Hotel Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.45 kb
#include <fstream>
#include <cstdio>

#define NMAX 100001
using namespace std;
ofstream fout("hotel.out");

struct info{int inc, sf; };

int bst, N4;
int lazzy[NMAX * 4], v[NMAX * 4];

void propagate(int st, int dr, int node)
{
    v[node] += (dr - st + 1) * lazzy[node];
    if(node * 2 <= N4)
        lazzy[node * 2] += lazzy[node];
    if(node * 2 + 1 <= N4)
        lazzy[node * 2 + 1] += lazzy[node];
    lazzy[node] = 0;
}

info querry(int st, int dr, int node)
{
    info nod;

    propagate(st, dr, node);

    if(v[node] == 0)
    {
        nod.inc = dr; nod.sf = st;
        if(bst < dr - st + 1) bst = dr - st + 1;
        return nod;
    }

    if(st == dr)
    {
        nod.inc = nod.sf = -1;
        return nod;
    }

    int mij = (st + dr) / 2;
    info stInfo = querry(st, mij, node * 2);
    info drInfo = querry(mij + 1, dr, node * 2 + 1);

    if(bst < drInfo.inc - stInfo.sf + 1 && drInfo.inc != -1 && stInfo.sf != -1)
        bst = drInfo.inc - stInfo.sf + 1;

    if(stInfo.inc == mij && drInfo.inc != -1)
        stInfo.inc = drInfo.inc;
    if(drInfo.sf == mij + 1 && stInfo.sf != -1)
        drInfo.sf = stInfo.sf;

    nod.inc = stInfo.inc; nod.sf = drInfo.sf;

    if(nod.inc - st + 1 > bst && nod.inc != -1)
        bst = nod.inc - st + 1;
    if(dr - nod.sf + 1 > bst && nod.sf != -1)
        bst = dr - nod.sf + 1;

    return nod;
}

void update(int inc, int sf, int val, int st, int dr, int node)
{
    if(inc <= st && dr <= sf)
    {
        lazzy[node] += val;
        propagate(st, dr, node);
        return;
    }

    propagate(st, dr, node);

    int mij = (st + dr) / 2, sumST, sumDR;
    if(inc <= mij)
        update(inc, sf, val, st, mij, node * 2);
    if(mij < sf)
        update(inc, sf, val, mij + 1, dr, node * 2 + 1);
    v[node] = v[node * 2] + v[node * 2 + 1];
    return;
}

int main()
{
    int tasks, n, c, inc, lg;

    freopen("hotel.in", "r", stdin);
    scanf("%d%d", &n, &tasks);

    N4 = n * 4;

    for(int task = 1; task <= tasks; task++)
    {
        scanf("%d", &c);
        if(c <= 2)
        {
            scanf("%d%d", &inc, &lg);
            if(c == 1)
                update(inc, inc + lg - 1, 1, 1, n, 1);
            else
                update(inc, inc + lg - 1, -1, 1, n, 1);
        }
        else
        {
            bst = 0;
            querry(1, n, 1);
            fout <<bst <<'\n';
        }
    }

    return 0;
}