Cod sursa(job #3226297)

Utilizator Victor_9Diaconescu Victor Victor_9 Data 20 aprilie 2024 20:32:28
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.45 kb
//arb-------------------------------------------------------------------
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int nmax = 1e5 + 5;

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

int aint[4 * nmax], offset, n, m, x, cerinta, a, b;


void update_begin(int poz, int val){
    
    aint[offset + poz] = val;
    
    for(int j=(offset + poz) / 2;j>0;j/=2){
        aint[j] = max (aint[j] , val);
    }
    
}

void update(int poz, int val){
    
    aint[offset + poz] = val;
    
    for(int j=(offset + poz) / 2;j>0;j/=2){
        aint[j] = max (aint[j*2] , aint[j*2+1]);
    }
    
}


int query(int node, int l, int r, int gl, int gr){
    
    if(l > gr || r < gl) return 0;
    
    if(gl <= l && r <= gr) return aint[node];
    
    int mij = (l + r) / 2;
    
    int stanga = query(node*2, l, mij, gl, gr);
    int dreapta = query(node*2+1, mij+1, r, gl, gr);
    
    //cout<<stanga<<" "<<node*2<<" "<<aint[node*2]<<"  "<<dreapta<<" "<<node*2+1<<endl;
    return max(stanga , dreapta);
    
}


signed main()
{
    fin>>n>>m;
    
    offset = 1;
    while(offset < n){
        offset *= 2;
    }

    
    for(int i=1;i<=n;i++){
        fin>>x;
        update_begin(i-1 , x);
    }
    
    
    for(int i=1;i<=m;i++){
        fin>>cerinta>>a>>b;
        
        if(cerinta == 1){
            update(a-1, b);
        }else{
            fout<<query(1, 0, offset-1, a-1, b-1)<<"\n";
        }
        
    }
    
    
    
}