Cod sursa(job #156467)

Utilizator flo_demonBunau Florin flo_demon Data 12 martie 2008 16:16:35
Problema Hotel Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.24 kb
#include <stdio.h>

#define MAX 100002

int n, p, op, stcam, nr, val;
int T[2*MAX+4], R_absolut[2*MAX+4], R_left[2*MAX+4], R_right[2*MAX+4];

void init(int nod, int st, int dr);
void update(int nod, int st, int dr, int a, int b, int val);

int main()
{
    FILE *fin = fopen("hotel.in", "r");
    FILE *fout = fopen("hotel.out", "w");
    fscanf(fin, "%d%d", &n, &p);
    init(1, 1, n);
    for (int i = 0; i < p; ++i)
    {
        fscanf(fin, "%d", &op);
        if (op == 1) val = 1;
        if (op == 2) val = -1;
        if (op == 3) val = 0;
        if (!val)
            fprintf(fout, "%d\n", R_absolut[1]);
        else
        {
            fscanf(fin, "%d%d", &stcam, &nr);
            update(1, 1, n, stcam, stcam+nr-1, val);
        } 
    }
    fclose(fin);
    fclose(fout);
}

void init(int nod, int st, int dr)
{
    R_left[nod] = R_right[nod] = R_absolut[nod] = dr-st+1;
    if (st == dr) return;
    int mijl = (st + dr) >> 1;
    init(2*nod, st, mijl);
    init(2*nod+1, mijl+1, dr);
}

void update(int nod, int st, int dr, int a, int b, int val)
{
   // if (a <= st && dr <= b)
    if (st == dr)
    {
        T[nod] += val;
        R_left[nod] = R_right[nod] = R_absolut[nod] = 0;  
        if (T[nod] == 0)
            R_left[nod] = R_right[nod] = R_absolut[nod] = (dr - st + 1);
    }
    else
    {
        int mijl = (st + dr) >> 1;
        if (a <= mijl)
            update(2*nod, st, mijl, a, b, val);
        if (b > mijl)
            update(2*nod+1, mijl+1, dr, a, b, val);
        if (R_absolut[2*nod] > R_absolut[2*nod+1])
        {
            R_absolut[nod] = R_absolut[2*nod];
            R_right[nod] = R_left[nod] = 0;
        }
        else
        {
            R_absolut[nod] = R_absolut[2*nod+1];
            R_right[nod] = R_left[nod] = 0;
        }
        if (R_right[2*nod] + R_left[2*nod+1] > R_absolut[nod])
            R_absolut[nod] = R_right[2*nod] + R_left[2*nod+1];
        R_left[nod] = R_left[2*nod];
        R_right[nod] = R_right[2*nod+1];
        if (R_right[2*nod] == mijl-st+1)
            R_left[nod] = R_right[2*nod] + R_left[2*nod+1];
        if (R_left[2*nod+1] == dr - (mijl+1) + 1)
            R_right[nod] = R_right[2*nod] + R_left[2*nod+1];
    }
}