/*
By: facug91
From:
Name:
Date: 20/12/2022
Solution:
*/
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#define endline std::endl
#define LOG(x) std::cout << "#" << (#x) << ": " << (x) << std::endl
#else
#define endline "\n"
#define LOG(x)
#endif
#define y0 dbnw9uddu0132dnd03dnqwuidg1o
#define y1 nd03dnqwuidg1odbnw9uddu0132d
const double EPS = 1e-9;
const double PI = 2.0 * acos(0.0);
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
using vii = vector<ii>;
const int MAX_N = 2e5 + 5;
const int MAX_Q = 2e5 + 5;
const int MOD = 1e9 + 7;
const int invalid = 1e9 + 5;
/**
* \brief Defines a class for a Segment Tree.
* State: untested.
* Ref: Competitive Programming 3, section 2.4.3
* https://cp-algorithms.com/data_structures/segment_tree.html
*
* \tparam ValueType Type of the elements.
* \tparam MaxSize Maximum number of elements.
*/
class SegmentTreePointUpdateRangeQuery {
private:
int n;
int st[MAX_N * 4];
int combine(int a, int b) {
return min(a, b);
}
void build(int v, int tl, int tr, int val) {
if (tl == tr) st[v] = val;
else {
int tm = (tl + tr) / 2;
build(v * 2, tl, tm, val);
build(v * 2 + 1, tm + 1, tr, val);
st[v] = combine(st[v * 2], st[v * 2 + 1]);
}
}
ii query(int v, int tl, int tr, int l, int r, int val) {
if (l > r || st[v] > val) return make_pair(invalid, -2);
if (tl == tr) return make_pair(st[v], tl);
int tm = (tl + tr) / 2;
ii left = query(v * 2, tl, tm, l, std::min(r, tm), val);
if (left.first != invalid) return left;
return query(v * 2 + 1, tm + 1, tr, std::max(l, tm + 1), r, val);
}
void update(int v, int tl, int tr, int pos, int val) {
if (tl == tr) st[v] = val;
else {
int tm = (tl + tr) / 2;
if (pos <= tm) update(v * 2, tl, tm, pos, val);
else update(v * 2 + 1, tm + 1, tr, pos, val);
st[v] = combine(st[v * 2], st[v * 2 + 1]);
}
}
public:
void build(int size, int val) {
n = size;
build(1, 0, size - 1, val);
}
ii query(int l, int r, int val) {
return query(1, 0, n - 1, l, r, val);
}
void update(int idx, int val) {
update(1, 0, n - 1, idx, val);
}
};
int n, q, x, a, y, b;
string type;
SegmentTreePointUpdateRangeQuery st;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n >> q;
st.build(n, invalid);
while (q--) {
cin >> type;
if (type[0] == 'M') {
cin >> x >> a;
st.update(a - 1, x);
} else if (type[0] == 'D') {
cin >> y >> b;
cout << st.query(b - 1, n - 1, y).second + 1 << endline;
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
88 ms |
6864 KB |
Output is correct |
5 |
Correct |
98 ms |
5332 KB |
Output is correct |
6 |
Correct |
94 ms |
6592 KB |
Output is correct |
7 |
Correct |
101 ms |
6820 KB |
Output is correct |