Cod sursa(job #2606215)

Utilizator ionutomutiuIonut Tomutiu ionutomutiu Data 27 aprilie 2020 12:17:17
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.16 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("arbint.in");
ofstream fout ("arbint.out");
int N;
int A[100005];
int M;
int Tree[300000];
string ope[100005];
int MaxTree (int left, int right, int position)
{
    if (left == right)
    {
        Tree[position] = A[left];
        return A[left];
    }
    int mid = (left + right) / 2;
    int sol1 = MaxTree (left, mid, position * 2);
    int sol2 = MaxTree (mid + 1, right, position * 2 + 1);
    Tree[position] = max (sol1, sol2);
    return Tree[position];
}
int CalcMaximum (int a, int b, int left, int right, int position)
{
    if (a == left && b == right)
        return Tree[position];
    int mid = (left + right) / 2;
    if (b <= mid)
        return CalcMaximum (a, b, left, mid, position * 2);
    else if (a > mid)
        return CalcMaximum (a, b, mid + 1, right, position * 2 + 1);
    else
    {
        int sol1 = CalcMaximum (a, mid, left, mid, position * 2);
        int sol2 = CalcMaximum (mid + 1, b, mid + 1, right, position * 2 + 1);
        int maximum = max (sol1, sol2);
        return maximum;
    }
}
int Position (int x, int left, int right, int position)
{
    if (x == left && right == left)
        return position;
    int mid = (left + right) / 2;
    if (x > mid)
        return Position (x, mid + 1, right, position * 2 + 1);
    else
        return Position (x, left, mid, position * 2);
}
void RedoMaximum (int position)
{
    while (position / 2)
    {
        if (position % (position / 2))
            Tree[position / 2] = max (Tree[position], Tree[position - 1]);
        else
            Tree[position / 2] = max (Tree[position], Tree[position + 1]);
        position /= 2;
    }
}
int main ()
{
    fin >> N >> M;
    for (int i = 1; i <= N; i++)
        fin >> A[i];
    int maxi = MaxTree (1, N, 1);
    for (int i = 1; i <= M; i++)
    {
        int cases, a, b;
        fin >> cases >> a >> b;
        if (cases == 0)
            fout << CalcMaximum (a, b, 1, N, 1) << "\n";
        else
        {
            int pos = Position (a, 1, N, 1);
            Tree[pos] = b;
            RedoMaximum (pos);
        }
    }
}