Cod sursa(job #1424387)

Utilizator UAIC_HreapcaVlasNegrusUAIC Hreapca Vlas Negrus UAIC_HreapcaVlasNegrus Data 24 aprilie 2015 10:51:09
Problema Arbori de intervale Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.26 kb
#include <iostream>
#include <deque>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;

int A[2000010], N, M, i, t, a, b;

void update(int node, int st, int dr, int pos, int val)
{
    if(st == dr && st == pos)
    {
        A[node] = val;
        return;
    }
    int mid = (st + dr) / 2;
    if(pos <= mid)
        update(2 * node, st, mid, pos, val);
    else
        update(2 * node + 1, mid + 1, dr, pos, val);
    A[node] = max(A[node + node], A[node + node + 1]);
}

int query(int node, int st, int dr, int left, int right)
{
    if(st >= left && dr <= right)
        return A[node];
    int mid = (st + dr) / 2;
    int x = 0, y = 0;
    if(left <= mid)
        x = query(2 * node, st, mid, left, mid);
    if(right > mid)
        y = query(2 * node + 1, mid + 1, dr, mid + 1, right);
    return max(x, y);
}

int main()
{
    freopen("arbint.in","r",stdin);
    freopen("arbint.out","w",stdout);
    scanf("%d%d", &N, &M);
    for(i = 1; i <= N; i++)
    {
        scanf("%d", &a);
        update(1, 1, N, i, a);
    }
    while(M--)
    {
        scanf("%d%d%d", &t, &a, &b);
        if(t == 0)
        {
            printf("%d\n", query(1, 1, N, a, b));
        }
        else
        {
            update(1, 1, N, a, b);
        }
    }
    return 0;
}