Cod sursa(job #2407269)

Utilizator horea4Cenan Horea horea4 Data 16 aprilie 2019 18:52:11
Problema Arbori de intervale Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.73 kb
#include <bits/stdc++.h>
#define DM 100005
using namespace std;
ifstream fi("arbint.in");
ofstream fo("arbint.out");


struct nod
{
    int st, dr, val;
} T[4*DM];

int V[DM];
int n,q;

void build(int st,int dr,int root)
{

    if(st==dr)
    {
        T[root].st=st;
        T[root].dr=dr;
        T[root].val=V[st];
    }
    else{
        int m=(st+dr)/2;
        build(st,m,2*root);
        build(m+1,dr,2*root+1);
        T[root].st=st;
        T[root].dr=dr;
        T[root].val=max(T[root*2].val,T[root*2+1].val);
        }
}

int query(int st,int dr,int root)
{


    if(dr<T[root].st || st>T[root].dr)
        return -1;
    if(T[root].st==T[root].dr)
        return T[root].val;
   /* if(st<=T[root].st && dr>=T[root].dr)
        return T[root].val;
        */
    else
    {

        int vst,vdr;

        vst=query(st,dr,root*2);
        vdr=query(st,dr,root*2+1);
        return max(vst,vdr);




    }






}

void update(int poz,int val,int root)
{

    if(poz<T[root].st || poz>T[root].dr)
    return;
    if(T[root].st==T[root].dr)
        T[root].val=val;
    else
    {
        update(poz,val,2*root);
        update(poz,val,2*root+1);
        T[root].val=max(T[2*root].val,T[2*root+1].val);
    }








}

int main()
{
    fi>>n>>q;
    for(int i=1;i<=n;i++)
        fi>>V[i];
    build(1,n,1);
    for(int i=1;i<=q;i++)
    {
        int a;
        fi>>a;
        if(a==0)
        {


            int st,dr;
            fi>>st>>dr;
            fo<<query(st,dr,1)<<'\n';
        }
        else
        {

            int poz,val;
            fi>>poz>>val;
            update(poz,val,1);



        }
    }
    return 0;

}