Cod sursa(job #1768097)

Utilizator antanaAntonia Boca antana Data 30 septembrie 2016 10:36:48
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.35 kb
#include <cstdio>
#include <ctype.h>
#define BUF_SIZE (1<<17)
#define MAXN 100000
#define DIM 400000

char buf[BUF_SIZE], buf2[BUF_SIZE];
int pos = BUF_SIZE, pos2, n, m, l1, l2, val;

struct nod{
    int prefix, sufix, maxs, lazy, l;
}aint[DIM];

FILE *fin, *fout;

inline char getChar();
inline int getInt();
inline void putch(char);
inline void scrie(int);

inline int maxim(int a, int b){
    return (a>b) ? a : b;
}

inline void modifica(nod &a)
{
    if(a.lazy == 1)
    {
        a.prefix = 0;
        a.sufix = 0;
        a.maxs = 0;
        return;
    }
    if(a.lazy == -1)
    {
        a.prefix = a.l;
        a.sufix  = a.l;
        a.maxs = a.l;
        return;
    }
}
inline nod join(nod a, nod b)
{
    nod ans;
    modifica(a), modifica(b);

    ans.l = a.l + b.l;
    ans.lazy = 0;

    if(a.maxs == a.l)
        ans.prefix = a.l + b.prefix;
    else ans.prefix = a.prefix;
    if(b.maxs == b.l)
        ans.sufix = b.l + a.sufix;
    else ans.sufix = b.sufix;

    ans.maxs = maxim(maxim(ans.prefix, ans.sufix), a.sufix + b.prefix);
    ans.maxs = maxim(ans.maxs, maxim(a.maxs, b.maxs));

    return ans;
}

void dfs(int st, int dr, int nod)
{
    if(st == dr){
        aint[nod].prefix = 1;
        aint[nod].sufix = 1;
        aint[nod].maxs = 1;
        aint[nod].l = 1;
        aint[nod].lazy = 0;
        return;
    }

    int m = (st+dr)/2;
    dfs(st, m, nod*2);
    dfs(m+1, dr, nod*2+1);

    aint[nod] = join(aint[nod*2], aint[nod*2+1]);
    aint[nod].lazy = 0;
}

inline void trimite(int nod)
{
    aint[nod*2].lazy = aint[nod].lazy;
    aint[nod*2+1].lazy = aint[nod].lazy;
    modifica(aint[nod*2]), modifica(aint[nod*2+1]);
    aint[nod].lazy = 0;
}

void update(int st, int dr, int nod)
{
    if(l1 <=st && dr <= l2)
    {
        aint[nod].lazy = val;
        modifica(aint[nod]);
        return;
    }

    int m=(st+dr)/2;
    if(aint[nod].lazy) trimite(nod);

    if(l1 <= m) update(st, m, nod*2);
    if(l2 > m) update(m+1, dr, nod*2+1);

    aint[nod] = join(aint[nod*2], aint[nod*2+1]);
}

int main()
{

    fin = fopen("hotel.in", "r");
    fout = fopen("hotel.out", "w");

    int i, a;

    n = getInt(); m = getInt();

    dfs(1, n, 1);

    for(i=1;i<=m;++i)
    {
        a = getInt();
        if(a == 3){
            scrie(aint[1].maxs);
            putch('\n');
        }
        if(a==2 || a==1)
        {
            l1 = getInt();
            l2 = getInt();
            l2 = l2 + l1 - 1;
            val = ((a==1) ? 1 : -1);
            update(1, n, 1);
        }
    }
    fwrite(buf2, 1, pos2, fout);

    fclose(fin);
    fclose(fout);

    return 0;
}

inline char getChar()
{
    if(pos == BUF_SIZE) pos=0, fread(buf, 1, BUF_SIZE, fin);
    return buf[pos++];
}

inline int getInt()
{
    int nr=0;
    char c;

    c = getChar();
    while(!isdigit(c)) c = getChar();
    while(isdigit(c))
    {
        nr = nr*10 + c-'0';
        c = getChar();
    }

    return nr;
}

inline void putch(char c)
{
    buf2[pos2++] = c;
    if(pos2 == BUF_SIZE) pos2=0, fwrite(buf2, 1, BUF_SIZE, fout);
}
inline void scrie(int nr)
{
    char s[10];
    int k=0;

    do{
        s[k++] = nr%10 + '0';
        nr /= 10;
    }while(nr);

    while(k)
    {
        --k;
        putch(s[k]);
    }
}