Cod sursa(job #3196737)

Utilizator vladdobro07vladdd vladdobro07 Data 24 ianuarie 2024 18:16:34
Problema Hotel Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 5.37 kb
#include <bits/stdc++.h>
using namespace std;
ifstream faina("hotel.in");
ofstream gaina("hotel.out");
struct NOD {
        int pref=0,suff=0,maxx=0,poz=0,lazy=-1;
};
NOD join(NOD st,NOD dr,int st1,int dr1,int st2,int dr2) {
        NOD rez;
        int unite=st.suff+dr.pref;
        rez.pref=st.pref;
        rez.suff=dr.suff;
        rez.maxx=unite;
        rez.poz=dr1-st.suff+1;
        if(st.maxx>rez.maxx) {
                rez.maxx=st.maxx;
                rez.poz=st.poz;
        }
        if(dr.maxx>rez.maxx) {
                rez.maxx=dr.maxx;
                rez.poz=dr.poz;
        }
        if(st.pref==(dr1-st1+1)) {
                rez.pref=st.pref+dr.pref;
        }
        if(dr.suff==(dr2-st2+1)) {
                rez.suff=dr.suff+st.suff;
        }
        if(st.pref>rez.maxx) {
                rez.maxx=st.pref;
                rez.poz=st1;
        }
        if(dr.suff>rez.maxx) {
                rez.maxx=dr.suff;
                rez.poz=dr2-dr.suff+1;
        }
        return rez;
}
struct AINT {
        vector<NOD> aint;
        AINT(int len) {
                aint.resize(4*len+1);
        }
        void update(int nod, int st, int dr, int l, int r,int val) {
                if(st>dr||l>r||st>r||dr<l)
                        return;
                if(st==l&&dr==r) {
                        if(val==1)
                                aint[nod].lazy=1;
                        else
                                aint[nod].lazy=0;
                } else {
                        int mid=(st+dr)/2;
                        if(aint[nod].lazy==0||aint[nod].lazy==1){
                                aint[nod*2].lazy=aint[nod].lazy;
                                aint[nod*2+1].lazy=aint[nod].lazy;
                                aint[nod].lazy=2;
                        }
                        update(nod*2,st,mid,l,min(r,mid),val);
                        update(nod*2+1,mid+1,dr,max(mid+1,l),r,val);
                        if((aint[nod*2].lazy!=-1)||(aint[nod*2+1].lazy!=-1))
                                aint[nod].lazy=2;
                }
        }
        void build(int nod,int st,int dr) {
                if(st>dr)
                        return;
                if(st==dr) {
                        aint[nod].pref=1;
                        aint[nod].suff=1;
                        aint[nod].maxx=1;
                        aint[nod].poz=st;
                } else {
                        int mid=(st+dr)/2;
                        build(nod*2,st,mid);
                        build(nod*2+1,mid+1,dr);
                        aint[nod]=join(aint[nod*2],aint[nod*2+1],st,mid,mid+1,dr);
                }
        }
        void resolve(int nod,int st,int dr) {
                if(st>dr)
                        return;
                if(aint[nod].lazy!=-1) {
                        if(st==dr) {
                                if(aint[nod].lazy==1) {
                                        aint[nod].pref=0;
                                        aint[nod].suff=0;
                                        aint[nod].maxx=0;
                                } else if(aint[nod].lazy==0) {
                                        aint[nod].pref=1;
                                        aint[nod].suff=1;
                                        aint[nod].maxx=1;
                                }
                                aint[nod].poz=st;
                                aint[nod].lazy=-1;
                                return;
                        }
                        int mid=(st+dr)/2;
                        if(aint[nod].lazy==0||aint[nod].lazy==1){
                                aint[nod*2].lazy=aint[nod].lazy;
                                aint[nod*2+1].lazy=aint[nod].lazy;
                                aint[nod].lazy=2;
                        }
                        resolve(nod*2,st,mid);
                        resolve(nod*2+1,mid+1,dr);
                        aint[nod]=join(aint[nod*2],aint[nod*2+1],st,mid,mid+1,dr);
                        aint[nod].lazy=-1;
                }
        }
        void showvect(int nod,int st,int dr) {
                if(st>dr||st<1||dr<1)
                        return;
                //gaina<<nod<<", "<<st<<" "<<dr<<", "<<aint[nod].maxx<<", "<<aint[nod].pref<<" "<<aint[nod].suff<<", "<<aint[nod].poz<<"\n";
                if(st==dr)
                        return;
                int mid=(st+dr)/2;
                showvect(nod*2,st,mid);
                showvect(nod*2+1,mid+1,dr);
        }
};
#define CUM 1
#define LIV 2
#define QUE 3
int main() {
        int n,p,c,x,l;
        faina>>n>>p;
        AINT aint(n);
        aint.build(1,1,n);
        for(int i=1; i<=p; i++) {
                faina>>c;
                if(c==QUE) {
                        //aint.showvect(1,1,n);
                        //gaina<<"\n";
                        aint.resolve(1,1,n);
                        //gaina<<"\n";
                        //aint.showvect(1,1,n);
                        gaina<<aint.aint[1].maxx<<"\n";
                } else if(c==CUM) {
                        faina>>x>>l;
                        aint.update(1,1,n,x,x+l-1,1);
                } else if(c==LIV) {
                        faina>>x>>l;
                        aint.update(1,1,n,x,x+l-1,0);
                }
        }
        return 0;
}