Cod sursa(job #2763178)

Utilizator AlexZeuVasile Alexandru AlexZeu Data 12 iulie 2021 10:47:24
Problema Arbori de intervale Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.13 kb
#include <bits/stdc++.h>
#define ll long long
#define nl '\n'
#define FOR(i, a, b) for (int i = a; i <= b; ++i) 
#define F0R(i, a, b) for (int i = a; i >= b; --i)
#define FORd(i, n) for (int i = 0; i < n; ++i)
#define F0Rd(i, n) for (int i = n - 1; i >= 0; --i)
#define trav(a, x) for (auto &a : x)
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int mxN = 1e5 + 5;
const int MOD = 1e9 + 7;

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

int MaxArb[2 * mxN];

void Update(int Pos, int nod, int left, int right, int value) {
    if (left == right) {
        MaxArb[nod] = value;
        return;
    }
    int mij = (left + right) / 2;
    if (Pos <= mij) {
        Update(Pos, 2 * nod, left, mij, value);
    }
    else {
        Update(Pos, 2 * nod + 1, mij + 1, right, value);
    }
    MaxArb[nod] = max(MaxArb[2 * nod], MaxArb[2 * nod + 1]);
}

void Query(int nod, int req_left, int req_right, int left, int right, int &maxim) {
    if (req_left <= left && right <= req_right) {
        maxim = max(maxim, MaxArb[nod]);
        return;
    }
    int mij = (left + right) / 2;
    if (mij >= req_left) {
        Query(2 * nod, req_left, req_right, left, mij, maxim);
    }
    if (mij < req_right) {
        Query(2 * nod + 1, req_left, req_right, mij + 1, right, maxim);
    }
}

void solve() {
    int N, Q;
    fin >> N >> Q;
    FOR(i, 1, N) {
        int x; fin >> x;
        Update(i, 1, 1, N, x);
    }
    while (Q--) {
        int op, a, b;
        fin >> op >> a >> b;
        if (op == 1) {
            Update(a, 1, 1, N, b);
        }
        else {
            int maxim = -1;
            Query(1, a, b, 1, N, maxim);
            fout << maxim << nl;
        }
    }
}

int main() {
    fin.tie(0); fout.tie(0);
    ios::sync_with_stdio(0);
    int T = 1;
//    cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}

//read the question correctly (ll vs int)
//what's the meaning of the problem ? Think outside the BOX !!!
//edge cases ?
//make it simple
//write everything (observations, edge cases, ideas, steps, methods, ...)