Cod sursa(job #7712)

Utilizator alextheroTandrau Alexandru alexthero Data 22 ianuarie 2007 02:54:31
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.4 kb
#include <stdio.h>
#include <algorithm>

#define arbmax 262144

using namespace std;

int n1,n2,n,p,o;
int a1[arbmax],a2[arbmax],a3[arbmax];
char type[arbmax];

void update(int nod,int st,int dr) {
    if(n1<=st && dr<=n2) {
        type[nod] = 1;
        a1[nod] = a2[nod] = a3[nod] = 0;
    }   
    else {  
        int mij=(st+dr)/2;
        if(type[nod] == 0) {
            type[nod*2] = 0;
            a1[nod*2] = a2[nod*2] = a3[nod*2] = mij-st+1;
            type[nod*2+1] = 0;
            a1[nod*2+1] = a2[nod*2+1] = a3[nod*2+1] = dr-mij;
        }
        type[nod] = 2;
        if(n1<=mij) update(nod*2,st,mij);
        if(mij<n2) update(nod*2+1,mij+1,dr);
        a2[nod] = a2[nod*2];
        if(type[nod*2] == 0) a2[nod]+=a2[nod*2+1];
        a3[nod] = a3[nod*2+1];
        if(type[nod*2+1] == 0) a3[nod]+=a3[nod*2];
        a1[nod] = max(a1[nod*2],max(a1[nod*2+1],a3[nod*2]+a2[nod*2+1]));
        if(type[nod*2] == 1 && type[nod*2+1] == 1) 
            type[nod] = 1;

    }   
}

void update1(int nod,int st,int dr) {
    if(n1<=st && dr<=n2) {
        type[nod] = 0;
        a1[nod] = a2[nod] = a3[nod] = dr-st+1;
    }
    else {
        int mij = (st+dr)/2;
        if(type[nod] == 1) {
            type[nod*2] = 1;
            a1[nod*2] = a2[nod*2] = a3[nod*2] = 0;
            type[nod*2+1] = 1;
            a1[nod*2+1] = a2[nod*2+1] = a3[nod*2+1] = 0;
        }
        type[nod] = 2;
        if(n1<=mij) update1(nod*2,st,mij);
        if(mij<n2) update1(nod*2+1,mij+1,dr);
        a2[nod] = a2[nod*2];
        if(type[nod*2] == 0) a2[nod]+=a2[nod*2+1];
        a3[nod] = a3[nod*2+1];
        if(type[nod*2+1] == 0) a3[nod]+=a3[nod*2];
        a1[nod] = max(a1[nod*2],max(a1[nod*2+1],a3[nod*2]+a2[nod*2+1]));
        if(type[nod*2] == 0 && type[nod*2+1] == 0) 
            type[nod] = 0;
    }
}

int main() {    
    freopen("hotel.in","r",stdin);
    freopen("hotel.out","w",stdout);

    scanf("%d %d\n",&n,&p);
    type[1] = 0;
    a1[1] = a2[1] = a3[1] = n;
    for(int i=1;i<=p;i++) {
        scanf("%d",&o);
        if(o==3) printf("%d\n",a1[1]);
        else {
            scanf("%d%d",&n1,&n2);
            n2=n1+n2-1;
            if(o==1) update(1,1,n);
            else {
                update1(1,1,n);
//                for(int i=1;i<=31;i++) printf("%d %d %d %d\n",a1[i],a2[i],a3[i],type[i]);
            }
        }
    }
    return 0;
}