Cod sursa(job #603779)

Utilizator nervousNervous nervous Data 18 iulie 2011 17:14:21
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.05 kb
#include <fstream>
#include <cstring>

#define X1 262145
#define max(a,b) a>b ? a : b

using namespace std;

ifstream in;
ofstream out;

int v[X1];
int a,b;

inline void update(int nod,int L,int R)
{
    if(L==R) v[nod]=b;
    else
    {
        int M=(L+R)/2;
        if(a<=M) update(2*nod,L,M);
        else update(2*nod+1,M+1,R);

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

inline int query(int nod,int L,int R)
{
    if(a<=L&&R<=b) return v[nod];
    else
    {
        int M=(L+R)/2,max1=0,max2=0;
        if(a<=M) max1=query(2*nod,L,M);
        if(b>M) max2=query(2*nod+1,M+1,R);
        return max(max1,max2);
    }
}

int main()
{
    int N,M,tip;

    memset(v,0,sizeof(v));

    in.open("arbint.in");

    in>>N>>M;
    for(a=1;a<=N;++a)
    {
        in>>b;
        update(1,1,N);
    }

    out.open("arbint.out");

    for(;M;--M)
    {
        in>>tip>>a>>b;
        if(tip==0) out<<query(1,1,N)<<'\n';
        else update(1,1,N);
    }

    in.close();
    out.close();

    return 0;
}