#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &pa) { is >> pa.first >> pa.second; return is; }
template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << "(" << pa.first << "," << pa.second << ")"; return os; }
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template <typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template <typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp) { os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
template <typename T> void resize_array(vector<T> &vec, int len) { vec.resize(len); }
template <typename T, typename... Args> void resize_array(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) resize_array(v, args...); }
template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); }
template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); }
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }
// 函数名后面加 ll 就可以计算 long long类型对应的结果
// __builtin_ffs(x) // 返回x中最后一个为1的位是从后向前的第几位
// __builtin_popcount(x) // x中1的个数
// __builtin_ctz(x) // x末尾0的个数。x=0时结果未定义。
// __builtin_clz(x) // x前导0的个数。x=0时结果未定义。
// __builtin_parity(x) // x中1的奇偶性。
#define highest_bit1_index(x) (31 - __builtin_clz(x))
#define highest_bit1_index_ll(x) (63 - __builtin_clzll(x))
#define rep(i, a, n) for (int i = a; i < (n); i++)
#define per(i, a, n) for (int i = (n)-1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
typedef vector<int> vi;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef double db;
#if DEBUG
#define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl;
#else
#define dbg(x)
#endif
template <typename T>
class SegmentTree {
public:
SegmentTree(int node_num, function<T(T, T)> f, T default_v = T())
: f_(f), default_value_(default_v) {
node_num_ = node_num;
offset_ = 1;
while (offset_ < node_num) offset_ *= 2;
nodes_.resize(offset_ * 2, default_v);
}
friend istream& operator>>(istream& in, SegmentTree<T>& st) {
for (int i = 0; i < st.node_num_; ++i) {
cin >> st.nodes_[i + st.offset_ - 1];
}
return in;
}
// Init 前需要先赋初始值给nodes_
// rep(i,0,n) {
// nodes_[i + offset_ - 1] = A[i];
// }
void Init() {
for (int k = offset_ - 2; k >= 0; --k) {
nodes_[k] = f_(nodes_[k * 2 + 1], nodes_[k * 2 + 2]);
}
}
// 将第k个值更新为a
void Update(int k, T a) {
// 叶子节点
k += offset_ - 1;
nodes_[k] = a;
// 向上更新
while (k > 0) {
k = (k - 1) / 2;
nodes_[k] = f_(nodes_[k * 2 + 1], nodes_[k * 2 + 2]);
}
}
T Query(int a, int b, int x) { return Query(a, b, 0, 0, offset_, x); }
private:
// 求[a,b)区间第一个值<=x的下标, f = min(a,b)
// 后面的参数是为了计算方便传入的
// k是节点的编号,l,r表示这个节点代表区间[l,r)
// 外部调用时,使用query(a,b,0,0,n);
int Query(int a, int b, int k, int l, int r, int x) {
// 如果[a,b)和[l,r)不相交,返回0
if (r <= a || b <= l) return -1;
// 如果[a,b)完全包含[l,r)
if (a <= l && r <= b) {
if (x < nodes_[k]) {
return -1;
}
if (l + 1 == r) {return l;}
int i = Query(a, b, k * 2 + 1, l, (l + r) / 2, x);
if (i != -1) return i;
return Query(a, b, k * 2 + 2, (l + r) / 2, r, x);
} else {
int v1 = Query(a, b, k * 2 + 1, l, (l + r) / 2, x);
if (v1 != -1) return v1;
return Query(a, b, k * 2 + 2, (l + r) / 2, r, x);
}
}
int offset_;
int node_num_;
vector<T> nodes_;
function<T(T, T)> f_;
T default_value_;
};
class Solution {
public:
void Solve() {
int n,m;
while(cin>>n>>m) {
SegmentTree<int> st(n, [](int a, int b) {
return min(a,b);
}, numeric_limits<int>::max());
string op;
int x,y;
rep(i,0,m) {
cin>>op>>x>>y;
if (op == "M") {
st.Update(y-1, x);
} else {
int idx = st.Query(y-1, n, x);
if (idx >=0) idx++;
cout << idx << '\n';
}
}
}
cout.flush();
}
private:
};
// # define FILE_IO 1
void set_io(const string &name = "") {
ios::sync_with_stdio(false);
cin.tie(nullptr);
#if FILE_IO
if (!name.empty()) {
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
#endif
}
int main() {
set_io("tmp");
Solution().Solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
2 ms |
336 KB |
Output is correct |
4 |
Correct |
96 ms |
6984 KB |
Output is correct |
5 |
Correct |
86 ms |
5448 KB |
Output is correct |
6 |
Correct |
93 ms |
6728 KB |
Output is correct |
7 |
Correct |
90 ms |
6912 KB |
Output is correct |