Cod sursa(job #2741195)

Utilizator CronosClausCarare Claudiu CronosClaus Data 15 aprilie 2021 17:52:12
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.67 kb
#include <bits/stdc++.h>

using namespace std;

const int mxn = 100 * 1000 + 10;

int arb[ 8 * mxn ];
vector<int> v;

int n;
int nrQ;

inline int stanga(int x){
    return 2 * x + 1;
}

inline int dreapta(int x){
    return 2 * x + 2;
}

void build(int nod, int st, int sf){

    if(st == sf){
        arb[ nod ] = v[ st ];
        return;
    }

    int mid = (st + sf) / 2;

    build(stanga(nod), st, mid);
    build(dreapta(nod), mid + 1, sf);
    arb[ nod ] = max(arb[ stanga(nod) ], arb[ dreapta(nod) ]);
}

void update(int nod, int st, int sf, int x, int v){
    if(x > sf || x < st){
        return;
    }
    if(st == sf){
        arb[ nod ] = v;
        return;
    }

    int mid = (st + sf) / 2;

    update(stanga(nod), st, mid, x, v);
    update(dreapta(nod), mid + 1, sf, x, v);

    arb[ nod ] = max(arb[ stanga(nod) ], arb[ dreapta(nod) ]);

}

int query(int nod, int st, int sf, int x, int y){
    if(x > sf || y < st){
        return -1;
    }
    if(x <= st && y >= sf){
        return arb[ nod ];
    }

    int mid = (st + sf) / 2;

    return max(query(stanga(nod), st, mid, x, y), query(dreapta(nod), mid + 1, sf, x, y));
}

int main()
{
    ifstream cin("arbint.in");
    ofstream cout("arbint.out");

    cin>> n;
    cin>> nrQ;

    for(int i = 0, x; i < n; i++){
        cin>> x;
        v.push_back( x );
    }

    build(0, 0, n - 1);

    for(int i = 0, c, x, y; i < nrQ; i++){
        cin>> c >> x >> y;
        if(c == 0){
            cout<<query(0, 0, n - 1, x - 1, y - 1) << '\n';

        }
        else{
            update(0, 0, n - 1, x - 1, y);
        }
    }

    return 0;
}