Cod sursa(job #2086010)

Utilizator Teodor.mTeodor Marchitan Teodor.m Data 11 decembrie 2017 09:02:33
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.91 kb
#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef long double ld;

inline void debugMode() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    #ifndef ONLINE_JUDGE
    freopen("arbint.in", "r", stdin);
    freopen("arbint.out", "w", stdout);
    #endif // ONLINE_JUDGE
}
inline void PrintYes() { cout << "Yes"; }
inline void PrintNo() { cout << "No"; }

const double eps = 1e-7;
const int Nmax = 1e5 + 5;

int n, m;
int tree[4 * Nmax];
int a[Nmax];

void buildTree(int left, int right, int poz)
{
    if(left == right) {
        tree[poz] = a[left];
        return;
    }

    int mij = (left + right) >> 1;
    buildTree(left, mij, 2 * poz);
    buildTree(mij + 1, right, 2 * poz + 1);

    tree[poz] = max(tree[2 * poz], tree[2 * poz + 1]);
}

void update(int poz, int val, int nod, int left, int right)
{
    if(left == right) {
        tree[nod] = val;
        return;
    }

    int mij = (left + right) >> 1;
    if(poz <= mij)
        update(poz, val, 2 * nod, left, mij);
    else
        update(poz, val,2 * nod + 1, mij + 1, right);

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

int query(int nod, int left, int right, int a, int b)
{
    if(a <= left && right <= b)
        return tree[nod];

    if(a > right || b < left)
        return INT_MIN;

    int mij =(left + right) >> 1;
    return max(query(2 * nod, left, mij, a, b), query(2 * nod + 1, mij + 1, right, a ,b));

}

int main()
{
    debugMode();

    cin >> n >> m;
    for(int i = 1; i <= n; ++i)
        cin >> a[i];
    buildTree(1, n, 1);

    for(int i = 1; i <= m; ++i) {
        int op, a, b;
        cin >> op >> a >> b;

        if(op == 0) {
            int val = query(1, 1, n, a, b);
            cout << val << '\n';
        }
        else {
            update(a, b, 1, 1, n);
        }
    }

    return 0;
}