Cod sursa(job #2754336)

Utilizator bogdan_modoleaBogdan Modolea bogdan_modolea Data 25 mai 2021 17:22:47
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.53 kb
#include <bits/stdc++.h>
#define NMAX 4 * 100001 + 66
using namespace std;
typedef long long ll;

int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1};
int dy[] = {0, 1, 0, -1, -1, 1, 1, -1};

const string file = "arbint";

ifstream fin(file + ".in");
ofstream fout(file + ".out");

int n, m;
int arbore[NMAX];
int start, finish, value, position, maxi;

void update(int node, int lb, int rb) {
    if (lb == rb) {
        arbore[node] = value;
        return;
    }
    int mid = (lb + rb) >> 1;
    if (position <= mid)
        update(2 * node, lb, mid);
    else
        update(2 * node + 1, mid + 1, rb);
    arbore[node] = max(arbore[2 * node], arbore[2 * node + 1]);
}

void query(int node, int lb, int rb) {
    if (start <= lb && rb <= finish) {
        if (maxi < arbore[node])
            maxi = arbore[node];
        return;
    }
    int mid = (lb + rb) >> 1;
    if (start <= mid)
        query(2 * node, lb, mid);
    if (mid < finish)
        query(2 * node + 1, mid + 1, rb);
}

int main() {
    int x, a, b;
    fin >> n >> m;
    for (int i = 1; i <= n; i++) {
        fin >> x;
        position = i;
        value = x;
        update(1, 1, n);
    }
    for (int i = 1; i <= m; i++) {
        fin >> x >> a >> b;
        if (x == 0) {
            maxi = -1;
            start = a;
            finish = b;
            query(1, 1, n);
            fout << maxi << "\n";
        } else {
            position = a;
            value = b;
            update(1, 1, n);
        }
    }
    return 0;
}