Cod sursa(job #1097554)

Utilizator 2dorTudor Ciurca 2dor Data 3 februarie 2014 16:36:36
Problema Arbori de intervale Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.78 kb
#include <iostream>
#include <fstream>
using namespace std;

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

const int MAXN = 100009;
int N, M, op, a, b, maxim, Val;
int v[4 * MAXN];
int arbore[4 * MAXN];

inline int Left_Son(int node) {
    return 2 * node;
}

inline int Right_Son(int node) {
    return 2 * node + 1;
}

inline int Father(int node) {
    return node / 2;
}

void Query(int node, int Left, int Right) {
    if (a <= Left && Right <= b)
        maxim = max(maxim, arbore[node]);
    else {
        int Middle = (Left + Right) / 2;
        if (a <= Middle)
            Query(Left_Son(node), Left, Middle);
        if (Middle + 1 <= b)
            Query(Right_Son(node), Middle + 1, Right);
    }
}

void Update(int node, int Left, int Right) {
    if (Left == Right)//we've reached the node whose value we want to change
        arbore[node] = Val;
    else {
        int Middle = (Left + Right) / 2;
        //calculates the maximums between ranges [Left, Middle] and [Middle + 1, Right]
        if (a <= Middle)
            Update(Left_Son(node), Left, Middle);
        if (Middle + 1 <= a)
            Update(Right_Son(node), Middle + 1, Right);
        arbore[node] = max(arbore[Left_Son(node)], arbore[Right_Son(node)]);
    }
}

void Read() {
    fin >> N >> M;
    for (int i = 1; i <= N; ++i) {
        fin >> v[i];
        a = i;
        Val = v[i];
        Update(1, 1, N);
    }
}

int main() {
    Read();
    while (M--) {
        fin >> op >> a >> b;
        if (op == 0) {//query
            maxim = 0;
            Val = b;
            Query(1, 1, N);
            fout << maxim << '\n';
        }else {//update
            Update(1, 1, N);
        }
    }
    fin.close();
    fout.close();
    return 0;
}