Cod sursa(job #1795639)

Utilizator OlivianOlivian Dan Cretu Olivian Data 2 noiembrie 2016 19:05:12
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.1 kb
#include<cstdio>
#include<algorithm>
using namespace std;
int arb[400007];
void arbint_update(int nod,int st,int dr,int poz,int val)
{
    if(st==dr) arb[nod]=val;
    else
    {
        int mid=(st+dr)/2;
        if(poz<=mid) arbint_update(nod*2,st,mid,poz,val);
        else arbint_update(nod*2+1,mid+1,dr,poz,val);
        arb[nod]=max(arb[nod*2],arb[nod*2+1]);
    }
}
int arbint_query(int nod,int st,int dr,int left,int right)
{
    if(left<=st && dr<=right) return arb[nod];
    int mid=(st+dr)/2,s=0;
    if(left<=mid) s=max(s,arbint_query(nod*2,st,mid,left,right));
    if(mid<right) s=max(s,arbint_query(nod*2+1,mid+1,dr,left,right));
    return s;
}
int main()
{
    freopen("arbint.in", "r", stdin);
    freopen("arbint.out", "w", stdout);
    int n,m,tip,a,b;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a);
        arbint_update(1,1,n,i,a);
    }
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&tip,&a,&b);
        if(tip==0) printf("%d\n",arbint_query(1,1,n,a,b));
        else arbint_update(1,1,n,a,b);
    }
    return 0;
}