Submission #1342828

#TimeUsernameProblemLanguageResultExecution timeMemory
1342828SzilDeda (COCI17_deda)C++20
140 / 140
382 ms3084 KiB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int INF = int(1e9)+1;

struct segtree {
    vector<int> tree;
    int n;

    segtree(int n) : n(n) {
        tree.resize(2*n, INF);
    }

    void upd(int u, int k) {
        tree[u += n] = k;
        for (u /= 2; u >= 1; u /= 2) {
            tree[u] = min(tree[2*u], tree[2*u+1]);
        }
    }

    int qry(int l, int r) {
        int res = INF;
        l += n; r += n;
        while (l <= r) {
            if (l % 2 == 1) res = min(res, tree[l++]);
            if (r % 2 == 0) res = min(res, tree[r--]);
            l /= 2; r /= 2;
        }
        return res;
    }
};

void solve() {
    int n, q; cin >> n >> q;
    segtree st(n+1);
    for (int i = 1; i <= q; i++) {
        char c; cin >> c;
        int a, b; cin >> a >> b;
        if (c == 'M') {
            st.upd(b, a);
        } else {
            int lo = b, hi = n;
            while (lo < hi) {
                int mid = (lo + hi) / 2;
                if (st.qry(b, mid) <= a) hi = mid;
                else lo = mid+1;
            }
            cout << (st.qry(b, lo) > a ? -1 : lo) << "\n";
        }
    }
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int t = 1; 
    // cin >> t;
    while (t--) solve();
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...