Cod sursa(job #2374740)

Utilizator ikogamesIon Ceaun ikogames Data 7 martie 2019 20:15:20
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.61 kb
#include <bits/stdc++.h>

using namespace std;

int a[100000];
int sTree[200000];
int n, tasks;

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

void Read()
{
    fin >> n >> tasks;
    for(int i = 0 ; i < n; i++)
        fin >> a[i];
}

void Build(int low, int high, int pos)
{
    if(low == high)
    {
        sTree[pos] = a[low];
        return ;
    }
    int mid = (low + high) / 2;
    Build(low, mid, 2 * pos + 1);
    Build(mid + 1, high, 2 * pos + 2);
    sTree[pos] = max(sTree[2 * pos + 1], sTree[2 * pos + 2]);

}
int Query(int qLow, int qHigh, int low, int high, int pos)
{
    if(qLow <= low && qHigh >= high) ///total overlap
        {
            return sTree[pos];
        }
    if(qLow > high || qHigh < low) ///no overlap
        {
            return -1;

        }
    int mid = (low + high) / 2;
    return max(Query(qLow, qHigh, low, mid , 2 * pos + 1), Query(qLow, qHigh, mid + 1, high , 2 * pos + 2));
}

void Update(int qLow, int qHigh, int x, int y,int pos)
{
    if(qLow == qHigh)
    {
        sTree[pos] = y;
        return ;
    }
    int mid = (qLow + qHigh) / 2;
    if(x <= mid) Update(qLow, mid, x, y, 2 * pos + 1);
    else Update(mid + 1, qHigh, x, y, 2 * pos + 2);
    sTree[pos] = max(sTree[2 * pos + 1], sTree[2 * pos + 2]);
}

int main()
{
    Read();
    Build(0, n - 1, 0);
    int op, x, y;
    for(int i = 1; i <= tasks; i++)
    {
        fin >> op >> x >> y;
        if(op == 0)
            fout << Query(x - 1, y - 1, 0, n - 1, 0) << "\n";
        else
            Update(0, n - 1, x - 1, y, 0);
    }

    return 0;
}