Cod sursa(job #1438333)

Utilizator andrei1998xAndrei Ionut andrei1998x Data 19 mai 2015 17:49:48
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.4 kb
#include <fstream>
using namespace std;
ifstream fin("arbint.in");
ofstream fout("arbint.out");

int a[400010],v[100001],n,m,i,maxim,t,x,y;
void build(int nod, int st, int dr){
    //construieste arborele de intervale ce reprezinta vectorul initial
    if (st==dr)
        a[nod]=v[st];
    else
    {
        int mid=(st+dr)/2;
        build(2*nod, st, mid);
        build(2*nod+1, mid+1, dr);
        a[nod]=max(a[2*nod],a[2*nod+1]);
    }
}
void query(int nod, int st, int dr, int x, int y){
    if (x<=st && dr<=y)
        maxim=max(maxim, a[nod]);
    else{
        int mid=(st+dr)/2;
        if (x<=mid)
            query(2*nod, st, mid, x, y);
        if (y>mid)
            query(2*nod+1, mid+1, dr, x, y);
    }
}
void update(int nod,int st,int dr,int x,int y)
{
    if(st==dr)
        a[nod]=y;
    else{
        int mid=(st+dr)/2;
        if (x<=mid)
            update(2*nod, st, mid, x, y);
        if (x>mid)
            update(2*nod+1, mid+1, dr, x, y);
        a[nod]=max(a[2*nod], a[2*nod+1]);
    }
}
int main()
{
    fin>>n>>m;
    for (i=1;i<=n;i++)
        fin>>v[i];
    build(1,1,n);
    for (i=1;i<=m;i++){
        fin>>t>>x>>y;
        if (t==0)
        {
            maxim=0;
            query(1,1,n,x,y);
            fout<<maxim<<"\n";
        }
        else
            update(1,1,n,x,y);//elem de pe poz x devine y
    }

    return 0;
}