Cod sursa(job #1468487)

Utilizator vlady1997Vlad Bucur vlady1997 Data 6 august 2015 10:52:15
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
#include <cstdio>
#include <algorithm>
using namespace std;
int a[400001], Max;
void update (int x, int poz, int nod, int st, int dr)
{
    int m=(st+dr)/2;
    if (st==dr && st==poz)
    {
        a[nod]=x;
        return;
    }
    else if (m<poz)
    {
        update(x,poz,nod*2+1,m+1,dr);
    }
    else
    {
        update(x,poz,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 ,x, y, z;
    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(x,i,1,1,n);
    }
    for (i=1; i<=m; i++)
    {
        scanf("%d%d%d",&x,&y,&z);
        if (x==0)
        {
            Max=-1;
            query(y,z,1,1,n);
            printf("%d\n",Max);
        }
        else
        {
            update(z,y,1,1,n);
        }
    }
    return 0;
}