Cod sursa(job #3287970)

Utilizator Carnu_EmilianCarnu Emilian Carnu_Emilian Data 20 martie 2025 09:32:55
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.06 kb
#include <bits/stdc++.h>
using namespace std;

ifstream fcin("arbint.in");
ofstream fcout("arbint.out");

int n, k, a[400010], Q;

void Update(int p, int x)
{
    p += (n - 1);
    a[p] = x;
    while(p > 1)
    {
        p /= 2;
        a[p] = max(a[2 * p], a[2 * p + 1]);
    }
}

int Query(int k, int x, int y, int st, int dr)
{
    if(x == st && y == dr) return a[k];
    int m = (st + dr) / 2;
    if(y <= m) return Query(2 * k, x, y, st, m);
    if(x >= m + 1) return Query(2 *k + 1, x, y, m + 1, dr);
    return max(Query(2 * k, x, m, st, m), Query(2 *k+ 1, m + 1, y, m + 1, dr));
}

int main()
{
    int i, x, y, op;
    fcin >> k >> Q;
    for(n = 1; n < k; n *= 2);
    for(i = 1; i <= k; i++)
        fcin >> a[n - 1 + i];
    for (i = n + k; i < 2 * n; i++)
        a[i] = 0;
    for(i = n - 1; i >= 1; i--)
        a[i] = max(a[2 * i], a[2 * i + 1]);
    for(i = 1; i <= Q; i ++)
    {
        fcin >> op >> x >> y;
        if(op == 0) fcout << Query(1, x, y, 1, n) << "\n";
        else Update(x, y);
    }
    return 0;
}