Cod sursa(job #1005724)

Utilizator narcis_vsGemene Narcis - Gabriel narcis_vs Data 5 octombrie 2013 16:57:31
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.78 kb
#include <fstream>

#define In "hotel.in"
#define Out "hotel.out"
#define Nmax 100004

using namespace std;

struct node
{
    int Left, Right, Best;
};
class SegmentTree
{
    public:
    inline void Init(const int node,const int value)
    {
       a[node].Left = a[node].Right = a[node].Best  = value;
    }
    inline void Update(const int node,const int Left,const int Middle,const int Right)
    {
        int LeftSon = 2*node,RightSon = 2*node+1;
        a[node].Left = a[LeftSon].Left;
        if(a[LeftSon].Best == Middle-Left+1)
            a[node].Left +=  a[RightSon].Left;

        a[node].Right = a[RightSon].Right;
        if(a[RightSon].Best == Right-Middle)
            a[node].Right += a[LeftSon].Right;

        a[node].Best = max(a[LeftSon].Best,a[RightSon].Best);
        a[node].Best = max(a[node].Best,max(a[node].Left,a[node].Right));
        a[node].Best = max(a[node].Best,a[LeftSon].Right+a[RightSon].Left);
    }
    inline void Down(const int node,const int Left,const int Middle,const int Right)
    {
        if(a[node].Best==0)
            Init(2*node,0),
            Init(2*node+1,0);
        if(a[node].Best==Right-Left+1)
        {
            Init(2*node,Middle-Left+1),
            Init(2*node+1,Right-Middle);
        }
    }
    inline void Lazy_Update(const int node,const int Left,const int Right,const int X,const int Y,const bool value)
    {
        if(X <= Left && Right <= Y)
        {
            if(value)
                Init(node,Right-Left+1);
            else
                Init(node,0);
            return ;
        }
        int Middle = (Left+Right)>>1;
        Down(node,Left,Middle,Right);
        if(X <= Middle)
            Lazy_Update(2*node,Left,Middle,X,Y,value);
        if(Middle < Y)
            Lazy_Update(2*node+1,Middle+1,Right,X,Y,value);
        Update(node,Left,Middle,Right);
    }
    inline void Build(const int node,const int Left,const int Right)
    {
        if(Left == Right)
        {
            Init(node,1);
            return ;
        }
        int Middle = (Left+Right)>>1;
        Build(2*node,Left,Middle);
        Build(2*node+1,Middle+1,Right);
        Init(node,Right-Left+1);
    }
    inline int GetSol()
    {
        return a[1].Best;
    }
    private:
    node a[4*Nmax];
};
SegmentTree Aint;
int main()
{
    int n, p , op, x, y;
    ifstream f(In);
    ofstream g(Out);
    f>>n>>p;
    Aint.Build(1,1,n);
    for( ; p ;--p)
    {
        f>>op;
        if(op==3)
            g<<Aint.GetSol()<<"\n";
        else
        {
            f>>x>>y;
            y = x+y-1;
            if(op==1)
                Aint.Lazy_Update(1,1,n,x,y,0);
            else
                Aint.Lazy_Update(1,1,n,x,y,1);
        }

    }
    return 0;
}