Cod sursa(job #1473378)

Utilizator dorumusuroiFMI - Doru Musuroi dorumusuroi Data 19 august 2015 10:48:52
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.86 kb
#include <cstdio>
#define LS (2*cam)
#define RS ((2*cam)+1)
const char iname[] = "hotel.in";
const char oname[] = "hotel.out";
const int MAXN = 100005;

int n, p;
struct Type{
    int best, left, right;
}T[4*MAXN];
inline int Maxim(const int a, const int b){
    if(a < b) return b;
    return a;
}
void update(const int cam, const int inc, const int sf, const int pcam, const int ucam, const int op){
    if(inc > ucam || sf < pcam)return;
    if(pcam <= inc && sf <= ucam){
        if(op == 1)
            T[cam].best = T[cam].left = T[cam].right = 0;
        else
            T[cam].best = T[cam].left = T[cam].right = sf - inc + 1;
    }
    else{
        int mij = (inc+sf)>>1;

        if(!T[cam].best)
            T[LS].best = T[LS].left = T[LS].right = 0,
            T[RS].best = T[RS].left = T[RS].right = 0;

        if(T[cam].best == sf-inc+1)
            T[LS].best = T[LS].left = T[LS].right = mij-inc+1,
            T[RS].best = T[RS].left = T[RS].right = sf - mij;

        update(LS, inc, mij, pcam, ucam, op);
        update(RS, mij+1, sf, pcam, ucam, op);

        T[cam].best = Maxim(Maxim(T[LS].best, T[RS].best), T[LS].right + T[RS].left);

        T[cam].left = T[LS].left;
        T[cam].right = T[RS].right;
        if(T[LS].left == mij - inc + 1)
            T[cam].left += T[RS].left;
        if(T[RS].right == sf - mij)
            T[cam].right += T[LS].right;
    }
}

void solve(){
    for(int i = 0; i < p; ++i){
        int op, prim, nr;
        scanf("%d", &op);
        if(op == 3) printf("%d\n", T[1].best);
        else{
            scanf("%d %d\n", &prim, &nr);
            update(1, 1, n, prim, prim+nr-1, op);
        }
    }
}
int main()
{
    freopen(iname, "r", stdin);
    freopen(oname, "w", stdout);
    scanf("%d %d\n", &n, &p);
    update(1, 1, n, 1, n, 2);
    solve();
    return 0;
}