Cod sursa(job #2787112)

Utilizator Robys01Robert Sorete Robys01 Data 22 octombrie 2021 16:04:28
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.2 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, m, baza, tree[270000];  // n <= 100000, ceil(log2(1000000)) = 17
                            // 2 ^ (17 + 1) - 1 = 262143 -> 270000

int maxim(int a, int b) 
{
    a += baza - 1;
    b += baza - 1;

    int maxx = 0;

    for(; a <= b; a/=2, b/=2) {
        if(a % 2 == 1)
            maxx = max(maxx, tree[a]), a++;
        if(b % 2 == 0)
            maxx = max(maxx, tree[b]), b--;
    }
    return maxx;
}

void update(int pos, int val) 
{
    pos += baza - 1;
    tree[pos] = val;

    pos/=2;

    for(; pos; pos/=2) 
        tree[pos] = max(tree[pos * 2], tree[pos * 2 + 1]);

}

int main()
{
    in>>n>>m;
    baza = (1 << (int)ceil(log2(n)));

    for(int i = baza; i < baza + n; i++) 
        in>>tree[i];

    for(int k = baza/2; k; k/=2)
        for(int i = k; i <= k * 2 - 1; i++)
            tree[i] = max(tree[2 * i], tree[2 * i + 1]);
    
    
    for(; m; m--) {
        int c, x, y;
        in>>c>>x>>y;

        if(c == 0) 
            out<<maxim(x, y)<<'\n';
        else if(c == 1)
            update(x, y);
    }
    

    return 0;
}