Cod sursa(job #2549942)

Utilizator Alexa2001Alexa Tudose Alexa2001 Data 18 februarie 2020 09:39:25
Problema Arbori de intervale Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.07 kb
#include <bits/stdc++.h>

using namespace std;

const int Nmax = 1e5 + 5;


int n, q;

class SegTree
{
    int a[Nmax];

public:
    void read()
    {
        int i;
        for(i=0; i<n; ++i) cin >> a[i+n];
        for(i=n-1; i; --i) a[i] = max(a[i<<1], a[i<<1|1]);
    }

    void update(int pos, int val)
    {
        pos += n-1;
        a[pos] = val;
        
        for(pos>>=1; pos; pos>>=1)
            a[pos] = max(a[pos<<1], a[pos<<1|1]);
    }

    int query(int L, int R)
    {
        L += n-1; R += n;
        int ans = 0;

        for(; L < R; L>>=1, R>>=1)
        {
            if(L&1) ans = max(ans, a[L++]);
            if(R&1) ans = max(ans, a[--R]);
        }
        return ans;
    }
} aint;

int main()
{
    freopen("arbint.in", "r", stdin);
    freopen("arbint.out", "w", stdout);
    cin.sync_with_stdio(false); cin.tie(0);
    
    cin >> n >> q;
    aint.read();

    while(q--)
    {
        int tip, x, y;
        cin >> tip >> x >> y;

        if(tip == 0)
            cout << aint.query(x, y) << '\n';
        else aint.update(x, y);
    }

    return 0;
}