Cod sursa(job #2639103)

Utilizator NeganAlex Mihalcea Negan Data 31 iulie 2020 13:34:22
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.95 kb
#include <bits/stdc++.h>

using namespace std;

const int INF = (1 << 30);
const int NMAX = 100005;
int N, M, v[NMAX];
int aint[4 * NMAX], answer;

int LeftSon(int node)
{
    return 2 * node;
}

int RightSon(int node)
{
    return 2 * node + 1;
}

void Build(int node, int left, int right)
{
    if (left == right) ///frunza
    {
        aint[node] = v[left];
        return;
    }
    int mid = (left + right) / 2;
    Build(LeftSon(node), left, mid);
    Build(RightSon(node), mid + 1, right);
    aint[node] = max(aint[LeftSon(node)], aint[RightSon(node)]);
}

void Update(int node, int left, int right, int pos, int val)
{
    if (left == right) ///pos = left = right
    {
        aint[node] = val;
        v[pos] = val;
        return;
    }
    int mid = (left + right) / 2;
    if (pos <= mid)
        Update(LeftSon(node), left, mid, pos, val);
    else
        Update(RightSon(node), mid + 1, right, pos, val);
    aint[node] = max(aint[LeftSon(node)], aint[RightSon(node)]);
}

void Query(int node, int left, int right, int LeftQuery, int RightQuery)
{
    if (LeftQuery <= left && right <= RightQuery)
    {
        answer = max(answer, aint[node]);
        return;
    }
    int mid = (left + right) / 2;
    if (LeftQuery <= mid)
        Query(LeftSon(node), left, mid, LeftQuery, RightQuery);
    if (RightQuery >= mid + 1)
        Query(RightSon(node), mid + 1, right, LeftQuery, RightQuery);
}

int main()
{
    ifstream fin("arbint.in");
    ofstream fout("arbint.out");
    fin >> N >> M;
    for (int i = 1;i <= N;++i)
        fin >> v[i];
    Build(1, 1, N);
    for (int i = 1;i <= M;++i)
    {
        int op, a, b;
        fin >> op >> a >> b;
        if (op == 0)
        {
            answer = -INF;
            Query(1, 1, N, a, b);
            fout << answer << "\n";
        }
        else
            Update(1, 1, N, a, b);
    }
    fin.close();
    fout.close();
    return 0;
}