Cod sursa(job #3139769)

Utilizator speedy_gonzalesDuca Ovidiu speedy_gonzales Data 1 iulie 2023 15:16:51
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.67 kb
#include <iostream>
#include <vector>
#include <map>
#include <cstring>
#include <fstream>
#include <sstream>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <set>
#include <unordered_map>
#include <stack>
#include <iomanip>
#include <random>
#include <climits>

using namespace std;

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

const int MAX = 1e5 + 2;

int arb[MAX];
int maxValue;

void update(int nod, int left, int right, int position, int value) {
    if (left == right) {
        arb[nod] = value;
        return;
    }

    int mid = (left + right) / 2;
    if (position <= mid) {
        update(2 * nod, left, mid, position, value);
    } else {
        update(2 * nod + 1, mid + 1, right, position, value);
    }

    arb[nod] = max(arb[2 * nod], arb[2 * nod + 1]);
}

void query(int nod, int left, int right, int start, int finish) {
    if (start <= left && right <= finish) {
        if (maxValue < arb[nod]) {
            maxValue = arb[nod];
        }
        return;
    }

    int mid = (left + right) / 2;
    if (start <= mid) {
        query(2 * nod, left, mid, start, finish);
    }
    if (mid < finish) {
        query(2 * nod + 1, mid + 1, right, start, finish);
    }
}

int main() {
    int m, n;
    fin >> n >> m;

    for (int i = 0; i < m; ++i) {
        int number;
        fin >> number;
        update(1, 1, m, i + 1, number);
    }

    for (int i = 0; i < n; ++i) {
        int type, a, b;
        fin >> type >> a >> b;
        if (type == 1) {
            update(1, 1, n, a, b);
        } else {
            maxValue = -1;
            query(1, 1, n, a, b);

            fout << maxValue << "\n";
        }
    }

    return 0;
}