Cod sursa(job #2879305)

Utilizator QwertyDvorakQwerty Dvorak QwertyDvorak Data 28 martie 2022 13:34:40
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.75 kb
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define dbg(x) cout << #x <<": " << x << "\n";
#define sz(x) ((int)x.size())

using ll = long long;

const string fn = "arbint";
ifstream fin(fn + ".in");
ofstream fout(fn + ".out");

int n, m;

class aint{

public:
    const int mxN = 1e5;
    vector<int> a;

    aint(){
        a.resize(4 * mxN + 1);
    }

    inline int op(int &x, int &y){
        return max(x, y);
    }

    void build(int nod, int st, int dr){
        if(st == dr){
            fin >> a[nod];
            return;
        }
        int mid = (st + dr) >> 1;
        build(nod << 1, st, mid);
        build(nod << 1 | 1, mid + 1, dr);
        a[nod] = op(a[nod << 1], a[nod << 1 | 1]);
    }

    void upd(int nod, int st, int dr, int p, int val){
        if(st == dr){
            a[nod] = val;
            return;
        }
        int mid = (st + dr) >> 1;
        if(p <= mid)
            upd(nod << 1, st, mid, p, val);
        else
            upd(nod << 1 | 1, mid + 1, dr, p, val);
        a[nod] = op(a[nod << 1], a[nod << 1 | 1]);
    }

    int qry(int nod, int st, int dr, int x, int y){
        if(st > dr || st > y || dr < x)
            return -1;
        if(st >= x && dr <= y)
            return a[nod];
        int mid = (st + dr) >> 1;
        int q1 = qry(nod << 1, st, mid, x, y);
        int q2 = qry(nod << 1 | 1, mid + 1, dr, x, y);
        return op(q1, q2);
    }

};

aint a;

int main(){

    fin >> n >> m;
    a.build(1, 1, n);
    while(m--){
        int op, x, y;
        fin >> op >> x >> y;
        if(op == 0)
            fout << a.qry(1, 1, n, x, y) << '\n';
        else
            a.upd(1, 1, n, x, y);
    }

    return 0;
}