Cod sursa(job #1935204)

Utilizator LazarAndreiLazar Andrei Teodor LazarAndrei Data 22 martie 2017 08:28:14
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.46 kb
#include <fstream>

using namespace std;

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

int N, maxTree[400070], M, Pos, Value, leftQ, rightQ, maxim, X;

void updateTree (int left, int right, int position) {
    if (left == right) {
            maxTree[position] = Value;
            return;
    }
    int mid = (left + right)/2;
    if (Pos <= mid) updateTree (left, mid, 2*position);
    else updateTree (mid+1, right, 2*position+1);

    maxTree[position] = max (maxTree[2*position], maxTree[2*position+1]);
}

void Query (int left, int right, int position) {
    if (leftQ <= left && rightQ >= right){    ///Total Overlap
       if (maxim < maxTree[position]) {
          maxim = maxTree[position];
       }

       return;
    }
    int mid = (left + right)/2;
    if (leftQ <= mid) Query (left, mid, 2*position);
    if (rightQ > mid) Query (mid+1, right, 2*position+1);
}

int main()
{
    in >> N >> M;
    for (int i = 1; i <= N; ++ i) {
        in >> X;
        Pos = i, Value = X;
        updateTree (1, N, 1);
    }

    for (int i = 1; i <= M; ++ i) {
        int query, a, b;
        in >> query >> a >> b;
        if (query == 1) {
            Pos = a;
            Value = b;

            updateTree (1, N, 1);
        }
        else  {
            maxim = -1;
            leftQ = a;
            rightQ = b;

            Query (1, N, 1);
            out << maxim << '\n';
        }
    }

    return 0;
}