Cod sursa(job #1690666)

Utilizator TibixbAndrei Tiberiu Tibixb Data 15 aprilie 2016 14:38:04
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.3 kb
#include<fstream>
using namespace std;
int x[100005], A[400005], n, m, i, op, a, b, sol;
void build(int st, int dr, int nod)
{
    if(st==dr)
    {
        A[nod]=x[st];
        return ;
    }
    int mij=st+(dr-st)/2;
    build(st, mij, 2*nod);
    build(mij+1, dr, 2*nod+1);
    A[nod]=max(A[2*nod], A[2*nod+1]);
}
void update(int st, int dr, int nod, int poz, int val)
{
    if(st==dr)
    {
        A[nod]=val;
        return ;
    }
    int mij=st+(dr-st)/2;
    if(poz<=mij)
        update(st, mij, 2*nod, poz, val);
    if(poz>=mij+1)
        update(mij+1, dr, 2*nod+1, poz, val);
    A[nod]=max(A[2*nod], A[2*nod+1]);
}
void query(int st, int dr, int nod, int a, int b)
{
    if(a<=st && dr<=b)
    {
        sol=max(sol, A[nod]);
        return ;
    }
    int mij=st+(dr-st)/2;
    if(a<=mij)
        query(st, mij, 2*nod, a, b);
    if(b>=mij+1)
        query(mij+1, dr, 2*nod+1, a, b);
}
ifstream in("arbint.in");
ofstream out("arbint.out");
int main()
{
    in>>n>>m;
    for(i=1; i<=n; i++)
        in>>x[i];
    build(1, n, 1);
    for(;m--;)
    {
        in>>op>>a>>b;
        if(op==0)
        {
            sol=-1;
            query(1, n, 1, a, b);
            out<<sol<<"\n";
            continue;
        }
        update(1, n, 1, a, b);
    }
}