#pragma GCC optimize("Ofast")
#include "bits/stdc++.h"
#define int long long
using namespace std;
#undef DEBUG
#ifdef DEBUG
#include "algo/debug"
#else
#define dbg(...)
#endif
const int N = 2e5 + 5;
int t[4 * N];
void upd(int v, int l, int r, int i, int x) {
if (l == r) {
t[v] = x;
return;
}
int m = (l + r) / 2;
if (i <= m) {
upd(v * 2, l, m, i, x);
} else {
upd(v * 2 + 1, m + 1, r, i, x);
}
t[v] = min(t[v * 2], t[v * 2 + 1]);
}
int ans;
void find(int v, int l, int r, int y) {
if (t[v] > y) {
return;
}
if (l == r) {
dbg(l);
ans = min(ans, l);
dbg(ans);
return;
}
int m = (l + r) / 2;
if (t[v * 2] <= y) {
find(v * 2, l, m, y);
return;
}
if (t[v * 2 + 1] <= y) {
find(v * 2 + 1, m + 1, r, y);
}
}
void ask(int v, int l, int r, int ql, int qr, int y) {
if (ql <= l and r <= qr) {
dbg(ql, qr, y, l, r);
find(v, l, r, y);
return;
}
int m = (l + r) / 2;
if (ql <= m) {
ask(v * 2, l, m, ql, qr, y);
}
if (m < qr) {
ask(v * 2 + 1, m + 1, r, ql, qr, y);
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
fill(t, t + 4 * N, 1e9);
while (q--) {
char c;
cin >> c;
if (c == 'M') {
int x, a;
cin >> x >> a;
upd(1, 1, n, a, x);
} else {
int b, y;
cin >> y >> b;
ans = 1e9;
ask(1, 1, n, b, n, y);
cout << (ans == 1e9 ? -1 : ans) << '\n';
}
}
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |