Cod sursa(job #3234571)

Utilizator razvan242Zoltan Razvan-Daniel razvan242 Data 10 iunie 2024 02:46:48
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.94 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMAX = 1e5 + 1;
vector<int> aint;
int n, queries;

void build(const vector<int>& content, const int node, int left, int right) {
    if (left == right) {
        aint[node] = content[left - 1];
        return;
    }
    int mid = (left + right) / 2;
    build(content, 2 * node, left, mid);
    build(content, 2 * node + 1, mid + 1, right);
    aint[node] = max(aint[2 * node], aint[2 * node + 1]);
}

int query(int node, int left, int right, const int& queryLeft, const int& queryRight) {
    if (queryLeft <= left && right <= queryRight) {
        return aint[node];
    }

    int mid = (left + right) / 2;
    int leftAnswer = 0, rightAnswer = 0;
    if (queryLeft <= mid) {
        leftAnswer = query(2 * node, left, mid, queryLeft, queryRight);
    }
    if (mid + 1 <= queryRight) {
        rightAnswer = query(2 * node + 1, mid + 1, right, queryLeft, queryRight);
    }
    return max(leftAnswer, rightAnswer);
}

void update(int node, int left, int right, const int& pos, const int& val) {
    if (left == right) {
        aint[node] = val;
        return;
    }

    int mid = (left + right) / 2;
    if (pos <= mid) {
        update(2 * node, left, mid, pos, val);
    }
    else {
        update(2 * node + 1, mid + 1, right, pos, val);
    }
    aint[node] = max(aint[2 * node], aint[2 * node + 1]);
}

void read() {
    fin >> n >> queries;
    aint = vector<int>(4 * n + 1);

    vector<int> content;
    int val;
    for (int i = 1; i <= n; ++i) {
        fin >> val;
        content.push_back(val);
    }
    build(content, 1, 1, n);
}

void solve() {
    int operation, x, y;
    while (queries--) {
        fin >> operation >> x >> y;
        if (operation == 0) {
            fout << query(1, 1, n, x, y) << '\n';
        }
        else {
            update(1, 1, n, x, y);
        }
    }
}

int main() {
    read();
    solve();

    return 0;
}