Cod sursa(job #1801310)

Utilizator jason2013Andronache Riccardo jason2013 Data 8 noiembrie 2016 21:07:40
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.26 kb
#include<bits/stdc++.h>
using namespace std;

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

const int NMax = 1000000;
int input[NMax], segTree[NMax];


void constructTree(int low, int high, int pos, int val, int nod)
{
    if(low == high)
    {
        segTree[nod] = val;
        return;

    }
    int mid = ( low + high ) / 2;
    if(pos<=mid)
        /*1<-------*/  constructTree(low, mid, pos, val, nod*2);
    if(pos>mid)
        /*2<-------*/  constructTree(mid+1, high, pos, val, nod*2+1);

    segTree[nod] = max(segTree[nod*2], segTree[nod*2+1]);
}

int rangeMaxQuery(int low,int high,int qlow,int qhigh,int nod)
{
    if(low>=qlow && high<=qhigh)
        return segTree[nod];
    if(low>qhigh || high<qlow)
        return INT_MIN;
    int mid = (low+high)/2;
    return max(rangeMaxQuery(low,mid,qlow,qhigh,2*nod),
               rangeMaxQuery(mid+1,high,qlow, qhigh,2*nod+1));
}

int main()
{
    int n,m,a,b,t;
    fin>>n>>m;
    for(int i=1; i<=n; i++)
    {
        fin>>a;
        constructTree(1,n,i,a,1);
    }
    for(int i=1; i<=m; i++)
    {
        fin>>t>>a>>b;
        if(t==0)
            fout<<rangeMaxQuery(1,n,a,b,1)<<'\n';
        else
            constructTree(1,n,a,b,1);
    }
    return 0;
}