practice2_j - Segment Tree

AC-Library test

Time complexity: $O(q \log{n})$

Memory complexity: $O(n)$

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
using S = int;
S op(S a, S b) { return max(a, b); }
S e() { return -1; }
bool f(S target, S v) { return v < target; }
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    int n, q;
    cin >> n >> q;
    vi a(n);
    for (auto &ai : a)
        cin >> ai;
    using RangeMax = atcoder::segtree<S, op, e>;
    using placeholders::_1;
    RangeMax st(a);
    while (q--)
    {
        int type;
        cin >> type;
        if (type == 1)
        {
            int x, v;
            cin >> x >> v, x--;
            st.set(x, v);
        }
        else if (type == 2)
        {
            int l, r;
            cin >> l >> r, l--;
            auto ans = st.prod(l, r);
            cout << ans << endl;
        }
        else
        {
            int x, v;
            cin >> x >> v, x--;
            auto ans = st.max_right(x, bind(f, v, _1));
            cout << ans + 1 << endl;
        }
    }
    return 0;
}