Cod sursa(job #992843)

Utilizator timicsIoana Tamas timics Data 2 septembrie 2013 17:30:19
Problema Arbori de intervale Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.26 kb
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int a,b,x,M,N,v[110000],t[440000],c,d,maxy;

void update(int nod, int st, int dr)
{
    if(st==dr)
    {
        t[nod]=d;
        return;
    }

    int mij = (st+dr)/2;

    if(c<=mij)
    {
        update(2*nod,st,mij);
    }

    else
    {
        update(2*nod+1,mij+1,dr);
    }

    t[nod] = max(t[2*nod],t[2*nod+1]);

}

void getmax(int nod, int st, int dr)
{
    if(c<=st && dr<=d)
    {
        maxy = max(maxy,t[nod]);
        return;
    }

    int mij = (st+dr)/2;

    if(d>mij)
        getmax(nod*2+1,1+mij,dr);

    if(c<=mij)
        getmax(nod*2,st,mij);
}


int main()
{
    freopen("arbint.in","r",stdin);
    freopen("arbint.out","w",stdin);
    scanf("%d%d",&N,&M);
    for(int i=1;i<=N;++i)
    {
        scanf("%d",&x);
        c=i;
        d=x;
        update(1,1,N);
    }

    for(int i=1;i<=M;++i)
    {
        scanf("%d%d%d",&x,&a,&b);
        if(x==0)
        {
            c=a;
            d=b;
            maxy = -1;
            getmax(1,1,N);
            printf("%d\n",maxy);
        }
        else
        {
            c=a;
            d=b;
            update(1,1,N);
        }
    }
    return 0;
}