Cod sursa(job #2364296)

Utilizator BogdanGhGhinea Bogdan BogdanGh Data 3 martie 2019 23:19:42
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.3 kb
#include <fstream>
using namespace std;
ifstream f("arbint.in");
ofstream g("arbint.out");
int n,m,i,a[100001],tree[4*100001],x,y,p,maxim;
void recalculate(int nod)
{
    tree[nod] = max(tree[2 * nod ], tree[2 * nod + 1 ]);
}
void build(int nod, int st, int dr)
{

    if (st == dr)
        tree[nod] = a[st];

    else
    {
        int mij = (st + dr) / 2;
        build(2 * nod, st, mij);
        build(2 * nod + 1, mij + 1, dr);
        recalculate(nod);
    }
}
void update(int nod,int st,int dr)
{
    if(st==dr)
            {tree[nod]=y;
            return;}
        int mij=(st+dr)/2;
        if(x<=mij)
            update(2 * nod, st, mij);
        else
            update(2 * nod + 1, mij + 1, dr);
        recalculate(nod);

}
int querry(int nod,int st,int dr)
{
    int x1=0,y1=0;
    if(x<=st&&dr<=y)
    return tree[nod];
    else{
        int mij=(st+dr)/2;
        if(x<=mij)
            x1=querry(nod*2,st,mij);
         if(y>mij)
            y1=querry(nod*2+1,mij+1,dr);
       return max(x1,y1);}
}
int main()
{
    f>>n>>m;
    for(i=1; i<=n; i++)
        f>>a[i];
    build(1,1,n);
    for(i=1; i<=m; i++)
    {
        f>>p>>x>>y;
        if(p)
            update(1,1,n);
        else
            g<<querry(1,1,n)<<'\n';
    }
    return 0;
}