Cod sursa(job #2579850)

Utilizator Alex100Alexandru Mihai Alex100 Data 12 martie 2020 21:30:48
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.25 kb
#include <bits/stdc++.h>
#define MAX 410000
using namespace std;
ifstream fin("arbint.in");
ofstream fout("arbint.out");
int arb[MAX];
inline int maxim(int a,int b)
{
    if(a>b)
        return a;
    return b;
}
void update(int node,int st,int dr,int pos,int val);
int query(int node,int st,int dr,int x,int y);
int main()
{
    int n,m,x,y,ce,i;
    fin>>n>>m;
    for(i=1;i<=n;i++)
    {
        fin>>x;
        update(1,1,n,i,x);
    }
    for(i=1;i<=m;i++)
    {
        fin>>ce>>x>>y;
        if(ce==1)
            update(1,1,n,x,y);
        else
            fout<<query(1,1,n,x,y)<<'\n';
    }
    return 0;
}
void update(int node,int st,int dr,int pos,int val)
{
    int mij;
    if(st==dr)
    {
        arb[node]=val;
        return;
    }
    mij=(st+dr)/2;
    if(pos<=mij)
        update(2*node,st,mij,pos,val);
    else
        update(2*node+1,mij+1,dr,pos,val);
    arb[node]=maxim(arb[2*node],arb[2*node+1]);
}
int query(int node,int st,int dr,int x,int y)
{
    int mij,ans_l=0,ans_r=0;
    if(x<=st&&dr<=y)
        return arb[node];
    mij=(st+dr)/2;
    if(x<=mij)
        ans_l=query(2*node,st,mij,x,y);
    if(mij<y)
        ans_r=query(2*node+1,mij+1,dr,x,y);
    return maxim(ans_l,ans_r);
}