Cod sursa(job #874658)

Utilizator Stefex09Stefan Teodorescu Stefex09 Data 9 februarie 2013 09:54:14
Problema Arbori de intervale Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.33 kb
#include <iostream>
#include <fstream>
#include <cstdio>

using namespace std;

ifstream in ("arbint.in");

int AINT[1 << 18];

inline int max (const int &A, const int &B)
{
    if (A > B)
        return A;

    return B;
}

void update (int nod, int st, int dr, int poz, int val)
{
    if (st == dr){
        AINT[nod] = val;

        return;
    }

    int mid = (st + dr) >> 1;

    if (poz <= mid)
        update (nod << 1, st, mid, poz, val);
    else
        update (nod << 1 | 1, mid + 1, dr, poz, val);

    AINT[nod] = max (AINT[nod << 1], AINT[nod << 1 | 1]);
}

int query (int nod, int st, int dr, int a, int b)
{
    if (a <= st && dr <= b)
        return AINT[nod];

    int mid = (st + dr) >> 1;
    int aux1 = 0, aux2 = 0;

    if (a <= mid)
        aux1 = query (nod << 1, st, mid, a, b);
    if (mid < b)
        aux2 = query (nod << 1 | 1, mid + 1, dr, a, b);

    return max (aux1, aux2);
}

int main()
{
    freopen ("arbint.out", "w", stdout);

    int N, Q, i, tip, x, y;

    in >> N >> Q;

    for (i = 1; i <= N; i ++){
        in >> x;
        update (1, 1, N, i, x);
    }

    while (Q --){
        in >> tip >> x >> y;

        if (tip)
            update (1, 1, N, x, y);
        else
            printf ("%d\n", query (1, 1, N, x, y));
    }

    return 0;
}