Cod sursa(job #1954291)

Utilizator dumbraveanbDumbravean Bogdan dumbraveanb Data 5 aprilie 2017 12:17:51
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.24 kb
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

#define Nmax 100005

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

int n, m, A[5 * Nmax];

void Update(const int& nod, const int& st, const int& dr, const int& val, const int& pos) {
    if(st == dr) {
        A[nod] = val;
        return;
    }
    int mij = (st + dr) / 2;
    if(pos <= mij)
        Update(2 * nod, st, mij, val, pos);
    else
        Update(2 * nod + 1, mij + 1, dr, val, pos);

    A[nod] = max(A[nod * 2], A[nod * 2 + 1]);
}

int Query(const int& nod, const int& st, const int& dr, const int& a, const int&b) {
    if(a <= st && dr <= b)
        return A[nod];

    int mij = (st + dr) / 2;
    int q1 = 0, q2 = 0;
    if(a <= mij) q1 = Query(2 * nod, st, mij, a, b);
    if(mij < b) q2 = Query(2 * nod + 1, mij + 1, dr, a, b);
    return max(q1, q2);
}

int main()
{
    fin >> n >> m;
    int op, x, y;
    for(int i = 1; i <= n; ++i) {
        fin >> x;
        Update(1, 1, n, x, i);
    }
    for(int j = 1; j <= m; ++j) {
        fin >> op >> x >> y;
        if(!op)
            fout << Query(1, 1, n, x, y) << '\n';
        else
            Update(1, 1, n, y, x);
    }
    return 0;
}