Cod sursa(job #1190394)

Utilizator andreiiiiPopa Andrei andreiiii Data 25 mai 2014 11:54:44
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.32 kb
#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

class SegmentTree {
public:
    SegmentTree() {}
    SegmentTree(const int _N)
    {
        for (N = 1; N < _N; N <<= 1);
        Aint = vector<int> (2 * N + 3, 0);
    }

    void update(int poz, const int val)
    {
        Aint[poz += N] = val;
        for (poz >>= 1; poz; poz >>= 1)
            Aint[poz] = max(Aint[2 * poz], Aint[2 * poz + 1]);
    }

    int query(int left, int right)
    {
        left += N;
        right += N;

        int ret = 0;
        while (left <= right)
        {
            ret = max(ret, max(Aint[left], Aint[right]));
            left = (left + 1) >> 1;
            right = (right - 1) >> 1;
        }

        return ret;
    }
private:
    vector<int> Aint;
    int N;
};

SegmentTree A;

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

    int N, Q;
    scanf("%d%d", &N, &Q);

    A = SegmentTree(N);

    for (int i = 1; i <= N; i++)
    {
        int x;
        scanf("%d", &x);

        A.update(i, x);
    }

    while (Q--)
    {
        int op, x, y;
        scanf("%d%d%d", &op, &x, &y);

        if (op == 1)
            A.update(x, y);
        else
            printf("%d\n", A.query(x, y));
    }
}