Cod sursa(job #900554)

Utilizator rootsroots1 roots Data 28 februarie 2013 20:21:33
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.78 kb
#include <cstdio>

#define buffLen 131071
#define aiLen 262145
 
using namespace std;
 
char buff[buffLen];
int index;
 
int ai[aiLen];
 
inline void read(int & num)
{
    num = 0;
     
    while(buff[index] < '0' || '9' < buff[index])
        if( ++ index == buffLen)
        {
            index = 0;
            fread(buff, 1, buffLen, stdin);
        }
 
    while('0' <= buff[index] && buff[index] <= '9')
    {
        num = num * 10 + buff[index] - '0';
        if( ++ index == buffLen)
        {
            index = 0;
            fread(buff, 1, buffLen, stdin);
        }
    }
}
 
inline int max(int a, int b)
{
    if(a > b) return a;
    return b;
}
 
inline void update(int nod, int L, int R, int idx, int val)
{
    if(L == idx && idx == R)
    {
        ai[nod] = val;
        return;
    }
 
    int M = L + (R - L) / 2;
 
    if(idx <= M) update(2 * nod, L, M, idx, val);
    else update(2 * nod + 1, M + 1, R, idx, val);
 
    ai[nod] = max(ai[2 * nod], ai[2 * nod + 1]);
}
 
inline int query(int nod, int L, int R, int a, int b)
{
    if(a <= L && R <= b) return ai[nod];
 
    int M = L + (R - L) / 2;
 
    int ret1 = 0;
    int ret2 = 0;
 
    if(a <= M) ret1 = query(2 * nod, L, M, a, b);
    if(b > M) ret2 = query(2 * nod + 1, M + 1, R, a, b);
 
    return max(ret1, ret2);
}
 
int main()
{
    freopen("arbint.in", "r", stdin);
    freopen("arbint.out", "w", stdout);
 
    fread(buff, 1, buffLen, stdin);
 
    int N, M;
    read(N);
    read(M);
 
    for(int i = 1; i <= N; ++ i)
    {
        int x;
        read(x);
 
        update(1, 1, N, i, x);
    }
 
    while(M -- )
    {
        int x, a, b;
        read(x);
        read(a);
        read(b);
 
        if(x == 0)
            printf("%d\n", query(1, 1, N, a, b));
        else
            update(1, 1, N, a, b);
    }
 
    return 0;
}