Cod sursa(job #2817455)

Utilizator LionMan101Achim-Panescu Silvian LionMan101 Data 13 decembrie 2021 18:24:26
Problema Arbori de intervale Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.2 kb
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
const int INF=1e9+7;
const ll INFF=1e18+7;
#define nl '\n'
void __print(int x) { cout << x; }
void __print(long x) { cout << x; }
void __print(long long x) { cout << x; }
void __print(unsigned x) { cout << x; }
void __print(unsigned long x) { cout << x; }
void __print(unsigned long long x) { cout << x; }
void __print(float x) { cout << x; }
void __print(double x) { cout << x; }
void __print(long double x) { cout << x; }
void __print(char x) { cout << '\'' << x << '\''; }
void __print(const char* x) { cout << '\"' << x << '\"'; }
void __print(const string& x) { cout << '\"' << x << '\"'; }
void __print(bool x) { cout << (x ? "true" : "false"); }
template<typename T, typename V>
void __print(const pair<T, V>& x) { cout << '{'; __print(x.first); cout << ','; __print(x.second); cout << '}'; }
template<typename T>
void __print(const T& x) { int f = 0; cout << '{'; for (auto& i : x) cout << (f++ ? "," : ""), __print(i); cout << "}"; }
void _print() { cout << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) { __print(t); if (sizeof...(v)) cout << ", "; _print(v...); }
#ifndef ONLINE_JUDGE
#define debug(x...) cout << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

class SegTree{
public:
    int leftmost, rightmost;
    int minn=0;
    SegTree *leftChild=nullptr, *rightChild=nullptr;
    SegTree(int leftmost, int rightmost, vector<int> a) :
    leftmost(leftmost), rightmost(rightmost){
        if(leftmost==rightmost) minn=a[leftmost]; //leaf
        else{ // 2 children
            int mid=(leftmost+rightmost)/2;
            leftChild = new SegTree(leftmost, mid, a);
            rightChild = new SegTree(mid+1, rightmost, a);
            recalc();
        }
    }

    void recalc(){
        if(leftmost==rightmost) return;
        minn=max(leftChild->minn,rightChild->minn);
    }

    void pointUpdate(int index, int newVal){
        if(leftmost==rightmost) minn=newVal;
        else if(index<=leftChild->rightmost) leftChild->pointUpdate(index,newVal);
        else rightChild->pointUpdate(index, newVal);
        recalc();
    }

    ll rangeSum(int l, int r){
        //entirely disjoint
        if(l>rightmost || r<leftmost) return 0;

        //covers us
        if(l<=leftmost && r>=rightmost) return minn;

        //wdk
        return max(leftChild->rangeSum(l,r),rightChild->rangeSum(l,r));
    }
};

void solve()
{
    int n,q;
    cin>>n>>q;
    vector<int> a(n);
    for(int i=0; i<n; i++){
        cin>>a[i];
    }
    SegTree tree(0,n-1,a);
    // cin>>q;
    for(int i=0; i<q; i++){
        int c, x, y;
        cin>>c>>x>>y;
        if(c==0) cout<<tree.rangeSum(x-1,y-1)<<nl;
        else tree.pointUpdate(x-1,y);
    }
}

int main()
{
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
// #ifndef ONLINE_JUDGE
    freopen("arbint.in", "r", stdin);
    freopen("arbint.out", "w", stdout);
// #endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t=1;
    // int t;
    // cin >> t;
    for(int tt=1; tt<=t; tt++){
        // cout<<"#Case "<<t<<nl;
        solve();
    }
    return 0;
}