Cod sursa(job #1378261)

Utilizator vlady1997Vlad Bucur vlady1997 Data 6 martie 2015 11:17:10
Problema Arbori de intervale Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
#include <cstdio>
#include <algorithm>
using namespace std;
int a[200001], Max;
void update (int poz, int val, int nod, int st, int dr)
{
    int m=(st+dr)/2;
    if (st==dr && st==poz)
    {
        a[nod]=val;
        return;
    }
    if (m<poz)
    {
        update(poz,val,nod*2+1,m+1,dr);
    }
    else
    {
        update(poz,val,nod*2,st,m);
    }
    a[nod]=max(a[nod*2],a[nod*2+1]);
}
void query (int p, int r, int nod, int st, int dr)
{
    int m=(st+dr)/2;
    if (st>=p && dr<=r)
    {
        if (a[nod]>Max) Max=a[nod];
        return;
    }
    if (m>=p)
    {
        query(p,r,nod*2,st,m);
    }
    if (m<r)
    {
        query(p,r,nod*2+1,m+1,dr);
    }
}
int main()
{
    int n, m, i, p, x, y;
    freopen("arbint.in","r",stdin);
    freopen("arbint.out","w",stdout);
    scanf("%d%d",&n,&m);
    for (i=1; i<=n; i++)
    {
        scanf("%d",&x);
        update(i,x,1,1,n);
    }
    for (i=1; i<=m; i++)
    {
        scanf("%d%d%d",&p,&x,&y);
        if (p==0)
        {
            Max=0;
            query(x,y,1,1,n);
            printf("%d\n",Max);
        }
        else
        {
            update(x,y,1,1,n);
        }
    }
    return 0;
}