Cod sursa(job #2600194)

Utilizator eugen5092eugen barbulescu eugen5092 Data 12 aprilie 2020 11:25:43
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.24 kb
#include <bits/stdc++.h>
using namespace std;

ifstream ci("arbint.in");
ofstream cou("arbint.out");

int n,m,N;
int aint[300000];

void citire(){
    int i;
    ci>>n>>m;
    N=1;
    while(N<n){
        N*=2;
    }
    for(i=N;i<N+n;i++){
        ci>>aint[i];
    }

    for(i=N-1;i;i--){
        aint[i]=max(aint[2*i],aint[2*i+1]);
    }

}

void Update(int p, int x)
{
    int t, mx;
    p = p + N - 1;
    aint[p] = x;
    while(p > 0)
    {
        t = p / 2;
        mx = max(aint[2 * t], aint[2 * t + 1]);
        aint[t] = mx;
        p = t;
    }

}

int Query(int nod, int x, int y, int st, int dr)
{
    int m;
    if(st == x && dr == y) return aint[nod];
    else
    {
        m = (st + dr) / 2;
        if(y <= m) return Query(nod * 2, x, y, st, m);
        else if(m + 1 <= x) return Query(nod * 2 + 1, x, y, m + 1, dr);
        else return max(Query(nod * 2, x, m, st, m), Query(nod * 2 + 1, m + 1, y, m + 1, dr));
    }
}

void rez(){
    int c,a,b;
    //cout<<N;
    while(m--){
        ci>>c>>a>>b;
        if(c==1){
            Update(a,b);
        }else{
            cou<<Query(1,a,b,1,N)<<"\n";
        }
    }
}

int main()
{
    citire();
    rez();
    return 0;
}