Cod sursa(job #2281944)

Utilizator veleanduAlex Velea veleandu Data 12 noiembrie 2018 23:05:25
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.44 kb
// 22:58
#include <fstream>
using namespace std;

const int kMaxN = 1e5+5;

struct Aint {
    int mx[3 * kMaxN];

    void update(int node, int left, int right, int pos, int value) {
        if (left == right) {
            mx[node] = value;
            return;
        }
        int mid = (left + right) / 2;
        if (pos <= mid) {
            update(2 * node, left, mid, pos, value);
        } else {
            update(2 * node + 1, mid + 1, right, pos, value);
        }

        mx[node] = max(mx[2 * node], mx[2 * node + 1]);
    }

    int query(int node, int left, int right, int c1, int c2) {
        if (c1 <= left and right <= c2) {
            return mx[node];
        }
        if (right < c1 or c2 < left) {
            return 0;
        }

        int mid = (left + right) / 2;
        return max(
                query(2 * node, left, mid, c1, c2),
                query(2 * node + 1, mid + 1, right, c1, c2)
        );
    }
};

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

    int n, q; cin >> n >> q;

    Aint aint;

    for (int i = 1; i <= n; i += 1) {
        int x; cin >> x;
        aint.update(1, 1, n, i, x);
    }

    while (q--) {
        char c; cin >> c;
        if (c == '0') {
            int a, b; cin >> a >> b;
            cout << aint.query(1, 1, n, a, b) << '\n';
        } else {
            int pos, x; cin >> pos >> x;
            aint.update(1, 1, n, pos, x);
        }
    }

    return 0;
}