Cod sursa(job #3147891)

Utilizator AndreiBOTOBotocan Andrei AndreiBOTO Data 27 august 2023 19:52:35
Problema Hotel Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.99 kb
#include <bits/stdc++.h>

#pragma optimize GCC ("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

///#include <tryhardmode>
///#include <GODMODE::ON>

using namespace std;

class InParser {
private:
	FILE *fin;
	char *buff;
	int sp;

	char read_ch() {
		++sp;
		if (sp == 4096) {
			sp = 0;
			fread(buff, 1, 4096, fin);
		}
		return buff[sp];
	}

public:
	InParser(const char* nume) {
		fin = fopen(nume, "r");
		buff = new char[4096]();
		sp = 4095;
	}

	InParser& operator >> (int &n) {
		char c;
		while (!isdigit(c = read_ch()) && c != '-');
		int sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}

	InParser& operator >> (long long &n) {
		char c;
		n = 0;
		while (!isdigit(c = read_ch()) && c != '-');
		long long sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}
};

InParser fin ("hotel.in");
ofstream fout ("hotel.out");

const int NMAX=1e5+5;
bool lazy[4*NMAX];
int v[NMAX];

struct elem{
    int prefix;
    int sufix;
    int subarray;
    int length;
}aint[4*NMAX];

elem solve(elem l,elem r)
{
    elem aux;
    if(l.prefix==l.length)
        aux.prefix=l.length+r.prefix;
    else
        aux.prefix=l.prefix;
    if(r.sufix==r.length)
        aux.sufix=r.length+l.sufix;
    else
        aux.sufix=r.sufix;
    aux.subarray=max({l.subarray,r.subarray,l.sufix+r.prefix});
    aux.length=l.length+r.length;
    return aux;
}

void build(long long p,long long st,long long dr)
{
    long long mij;
    if(st==dr)
    {
        aint[p]={1,1,1,1};
        return ;
    }
    else
    {
        mij=st+dr;
        mij/=2;
        build(2*p,st,mij);
        build(2*p+1,mij+1,dr);
        aint[p]=solve(aint[2*p],aint[2*p+1]);
    }
}

void compute_node(int p)
{
    if(aint[p].subarray)
    {
        aint[p].sufix=0;
        aint[p].prefix=0;
        aint[p].subarray=0;
    }
    else
    {
        aint[p].sufix=aint[p].length;
        aint[p].prefix=aint[p].length;
        aint[p].subarray=aint[p].length;
    }
}

void propag(int p,int st,int dr)
{
    if(st!=dr)
    {
        if(lazy[p]==0)
            return ;
        compute_node(2*p);
        compute_node(2*p+1);
        if(lazy[2*p]==1)
            lazy[2*p]=0;
        else
            lazy[2*p]=lazy[p];
        if(lazy[2*p+1]==1)
            lazy[2*p+1]=0;
        else
            lazy[2*p+1]=lazy[p];
        lazy[p]=0;
    }
}

void update(int p,int st,int dr,int a,int b)
{
    propag(p,st,dr);
    if(a<=st && dr<=b)
    {
        lazy[p]+=1;
        compute_node(p);
        return ;
    }
    else
    {
        int mij=st+dr;
        mij/=2;
        if(a<=mij)
            update(2*p,st,mij,a,b);
        if(mij<b)
            update(2*p+1,mij+1,dr,a,b);
        aint[p]=solve(aint[2*p],aint[2*p+1]);
    }
}

elem query(int p,int st,int dr,int l,int r)
{
    int mij;
    elem a,b;
    propag(p,st,dr);
    if(l<=st && dr<=r)
        return aint[p];
    else
    {
       mij=st+dr;
       mij/=2;
       if(l<=mij)
            return query(2*p,st,mij,l,r);
       if(mij<r)
            return query(2*p+1,mij+1,dr,l,r);
       a=query(2*p,st,mij,l,r);
       b=query(2*p+1,mij+1,dr,l,r);
       return solve(a,b);
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    fout.tie(NULL);

    int n,q,t;
    fin>>n>>q;
    build(1,1,n);
    while(q--)
    {
        int x,y;
        fin>>t;
        if(t==1)
        {
            fin>>x>>y;
            update(1,1,n,x,x+y-1);
        }
        else if(t==2)
        {
            fin>>x>>y;
            update(1,1,n,x,x+y-1);
        }
        else
            fout<<query(1,1,n,1,n).subarray<<"\n";
    }
    fout.tie();
    return 0;
}