Cod sursa(job #1761946)

Utilizator razvan242Zoltan Razvan-Daniel razvan242 Data 23 septembrie 2016 09:20:19
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.5 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMAX = 4 * 1e5 + 1;

int n, m;
int arbInt[NMAX];
int updatePos, updateVal, xQuery, yQuery;
int maxQuery;

inline int getMax(int a, int b) {
    return a > b ? a : b;
}

void update(int node, int lo, int hi) {
    if (lo == hi) {
        arbInt[node] = updateVal;
        return;
    }
    int mid = (lo + hi) / 2;
    if (updatePos <= mid)
        update(2 * node, lo, mid);
    else
        update(2 * node + 1, mid + 1, hi);
    arbInt[node] = getMax(arbInt[2 * node], arbInt[2 * node + 1]);
}

void query(int node, int lo, int hi) {
    if (xQuery <= lo and hi <= yQuery) {
        maxQuery = getMax(maxQuery, arbInt[node]);
        return;
    }

    int mid = (lo + hi) / 2;
    if (xQuery <= mid)
        query(2 * node, lo, mid);
    if (mid < yQuery)
        query(2 * node + 1, mid + 1, hi);
}

int main()
{
    int x, a, b;
    fin >> n >> m;
    for (int i = 1; i <= n; ++i) {
        fin >> x;
        updatePos = i;
        updateVal = x;
        update(1, 1, n);
    }
    for (int i = 1; i <= m; ++i) {
        fin >> x >> a >> b;
        if (x == 0) {
            xQuery = a;
            yQuery = b;
            maxQuery = -1;
            query(1, 1, n);
            fout << maxQuery << '\n';
        }
        else {
            updatePos = a;
            updateVal = b;
            update(1, 1, n);
        }
    }
    return 0;
}