Cod sursa(job #2327357)

Utilizator priboiraduPriboi Radu Bogdan priboiradu Data 24 ianuarie 2019 17:39:17
Problema Hotel Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.69 kb
#include <bits/stdc++.h>

int pf[400000], sf[400000], t[400000], mx[400000];

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

void arbore( int st, int dr, int p ) {
    if ( st == dr ) {
        pf[p] = sf[p] = t[p] = mx[p] = 1;
    } else {
        arbore( st, ( st + dr ) / 2, 2 * p );
        arbore( ( st + dr ) / 2 + 1, dr, 2 * p + 1 );
        if ( pf[2 * p] == t[2 * p] ) {
            pf[p] = pf[2 * p] + pf[2 * p + 1];
        } else {
            pf[p] = pf[2 * p];
        }
        if ( sf[2 * p + 1] == t[2 * p + 1] ) {
            sf[p] = sf[2 * p + 1] + sf[2 * p];
        } else {
            sf[p] = sf[2 * p + 1];
        }
        t[p] = t[2 * p] + t[2 * p + 1];
        mx[p] = max( max( mx[2 * p], mx[2 * p + 1] ), max( pf[p], sf[p] ) );
    }
}

void update( int st, int dr, int p, int x, int i, int j ) {
    if ( st == dr ) {
        pf[p] = sf[p] = x;
    } else {
        if ( i <= st && dr <= j ) {
            pf[p] = sf[p] = x * t[p];
            mx[p] = max( pf[p], sf[p] );
        } else {
            if ( pf[p] == t[p] && sf[p] == t[p] ) {
                pf[2 * p] = sf[2 * p] = t[2 * p];
                pf[2 * p + 1] = sf[2 * p + 1] = t[2 * p + 1];
            } else if ( pf[p] == 0 && sf[p] == 0 ) {
                pf[2 * p] = sf[2 * p] = 0;
                pf[2 * p + 1] = sf[2 * p + 1] = 0;
            }
            if ( i <= ( st + dr ) / 2 )
                update( st, ( st + dr ) / 2, 2 * p, x, i, j );
            if ( j >= ( st + dr ) / 2 + 1 )
                update( ( st + dr ) / 2 + 1, dr, 2 * p + 1, x, i, j );
            if ( pf[2 * p] == t[2 * p] ) {
                pf[p] = pf[2 * p] + pf[2 * p + 1];
            } else {
                pf[p] = pf[2 * p];
            }
            if ( sf[2 * p + 1] == t[2 * p + 1] ) {
                sf[p] = sf[2 * p + 1] + sf[2 * p];
            } else {
                sf[p] = sf[2 * p + 1];
            }
            mx[p] = max( max( mx[2 * p], mx[2 * p + 1] ), max( pf[p], sf[p] ) );
            mx[p] = max( mx[p], sf[2 * p] + pf[2 * p + 1] );
        }
    }
}

int main() {
    FILE *fin, *fout;
    int n, p, i, c, a, b, x;
    fin = fopen( "hotel.in", "r" );
    fout = fopen( "hotel.out", "w" );
    fscanf( fin, "%d%d", &n, &p );
    arbore( 1, n, 1 );
    for ( i = 0; i < p; i++ ) {
        fscanf( fin, "%d", &c );
        if ( c == 3 ) {
            fprintf( fout, "%d\n", mx[1] );
        } else if ( c == 1 ) {
            fscanf( fin, "%d%d", &a, &b );
            x = 0;
            update( 1, n, 1, x, a, b + a - 1 );
        } else if ( c == 2 ) {
            fscanf( fin, "%d%d", &a, &b );
            x = 1;
            update( 1, n, 1, x, a, b + a - 1 );
        }
    }
    fclose( fin );
    fclose( fout );
    return 0;
}