Cod sursa(job #2450858)

Utilizator AlexandruGabrielAliciuc Alexandru AlexandruGabriel Data 24 august 2019 18:21:06
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.35 kb
#include <bits/stdc++.h>
#define N 100005

using namespace std;

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

int n, m, a[N], op, k, ans = -1;
int aint[4 * N];

void Query(int pos, int st, int dr, int x, int y)
{
    int mij = (st + dr) / 2;

    if (st <= dr)
    {
        if (x >= st && dr <= y)
            ans = max(ans, aint[pos]);
        else
        {
            if (x <= mij)
                Query(2 * pos, st, mij, x, y);

            if (y >= mij + 1)
                Query(2 * pos + 1, mij + 1, dr, x, y);
        }
    }

}

void Update(int pos, int st, int dr, int x, int val)
{
    int mij = (st + dr) / 2;

    if (st == dr)
    {
        aint[pos] = val;
        return;
    }

    if (x <= mij)
        Update(2 * pos, st, mij, x, val);
    else
        Update(2 * pos + 1, mij + 1, dr, x, val);

    aint[pos] = max(aint[2 * pos], aint[2 * pos + 1]);

}

int main()
{
    fin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        fin >> a[i];
        Update(1, 1, n, i, a[i]);
    }

    for (int i = 1; i <= m; i++)
    {
        int x, y;
        fin >> op >> x >> y;

        if (op == 0)
        {
            ans = -1;
            Query(1, 1, n, x ,y);
            fout << ans << "\n";
        }
        else
            Update(1, 1, n, x, y);

    }
    return 0;
}