Cod sursa(job #1045118)

Utilizator smaraldaSmaranda Dinu smaralda Data 30 noiembrie 2013 21:48:02
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.29 kb
#include<stdio.h>
#include<algorithm>
using namespace std;

const int NMAX = 132000;
int n,lmax[3 * NMAX + 10], seqLeft[3 * NMAX + 10], seqRight[3 * NMAX + 10], room[3 * NMAX + 10];

inline int max3 (int a, int b, int c) {
    int res = a;
    if(b > res) res = b;
    if(c > res) res = c;
    return res;
}

void updateRoom (int node, int left, int right) {
    int val;
    if(right > n)
        right = n;

    if(room[node] == 1)
        val = 0;
    else
        val = right - left + 1;

    if(val < 0)
        val = 0;
    lmax[node] = seqLeft[node] = seqRight[node] = val;
}

inline bool empty (int node, int left, int right) {
    if(right > n) right = n;
    return lmax[node] == right - left + 1;
}

inline bool full (int node) {
    return lmax[node] == 0;
}

void update (int node, int left, int right, int a, int b, int val) {
    if(left >= a && right <= b) {
        room[node] = val;
        updateRoom(node,left,right);
        return;
    }

    int leftSon = node * 2, rightSon = node * 2 + 1, mid = (left + right) / 2, half = (right - left + 1) / 2;

    if(empty(node, left, right) || full(node)) {
        room[node] = room[leftSon] = room[rightSon] = full(node);
        updateRoom(leftSon, left, mid);
        updateRoom(rightSon, mid + 1, right);
    }

    if(a <= mid)
        update(leftSon, left, mid, a, b, val);
    if(b > mid)
        update(rightSon, mid + 1, right, a, b, val);

    seqLeft[node] = empty(leftSon, left, mid) ? lmax[leftSon] + seqLeft[rightSon] : seqLeft[leftSon];
    seqRight[node] = empty(rightSon, mid + 1, right) ? lmax[rightSon] + seqRight[leftSon] : seqRight[rightSon];

    lmax[node] = max3(lmax[leftSon], lmax[rightSon], seqRight[leftSon] + seqLeft[rightSon]);
}

int main() {
    freopen("hotel.in","r",stdin);
    freopen("hotel.out","w",stdout);
    int p2, i, p, c, a, b;

    scanf("%d%d",&n,&p);

    for(p2 = 1; p2 <= n; p2 = p2 << 1);

    update(1,1,p2,1,n,0);

    for(i = 1; i <= p; i++) {
        scanf("%d",&c);
        if(c == 3) {
            printf("%d\n",lmax[1]);
            continue;
        }

        scanf("%d%d",&a,&b);
        if(c == 1)
            update(1,1,p2,a, a + b - 1, 1);
        if(c == 2)
            update(1,1,p2,a, a + b - 1, 0);
        }

    return 0;
}