Cod sursa(job #2158051)

Utilizator maria_sinteaMaria Sintea maria_sintea Data 10 martie 2018 10:05:40
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.66 kb
#include <iostream>
#include <cstdio>
#define N 100005
#define inf 0x3f3f3f3f
#define max(a, b) a>b?a:b

using namespace std;

int n, m, x, y, op;
int a[N], arbint[4*N];

void citire()
{
    scanf("%d %d\n", &n, &m);
    for(int i=1;i<=n;i++)
        scanf("%d ", &a[i]);
}

void constructie(int st, int dr, int poz)
{
    if(st==dr)
        arbint[poz]=a[st];
    else
    {
         int mij=(st+dr)/2;
         constructie(st, mij, poz*2);
         constructie(mij+1, dr, poz*2+1);
         arbint[poz]=max(arbint[poz*2], arbint[poz*2+1]);
    }
}

int query(int x, int y, int st, int dr, int nod)
{
    if(x<=st && y>=dr)
        return arbint[nod];
    int mij=(st+dr)/2, maxa=-inf, maxb=-inf;
    if(x<=mij)
        maxa=query(x, y, st, mij, nod*2);
    if(y>=mij+1)
        maxb=query(x, y, mij+1, dr, nod*2+1);
    return max(maxa, maxb);
}

int update(int poz, int st, int dr, int nod)
{
    if(st==dr)
        arbint[nod]=a[poz];
    else
    {
        int mij=(st+dr)/2;
        if(poz>mij)
            update(poz, mij+1, dr, 2*nod+1);
        else
            update(poz, st, mij, 2*nod);
        arbint[nod]=max(arbint[2*nod+1], arbint[2*nod]);
    }
}

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

    citire();
    constructie(1, n, 1);
    for(int i=1;i<=m;i++)
    {
        scanf("%d %d %d\n", &op, &x, &y);
        if(op==0)
            printf("%d\n", query(x, y, 1, n, 1));
        else
        {
            a[x]=y;
            update(x, 1, n, 1);
        }
    }
//    for(int i=1;i<=2*n-1;i++)
//        printf("%d ", arbint[i]);
    return 0;
}