Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <set>
- #include <map>
- #include <cstring>
- #include <algorithm>
- #include <stack>
- #include <queue>
- using namespace std;
- const int maxn = 1e5 + 10;
- const int INF = 2e9;
- int arr[maxn];
- pair<int, int> segment_tree[3 * maxn];
- void merge_nodes(pair<int, int> & tree_node, pair<int, int> & L, pair<int, int> & R) {
- vector<int> v;
- v.push_back(L.first);
- v.push_back(L.second);
- v.push_back(R.first);
- v.push_back(R.second);
- sort(v.begin(), v.end());
- tree_node = make_pair(v[2], v[3]);
- }
- pair<int, int> merge_query(pair<int, int> L, pair<int, int> R) {
- vector<int> v;
- v.push_back(L.first);
- v.push_back(L.second);
- v.push_back(R.first);
- v.push_back(R.second);
- sort(v.begin(), v.end());
- return make_pair(v[2], v[3]);
- }
- void build_tree(int L, int R, int position) {
- if(L == R) {
- segment_tree[position] = make_pair(arr[L], -INF);
- }
- else {
- int middle = (L + R) / 2;
- build_tree(L, middle, 2 * position);
- build_tree(middle + 1, R, 2 * position + 1);
- merge_nodes(segment_tree[position], segment_tree[2 * position], segment_tree[2 * position + 1]);
- }
- }
- // L R i L R j L R
- pair<int, int> query(int L, int R, int position, int i, int j) {
- if(i <= L and R <= j) {
- return segment_tree[position];
- }
- if(R < i or j < L) {
- return make_pair(-INF, -INF);
- }
- int middle = (L + R) / 2;
- return merge_query(query(L, middle, 2 * position, i, j), query(middle + 1, R, 2 * position + 1, i, j));
- }
- void update(int L, int R, int position, int idx, pair<int, int> new_value) {
- if(L == R) {
- segment_tree[position] = new_value;
- return;
- }
- int middle = (L + R) / 2;
- if(idx <= middle) {
- update(L, middle, 2 * position, idx, new_value);
- }
- else {
- update(middle + 1, R, 2 * position + 1, idx, new_value);
- }
- merge_nodes(segment_tree[position], segment_tree[2 * position], segment_tree[2 * position + 1]);
- }
- int main() {
- ios_base::sync_with_stdio(false);
- int n;
- cin >> n;
- for(int i = 0; i < n; i++) {
- cin >> arr[i];
- }
- build_tree(0, n - 1, 1);
- int q;
- cin >> q;
- for(int i = 0; i < q; i++) {
- char c;
- cin >> c;
- if(c == 'U') {
- int idx, new_value;
- cin >> idx >> new_value;
- idx--;
- update(0, n - 1, 1, idx, make_pair(new_value, -INF));
- }
- else {
- int a, b;
- cin >> a >> b;
- a--; b--;
- pair<int, int> p = query(0, n - 1, 1, a, b);
- cout << p.first + p.second << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement