Cod sursa(job #2657029)

Utilizator redstonegamer22Andrei Ion redstonegamer22 Data 9 octombrie 2020 15:41:17
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.08 kb
#include <bits/stdc++.h>

using namespace std;

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

class SegmTree {
    vector<int> T;
    int n;

public:

    SegmTree(int n) : T(4 * n), n(n) {}

    void Update(int pos, int val) {
        for (T[pos += n] = val; pos > 1; pos /= 2)
            T[pos / 2] = max(T[pos], T[pos ^ 1]);
    }

    int Query(int b, int e) {
        int res = 0;
        for (b += n, e += n; b < e; b /= 2, e /= 2) {
            if (b % 2) res = max(res, T[b++]);
            if (e % 2) res = max(res, T[--e]);
        }
        return res;
    }
};


int main()
{
    int n, m; in >> n >> m;
    SegmTree aint(n);

    for(int i = 0; i < n; i++)
    {
        int rd; in >> rd;
        aint.Update(i, rd);
    }

    for(int i = 0; i < m; i++)
    {
        int op; in >> op;
        if(op == 0)
        {
            int a, b; in >> a >> b;
            out << aint.Query(a-1, b) << "\n";
        }
        else
        {
            int a, b; in >> a >> b;
            aint.Update(a-1, b);

        }
    }
}