Cod sursa(job #2759985)

Utilizator GeorgianBaditaBadita Marin-Georgian GeorgianBadita Data 21 iunie 2021 23:21:58
Problema Arbori de intervale Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.83 kb
#include<bits/stdc++.h>
#define NMAX 100000
#define pb push_back
#define MAX(A, B) A >= B ? A : B

using namespace std;

vector<int> T;

int makePowOfTwo(int n) {
    while((n & (n - 1)) != 0) {
        n ++;
    }
    return n;
}

int query(int node, int nodeLow, int nodeHigh, int queryLow, int queryHigh) {
    if(nodeLow >= queryLow && queryHigh >= nodeHigh) {
        return T[node];
    }
    if(nodeHigh < queryLow || nodeLow > queryHigh) {
        return 0;
    }

    int onLeft = (nodeHigh + nodeLow) / 2;
    return MAX(query(2 * node, nodeLow, onLeft, queryLow, queryHigh),
               query(2 * node + 1, onLeft + 1, nodeHigh, queryLow, queryHigh));
}

void update(int node, int nodeLow, int nodeHigh, int queryLow, int queryHigh, int value) {
    if(nodeLow >= queryLow && queryHigh >= nodeHigh) {
        T[node] = value;
        return;
    }
    if(nodeHigh < queryLow || nodeLow > queryHigh) {
        return;
    }

    int onLeft = (nodeHigh + nodeLow) / 2;
    update(2 * node, nodeLow, onLeft, queryLow, queryHigh, value);
    update(2 * node + 1, onLeft + 1, nodeHigh, queryLow, queryHigh, value);
    T[node] = MAX(T[2 * node], T[2 * node + 1]);
}

int main() {

    int N, M;
    ifstream f("arbint.in");
    ofstream g("arbint.out");

    f >> N >> M;
    int powOf2 = makePowOfTwo(N);
    T.resize(2 * powOf2);
    for(int i = 0; i < N; i++) {
        f >> T[powOf2 + i];
    }
    N = powOf2;
    for(int i = N - 1; i >= 1; i--) {
        T[i] = MAX(T[2 * i], T[2 * i + 1]);
    }

    while(M --) {
        int op, a, b;
        f >> op >> a >> b;
        if(op == 1) {
            a --;
            update(1, 0, N - 1, a, a, b);
        } else {
            a --;
            b --;
            g << query(1, 0, N - 1, a, b) << '\n';
        }
    }
    return 0;
}