Cod sursa(job #1778889)

Utilizator touristGennady Korotkevich tourist Data 14 octombrie 2016 12:48:44
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.22 kb
#include <bits/stdc++.h>

using namespace std;

struct Node
{
    int mx;
    Node *L,*R;
    Node()
    {
        mx=0;
        L=R=0;
    }
} *root = new Node();

inline int Mx(Node *nod)
{
    if(!nod) return 0;
    return nod->mx;
}

inline void upd(Node *& nod, int st, int dr, int p, int val)
{
    if(!nod) nod=new Node();
    if(st==dr)
    {
        nod->mx=val;
        return;
    }
    int mij=((st+dr)>>1);
    if(p<=mij) upd(nod->L,st,mij,p,val);
    else upd(nod->R,mij+1,dr,p,val);
    nod->mx=max(Mx(nod->L),Mx(nod->R));
}

inline int qry(Node *& nod, int st, int dr, int x, int y)
{
    if(st==x && y==dr) return nod->mx;
    int mij=((st+dr)>>1);
    if(y<=mij) return qry(nod->L,st,mij,x,y);
    else
        if(x>mij) return qry(nod->R,mij+1,dr,x,y);
        else return max(qry(nod->L,st,mij,x,mij),qry(nod->R,mij+1,dr,mij+1,y));
}

int main()
{
    int m,t,x,y,i,n;
    ifstream cin("arbint.in");
    ofstream cout("arbint.out");
    cin>>n>>m;
    for(i=1;i<=n;++i)
    {
        cin>>x;
        upd(root,1,n,i,x);
    }
    while(m--)
    {
        cin>>t>>x>>y;
        if(!t) cout<<qry(root,1,n,x,y)<<"\n";
        else upd(root,1,n,x,y);
    }
    return 0;
}