Cod sursa(job #1954284)

Utilizator dumbraveanbDumbravean Bogdan dumbraveanb Data 5 aprilie 2017 12:15:47
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.28 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], maxim;

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]);
}

void Query(const int& nod, const int& st, const int& dr, const int& a, const int&b) {
    if(a <= st && dr <= b) {
        if(A[nod] > maxim) maxim = A[nod];
        return;
    }
    int mij = (st + dr) / 2;
    if(a <= mij) Query(2 * nod, st, mij, a, b);
    if(mij < b) Query(2 * nod + 1, mij + 1, dr, a, b);
}

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) {
            maxim = -1;
            Query(1, 1, n, x, y);
            fout << maxim << '\n';
        }
        else
            Update(1, 1, n, y, x);
    }
    return 0;
}