Cod sursa(job #786235)

Utilizator GrimpowRadu Andrei Grimpow Data 10 septembrie 2012 18:56:18
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.7 kb
#include <cstdio>
#define nmax 100012
#define f "hotel.in"
#define g "hotel.out"

typedef struct{
    int st;
    int dr;
    int bst;
    int use;
}tree;

tree t[nmax*3];
int n, m;

int MAX(int a, int b){
    if (a > b) return a;
    else return b;
}
void build(int nod, int st, int dr){

    t[nod].st = t[nod].dr = t[nod].bst = dr - st + 1;
    t[nod].use = -1;
    if (st == dr ) return;

    int mij = (st + dr) / 2;
    build(nod*2, st, mij);
    build(nod*2+1, mij+1, dr);
}

void udpate(int nod, int st, int dr, int a, int b, int val){

    if (a <= st && dr <= b){
        t[nod].use=val;
        t[nod].st=val*(dr-st+1);
        t[nod].dr=val*(dr-st+1);
        t[nod].bst=val*(dr-st+1);
        return;
    }

    int mij=(st + dr) / 2;

    if (t[nod].use!=-1){
        udpate(nod*2, st, mij, st, mij, t[nod].use);
        udpate(nod*2+1, mij+1, dr, mij+1, dr, t[nod].use);
        t[nod].use = -1;
    }

    if (a <= mij) udpate(nod*2, st, mij, a, b, val);
    if (b > mij) udpate(nod*2+1, mij+1, dr, a ,b, val);


    t[nod].bst = MAX(MAX(t[nod*2].bst, t[nod*2+1].bst), t[nod*2].dr + t[nod*2+1].st);

    if (t[nod*2].st == mij - st + 1) t[nod].st = t[nod*2].st + t[nod*2+1].st;
    else t[nod].st = t[nod*2].st;

    if (t[nod*2+1].dr == dr - mij) t[nod].dr = t[nod*2].dr + t[nod*2+1].dr;
    else t[nod].dr = t[nod*2+1].dr;


}
int main(){
    freopen(f, "r", stdin);
    freopen(g, "w", stdout);
    scanf("%d %d\n", &n, &m);

    build(1,1,n);


    for(int i=1; i<=m; i++){
        int tip, a, b;
        scanf("%d", &tip);
        if (tip<3){
            scanf("%d %d\n", &a, &b);
            udpate(1, 1, n, a, a+b-1, 1-tip%2);
        }
        else printf("%d\n",t[1].bst),scanf("\n");
    }
    return 0;
}