Cod sursa(job #1697661)

Utilizator diana97Diana Ghinea diana97 Data 2 mai 2016 17:19:35
Problema Arbori de intervale Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.14 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream f ("arbint.in");
ofstream g ("arbint.out")

const int NMAX = 100000 + 1;

int n, m;
int arbint[4 * NMAX];

void update(int a, int b, int poz, int x, int val) {
    if (a == b) {
        arbint[poz] = val;
        return;
    }

    int m = (a + b) / 2;
    if (x <= m) update(a, m, 2 * poz, x, val);
    else update(m + 1, b, 2 * poz + 1, x, val);
    arbint[poz] = max(arbint[2 * poz], arbint[2 * poz + 1]);
}

int query(int a, int b, int poz, int x, int y) {
    if (x <= a && b <= y) return arbint[poz];

    int rez = 0;
    int m = (a + b) / 2;
    if (x <= m) rez = query(a, m, 2 * poz, x, y);
    if (m < y) rez = max(rez, query(m + 1, b, 2 * poz + 1, x, y));

    return rez;
}

void citeste() {
    f >> n >> m;

    int val;
    for (int i = 1; i <= n; i++) {
        f >> val;
        update(1, n, 1, i, val);
    }
}

void rezolva() {
    int tip, x, y;

    for (int i = 1; i <= m; i++) {
        f >> tip >> x >> y;
        if (tip == 0) g << query(1, n, 1, x, y) << '\n';
        else update(1, n, 1, x, y);
    }
}

int main() {
    citeste();
    rezolva();

    return 0;
}