Cod sursa(job #2759316)

Utilizator lahayonTester lahayon Data 16 iunie 2021 20:20:55
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.97 kb
#include <fstream>	
#include <vector>


using namespace std;

void update(vector<int>& Arb, int node, int low, int high, const int& position, const int& value){

    if(low == high)
        Arb[node] = value;
    else{
          int mid = (low + high) / 2;
          if(position <= mid && 2 * node + 1 < Arb.size()){
            update(Arb, 2 * node + 1, low, mid, position, value);
          }
          if(position > mid && 2 * node + 2 < Arb.size()){
            update(Arb, 2 * node + 2, mid + 1, high, position, value);
            Arb[node] = max(Arb[node], Arb[2 * node + 2]);
          }
          int leftvalue = 0, rightvalue = 0;
          if(2 * node + 1 < Arb.size())
            leftvalue = Arb[2 * node + 1];
          if(2 * node + 2 < Arb.size())
            rightvalue = Arb[2 * node + 2];
          Arb[node] = max(leftvalue, rightvalue);
    }
}

int query(const vector<int>& Arb, int node, int low, int high, const int& left, const int& right){

    if(left <= low && high <= right)
        return Arb[node];
    else{
        int mid = (low + high) / 2, leftvalue = 0, rightvalue = 0;
        if(left <= mid && 2 * node + 1 < Arb.size())
            leftvalue = query(Arb, 2 * node + 1, low, mid, left, right);
        if(mid < right && 2 * node + 2 < Arb.size())
            rightvalue = query(Arb, 2 * node + 2, mid + 1, high, left, right);
        return max(leftvalue, rightvalue); 
    }
}



int main(){

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

    int N, M;
    cin >> N >> M;
    vector<int> Arb(2* N - 1, 0);
   
    for(int i = 0, x; i < N; ++i){
        cin >> x;
        update(Arb, 0, 0, N - 1, i, x);
    }

    for(int op, x, y; M > 0; --M){

        cin >> op >> x >> y;
        if(op){
            update(Arb, 0, 0, N - 1, --x, y);
        }
        else cout << query(Arb, 0, 0, N - 1, --x, --y) << "\n"; 
        
    }

    cin.close();
    cout.close();


    return 0;	
}