Cod sursa(job #2673857)

Utilizator Giulian617Buzatu Giulian Giulian617 Data 17 noiembrie 2020 21:40:36
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.4 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("arbint.in");
ofstream g("arbint.out");
const int NMAX=100005;
int n,m,x,op,a,b,start,finish,val,pos,maxx,MaxArb[4*NMAX+70];
void update(int nod,int left,int right)
{
    if(left==right)//leaf
    {
        MaxArb[nod]=val;
        return;
    }
    int middle=(left+right)/2;
    if(pos<=middle)
        update(2*nod,left,middle);//update left son
    else
        update(2*nod+1,middle+1,right);//update right son
    MaxArb[nod]=max(MaxArb[2*nod],MaxArb[2*nod+1]);
}
void query(int nod,int left,int right)
{
    if(start<=left && right<=finish)//verify if the query is valid
    {
        if(maxx<MaxArb[nod])
            maxx=MaxArb[nod];
        return;
    }
    int middle=(left+right)/2;
    if(start<=middle)
        query(2*nod,left,middle);//query left son
    if(middle<finish)
        query(2*nod+1,middle+1,right);//query right son
}
int main()
{
    f>>n>>m;
    for(int i=1; i<=n; i++)
    {
        f>>x;
        pos=i;
        val=x;
        update(1,1,n);
    }
    for(int i=1; i<=m; i++)
    {
        f>>op>>a>>b;
        if(op==0)
        {
            maxx=-1;
            start=a;
            finish=b;
            query(1,1,n);
            g<<maxx<<"\n";
        }
        else
        {
            pos=a;
            val=b;
            update(1,1,n);
        }
    }
    return 0;
}