Cod sursa(job #3292988)

Utilizator edi1Tudoran Eduard edi1 Data 9 aprilie 2025 22:22:31
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.51 kb
#include <bits/stdc++.h>
using namespace std;

#define ll long long

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

int n;
vector<int> tree;

void build(vector<int>& a, int poz, int st, int dr){
    if(st==dr)
        tree[poz]=a[st];
    else{
        int mid= (st+dr)/ 2;
        build(a, poz*2 , st, mid);
        build(a, poz*2+1, mid+1, dr);
        tree[poz]=max(tree[poz*2],tree[poz*2+1]);
    }
}

void update(int poz,int index, int val, int st, int dr){
    if(st==dr){
        tree[poz]=val;
    }else{
        int mid=(st+dr)/2;
        if(index<=mid)
            update(poz*2, index, val , st ,mid);
        else
            update(poz*2+1, index, val, mid+1, dr);

        tree[poz]= max(tree[2*poz],tree[2*poz+1]);
    }
}

int query(int poz, int st, int dr, int stbun, int drbun){
    if(stbun>drbun)
        return 0;

    if(stbun==st && drbun==dr)
        return tree[poz];

    int mid= (st+dr)/2;
    return max(query(poz*2, st, mid, stbun, min(drbun, mid)),
           query(poz*2+1, mid+1, dr, max(stbun, mid+1), drbun));
}


int main()
{
	ios::sync_with_stdio(false);
	fin.tie(nullptr);
    int m;
	fin>>n; fin>>m;
	tree.resize(4*n+1);
	vector<int> arr(n);

	for(auto &it:arr)
        fin>>it;

    build(arr,1,0,n-1);

    while(m--){
        int op; fin>>op;
        int x, y;
        fin>>x>>y;

        if(op==0)
            fout<<query(1,0,n-1, x-1, y-1)<<'\n';
        else
            update(1,x-1,y,0,n-1);
    }

    return 0;
}