Cod sursa(job #2461760)

Utilizator mihailescu_eduardMihailescu Eduard-Florin mihailescu_eduard Data 26 septembrie 2019 09:34:18
Problema Arbori de intervale Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.52 kb
#include <bits/stdc++.h>

using namespace std;

static const int NMAX = 1e5+5;

ifstream fin("arbint.in");
ofstream fout("arbint.out");

int aint[NMAX];
int v[NMAX];

void build(int node,int low, int high)
{
    if(low == high)
    {
        aint[node] = v[low];
        return;
    }
    int mid = low+(high-low)/2;
    build(node*2+1,low,mid);
    build(node*2+2,mid+1,high);

    aint[node] = max(aint[node*2+1],aint[node*2+2]);

}
void update(int node,int low, int high, int pos, int val)
{
    if(low == high)
    {
        aint[node] = val;
        return;
    }
    int mid = low+(high-low)/2;
    if(pos <=mid)
        update(node*2+1,low,mid,pos,val);
    else
        update(node*2+2,mid+1,high,pos,val);

    aint[node] = max(aint[node*2+1],aint[node*2+2]);
}
int quary(int node,int low,int high, int a,int b)
{
    if(a> high || b < low)
        return 0;
    if(a<= low && b >= high)
        return aint[node];
    else
    {
        int mid = low + (high-low)/2;
        return max(quary(node*2+1,low,mid,a,b),quary(node*2+2,mid+1,high,a,b));
    }

}

int main()
{
    int n,m;
    fin>> n>> m;
    for(int i= 0; i<n;++i)
    {
        fin >> v[i];
    }
    build(0,0,n-1);
    for(int i = 0; i< m; ++i)
    {
        int op;fin >> op;
        int a,b;
        fin >> a >> b;
        if(op == 0)
        {
            fout << quary(0,0,n-1,a-1,b-1)<<'\n';
        }
        else
        {
            update(0,0,n-1,a-1,b);
        }
    }
    return 0;
}