Cod sursa(job #2305688)

Utilizator tifui.alexandruTifui Ioan Alexandru tifui.alexandru Data 20 decembrie 2018 20:50:13
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.04 kb
#include <bits/stdc++.h>
#define Nmax 100005

using namespace std;

ifstream f("arbint.in");
ofstream g("arbint.out");

int v[Nmax],lsh,rsh,pos;

class SegmentTree
{
    private:
        int *T;

        inline int ls(const int &x) {return x<<1;}
        inline int rs(const int &x) {return ls(x)+1;}

    public:

        SegmentTree(const int &n)
        {
            T=new int[n<<2]();
        }

        void build(int lo, int hi, int node)
        {
            if(lo==hi)
                T[node]=v[lo];
            else
            {
                int mid=(lo+hi)>>1;

                build(lo,mid,ls(node));
                build(mid+1,hi,rs(node));

                T[node]=max(T[ls(node)],T[rs(node)]);
            }
        }

        void update(int lo, int hi, int node)
        {
            if(lo==hi)
                T[node]=v[lo];
            else
            {
                int mid=(lo+hi)>>1;

                if(pos<=mid)
                    update(lo,mid,ls(node));
                else
                    update(mid+1,hi,rs(node));

                T[node]=max(T[ls(node)],T[rs(node)]);
            }
        }

       int query(int lo, int hi, int node)
        {
            if(lsh<=lo and hi<=rsh)
                return T[node];
            else
            {
                int mid=(lo+hi)>>1,M1=INT_MIN,M2=INT_MIN;

                if(lsh<=mid)
                    M1=query(lo,mid,ls(node));
                if(mid<rsh)
                    M2=query(mid+1,hi,rs(node));

                return max(M1,M2);
            }
        }
};

int main()
{
    int n,i,m,op,x,y;
    f>>n>>m;
    for(i=1;i<=n;i++)
        f>>v[i];

    SegmentTree ST(n+5);
    ST.build(1,n,1);

    while(m--)
    {
        f>>op>>x>>y;
        if(!op)
        {
            lsh=x;
            rsh=y;
            g<<ST.query(1,n,1)<<'\n';
        }
        else
        {
            v[x]=y;
            pos=x;
            ST.update(1,n,1);
        }
    }

    return 0;
}