Cod sursa(job #385279)

Utilizator yotherockerPuia Tudor yotherocker Data 22 ianuarie 2010 15:04:25
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.92 kb
//"

#include <cstdio>
 
const int MAX_N = 100010;
 
int n, m;
int s[MAX_N * 3], d[MAX_N * 3], max[MAX_N * 3];

void initialize(int nod, int st, int dr)
{
    int mij = st + ((dr - st) >> 1);
    max[nod] = s[nod] = d[nod] = dr - st + 1;
     
    if (st == dr) return;
     
    initialize(nod * 2, st, mij);
    initialize(nod * 2 + 1, mij + 1, dr);
}
 
int maxim(int x1, int x2, int x3, int x4, int x5)
{
    int ret = x1;
    ret = (ret > x2) ? ret : x2;
    ret = (ret > x3) ? ret : x3;
    ret = (ret > x4) ? ret : x4;
    ret = (ret > x5) ? ret : x5;
     
    return ret;
}

void update(int nod, int st, int dr, int x, int y, int z)
{
    int mij = st + ((dr - st) >> 1);
     
    if (x <= st && dr <= y)
    {
        max[nod] = s[nod] = d[nod] = z * (dr - st + 1);
        return;
    }
     
    if (max[nod] == (1 - z) * (dr - st + 1))
    {
        max[nod * 2] = s[nod * 2] = d[nod * 2] = (1 - z) * (mij - st + 1);
        max[nod * 2 + 1] = s[nod * 2 + 1] = d[nod * 2 + 1] = (1 - z) * (dr - mij);
    }     
    if (x <= mij) update(nod * 2, st, mij, x, y, z);
    if (y > mij) update(nod * 2 + 1, mij + 1, dr, x, y, z);
     
    s[nod] = s[nod * 2];
    if (s[nod * 2] == mij - st + 1) s[nod] += s[nod * 2 + 1];
     
    d[nod] = d[nod * 2 + 1];
    if (d[nod * 2 + 1] == dr - mij) d[nod] += d[nod * 2];
     
    max[nod] = maxim(max[nod * 2], max[nod * 2 + 1], s[nod], d[nod], d[nod * 2] + s[nod * 2 + 1]);
}
 
int main()
{
    int x, y, z;
    freopen("hotel.in", "r", stdin);
   freopen("hotel.out", "w", stdout);
     
    scanf("%d %d", &n, &m);
     
    initialize(1, 1, n);
     
    for (; m; --m)
    {
        scanf("%d", &z);
         
        if (z <= 2)
        {
            scanf("%d %d", &x, &y);
           y = x + y - 1;
           update(1, 1, n, x, y, z - 1);
        }
       else printf("%d\n", max[1]);
    }
}
//"  (by Radu Zernoveanu )