Cod sursa(job #1155606)

Utilizator Vladinho97Iordan Vlad Vladinho97 Data 27 martie 2014 00:46:48
Problema Arbori de intervale Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.29 kb
#include <cstdio>
#include <algorithm>
#include<fstream>
#define nmax 100009
using namespace std;
int arb[3*nmax],n;
void update(int poz,int x,int val,int inc,int sf)
{
    if(inc==sf)
    {
        arb[poz]=val;
    }
    else
    {
        int med=(inc+sf)/2;
        if(x<=med)
        {
            update(poz*2,x,val,inc,med);
        }
        else
        {
            update(poz*2+1,x,val,med+1,sf);
        }
        arb[poz]=max(arb[poz*2],arb[poz*2+1]);
    }
}
int query(int poz,int st,int dr,int inc,int sf)
{
    if(inc>=st&&sf<=dr)
        return arb[poz];
    int med=(inc+sf)/2;
    if(st<=med&&dr>med)
        return max(query(poz*2,st,med,inc,med),
                   query(poz*2+1,med+1,dr,med+1,sf));
    if(st<=med)
        return query(poz*2,st,dr,inc,med);
    return query(poz*2+1,st,dr,med+1,sf);
}
int main()
{
    ifstream f("arbint.in");
    ofstream g("arbint.out");
    int i,m,op,x,y;
    f>>n>>m;
    for(i=1;i<=n;i++)
    {
        f>>x;
        update(1,i,x,1,n);
    }
    for(i=1;i<=m;++i)
    {
        f>>op;
        if(op==1)
        {
            f>>x>>y;
            update(1,x,y,1,n);
        }
        if(op==0)
        {
            f>>x>>y;
            g<<query(1,x,y,1,n)<<"\n";
        }
    }
    return 0;
}