Cod sursa(job #3142044)

Utilizator Traian_7109Traian Mihai Danciu Traian_7109 Data 18 iulie 2023 15:54:25
Problema Arbori de intervale Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.23 kb
#include <bits/stdc++.h>

#define int long long 

using namespace std;

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

struct AINT {
    int n;
    vector<int> a, aint;
    
    void init(const int &new_n) {
        n = new_n;
        a.resize(n+1), aint.resize(4*n+1);
        for (int i = 1; i <= n; i++)
            fin>>a[i];
    }
    
    void build(const int &node, const int &left, const int &right) {
        if (left == right)
            aint[node] = a[left];
        else {
            int middle = (left+right)/2;
            build(2*node, left, middle);
            build(2*node+1, middle+1, right);
            aint[node] = max(aint[2*node], aint[2*node+1]);
        }
    }
    
    void update(const int &node, const int &left, const int &right, const int &position, const int &value) {
        if (position <= left && right <= position)
            aint[node] = value;
        else {
            int middle = (left+right)/2;
            if (position <= middle)
                update(2*node, left, middle, position, value);
            if (middle < position)
                update(2*node+1, middle+1, right, position, value);
            aint[node] = max(aint[2*node], aint[2*node+1]);
        }
    }
    
    int query(const int &node, const int &left, const int &right, const int &start, const int &finish) {
        if (left == right)
            return aint[node];
        else {
            int middle = (left+right)/2, ans = 0;
            if (start <= middle)
                ans = max(ans, query(2*node, left, middle, start, finish));
            if (middle < finish)
                ans = max(ans, query(2*node+1, middle+1, right, start, finish));
            return ans;
        }
    }
};

AINT aint;

signed main() {
    ios_base :: sync_with_stdio(false);
    fin.tie(nullptr), fout.tie(nullptr);
    
    int n, m;
    fin>>n>>m;
    aint.init(n);
    aint.build(1, 1, n);
    while (m--) {
        int task;
        fin>>task;
        if (task == 0) {
            int left, right;
            fin>>left>>right;
            fout<<aint.query(1, 1, n, left, right)<<'\n';
        }
        else {
            int position, value;
            fin>>position>>value;
            aint.update(1, 1, n, position, value);
        }
    }
    return 0;
}