Cod sursa(job #2570680)

Utilizator rusu.ralucaRusu Raluca rusu.raluca Data 4 martie 2020 18:29:08
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.3 kb
#include <iostream> 
#include <fstream>
#include <vector>
#include <cstring>
#include <climits>
#include <algorithm>

using namespace std;

ifstream fin("arbint.in");
ofstream fout("arbint.out");

const int maxn = 100005;
int n, m;
int arb[maxn << 2];

inline void update(int node, int st, int dr, int poz, int value){
    if(st == dr){
        arb[node] = value;
        return ;
    }
    int mid = ((st + dr) >> 1);//(st+dr)/2
    if(poz <= mid)
        update(node << 1, st, mid, poz, value);
    else
        update((node << 1) | 1, mid + 1, dr, poz, value);
    arb[node] = max(arb[node << 1], arb[(node << 1) | 1]);
}

inline int query(int node, int st, int dr, int a, int b) {
    if(a <= st && dr <= b)
        return arb[node];
    int mid = ((st + dr) >> 1);
    int ret = -0x3f3f3f3f;
    if(a <= mid)
        ret = query(node << 1, st, mid, a, b);
    if(mid < b)
        ret = max(ret, query((node << 1) | 1, mid + 1, dr, a, b));
    return ret;
}

int main(){
    fin >> n >> m;
    for(int i = 1; i <= n; ++i){
        int x;
        fin >> x;
        update(1, 1, n, i, x);
    }
    for(int i = 1; i <= m; ++i){
        int op, x, y;
        fin >> op >> x >> y;
        if(op == 0){
            fout << query(1, 1, n, x, y) << '\n';
        }
        else
            update(1, 1, n, x, y); 
    }
    return 0;
}