Cod sursa(job #2317312)

Utilizator DariusDCDarius Capolna DariusDC Data 13 ianuarie 2019 08:45:00
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.35 kb
#include <iostream>
#include <fstream>
#define NMAX 100001
#define MAX (1 << 30)

using namespace std;

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

int n, m;
int tree[4*NMAX];

void update(int node,int low,int high,int position,int value)
{
    if (low == high)
    {
        tree[node] = value;
        return;
    }
    int mid = (low + high) / 2;
    if (position <= mid)
        update(2* node,low,mid,position,value);
    if (position > mid)
        update(2*node+1,mid+1,high,position,value);
    tree[node] = max(tree[2*node],tree[2*node+1]);
}

int Querry(int node,int low,int high,int a,int b)
{
    if (low >= a && high <= b) // [a,b] sa cuprinda [low,high]
        return tree[node];
    int mid = (low + high) / 2;
    int left, right;
    if (a <= mid)
        left = Querry(2*node,low,mid,a,b);
    else
        left = -1;
    if (b > mid)
        right = Querry(2*node+1,mid+1,high,a,b);
    else
        right = -1;
    return max(left,right);
}

int main()
{
    fin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        int x;
        fin >> x;
        update(1,1,n,i,x);
    }
    while (m--)
    {
        int p, a, b;
        fin >> p >> a >> b;
        if (p == 0)
            fout << Querry(1,1,n,a,b) << "\n";
        else
            update(1,1,n,a,b);
    }
    return 0;
}