Cod sursa(job #2870920)

Utilizator Cosmin2004_InfoMoldoveanu Cosmin Cosmin2004_Info Data 12 martie 2022 18:30:02
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.37 kb
#include <bits/stdc++.h>

using namespace std;
#ifndef HOME
ifstream fin("arbint.in");
ofstream fout("arbint.out");
#else
ifstream fin("ciorna.in");
ofstream fout("ciorna.out");
#endif
const int N = 1e5;
int a[N + 5];
class Segtree {
    int n, neutral;
    vector <int> t;
    function <int(int, int)> f;
public:
    template <typename Iterator>
    Segtree(Iterator first, Iterator last, int neut, auto f) : n(last - first), neutral(neut), t(2 * (last - first), neut), f(f) {
        for(int i = 0; i < n; i++) t[i + n] = *(first + i);
        for(int i = n - 1; i > 0; i--)
            t[i] = f(t[i << 1], t[i << 1 | 1]);
    }
    void update(int pos, int val) { for(t[pos += n] = val; pos > 1; pos >>= 1) t[pos >> 1] = f(t[pos], t[pos ^ 1]); }
    int query(int l, int r) {
        int res = neutral;
        for(l += n, r += n; l < r; l >>= 1, r >>= 1) {
            if(l & 1) res = f(res, t[l++]);
            if(r & 1) res = f(res, t[--r]);
        }
        return res;
    }
};

int main()
{
    int n, m;
    fin >> n >> m;
    for(int i = 1; i <= n ; i++)
        fin >> a[i];
    Segtree S(a + 1, a + n + 1, 0, [](int a, int b){ return max(a, b); });
    for(int i = 1; i <= m; i++) {
        int t, x , y;
        fin >> t >> x >> y;
        if(t == 0) fout << S.query(x - 1, y) << "\n";
        else S.update(x - 1, y);
    }
    return 0;
}