Cod sursa(job #1557445)

Utilizator Ionut.popescuLiviu Rebreanu Ionut.popescu Data 27 decembrie 2015 16:02:51
Problema Arbori de intervale Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include <bits/stdc++.h>

const int kMaxN = 100000;

using namespace std;

int T[2 * kMaxN];

int main(void) {
    ifstream in("arbint.in");
    ofstream out("arbint.out");
    int N, Q;

    in >> N >> Q;

    for (int i = 0; i < N; i++) {
        in >> T[i + N];
    }

    for (int i = N - 1; i > 0; i--) {
        T[i] = max(T[2 * i], T[2 * i + 1]);
    }

    while (Q--) {
        int opType;
        in >> opType;
        if (!opType) {
            int x, y;
            in >> x >> y;
            x--; y--;
            x += step;
            y += step;
            int ans = 0;
            while (x <= y) {
                ans = max(ans, T[x]);
                x = (x + 1) / 2;
                ans = max(ans, T[y]);
                y = (y - 1) / 2;
            }
            out << ans << '\n';
        } else {
            int x, key;
            in >> x >> key;
            x--;
            x += step;
            T[x] = key;
            while (x > 1) {
                int y = x / 2;
                T[y] = max(T[x], T[x ^ 1]);
                x = y;
            }
        }
    }
    in.close();
    out.close();
    return 0;
}