Cod sursa(job #1214131)

Utilizator mihail.jianuJianu Mihail mihail.jianu Data 29 iulie 2014 18:21:32
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.57 kb
#include<cstdio>
const int N=500000;
int n,qL,qR;
bool qF;
class Segment{
    public:
        int l,max,r,lazy;
        Segment(){
        }
        Segment(int left,int maximum,int right,int laziness){
            this->l=left;
            this->max=maximum;
            this->r=right;
            this->lazy=laziness;
        }
};
int max3(int a,int b,int c){
    if(b>a)
        a=b;
    if(c>a)
        return c;
    return a;
}
class SegmentTree{
    public:
        Segment v[2*N+1];
        void internalUpdate(int l,int r,int p){
            int m=(l+r)/2,d=r-l+1;
            if(this->v[p].lazy!=0){
                if(this->v[p].lazy==1){
                    this->v[p].max=d;
                    this->v[p].l=d;
                    this->v[p].r=d;
                }
                else{
                    this->v[p].max=0;
                    this->v[p].l=0;
                    this->v[p].r=0;
                }
                if(l<r){
                    this->v[p*2].lazy=this->v[p].lazy;
                    this->v[p*2+1].lazy=this->v[p].lazy;
                }
                this->v[p].lazy=0;
            }
            if(qR<l||r<qL)
                return;
            if(qL<=l&&r<=qR){
                if(qF){
                    this->v[p].max=r-l+1;
                    this->v[p].l=r-l+1;
                    this->v[p].r=r-l+1;
                }
                else{
                    this->v[p].max=0;
                    this->v[p].l=0;
                    this->v[p].r=0;
                }
                if(l<r)
                    if(qF){
                        this->v[p*2].lazy=1;
                        this->v[p*2+1].lazy=1;
                    }
                    else{
                        this->v[p*2].lazy=2;
                        this->v[p*2+1].lazy=2;
                    }
                this->v[p].lazy=0;
            }
            else{
                this->internalUpdate(l,m,p*2);
                this->internalUpdate(m+1,r,p*2+1);
                this->v[p].l=this->v[p*2].l;
                if(this->v[p*2].l==m-l+1)
                    this->v[p].l+=this->v[p*2+1].l;
                this->v[p].r=this->v[p*2+1].r;
                if(this->v[p*2+1].l==r-m)
                    this->v[p].r+=this->v[p*2].r;
                this->v[p].max=max3(this->v[p*2].r+this->v[p*2+1].l,this->v[p*2].max,this->v[p*2+1].max);
            }
        }
        void internalInit(int l,int r,int p){
            int d=r-l+1,m=(l+r)/2;
            this->v[p]=Segment(d,d,d,0);
            if(l==r)
                return;
            internalInit(l,m,p*2);
            internalInit(m+1,r,p*2+1);
        }
        void update(int l,int r,bool f){
            qL=l;
            qR=r;
            qF=f;
            this->internalUpdate(1,n,1);
        }
        void init(){
            this->internalInit(1,n,1);
        }
};
SegmentTree st;
int noQ,qType,qPos,qM;
FILE*in,*out;
void scan(){
    fscanf(in,"%d%d",&n,&noQ);
}
void init(){
    in=fopen("hotel.in","r");
    out=fopen("hotel.out","w");
    scan();
    st.init();
}
void scanTest(){
    fscanf(in,"%d",&qType);
    if(qType<3)
        fscanf(in,"%d%d",&qPos,&qM);
}
void initTest(){
    scanTest();
}
void solveTest(){
    if(qType==1)
        st.update(qPos,qPos+qM-1,false);
    else if(qType==2)
        st.update(qPos,qPos+qM-1,true);
    else
        fprintf(out,"%d\n",st.v[1].max);
}
int main(){
    init();
    while(noQ>0){
        initTest();
        solveTest();
        noQ--;
    }
    return 0;
}