Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "bits/stdc++.h"
- using namespace std;
- #define ll long long int
- #define rep(i,a,n) for(int i = a; i < n; i++)
- #define A(v) (v.begin(), v.end())
- #define S(x) scanf("%I64d", &x)
- #define s(x) scanf("%d", &x)
- #define pb(x) push_back(x)
- #define V vector < int >
- const int N = 4e5+5;
- vector < int > ans1, ans2;
- bool al, ar;
- struct st{
- int x, y;
- }tree[N];
- int arr[N];
- void build(int l, int r, int node){
- if(l == r){
- tree[node].x = arr[l];
- tree[node].y = 0;
- return;
- }
- int mid = (l+r) >> 1, lc = node + node;
- build(l, mid, lc);
- build(mid + 1, r, lc | 1);
- vector < int > a;
- a.pb(tree[lc].x),a.pb(tree[lc].y),a.pb(tree[lc|1].x),a.pb(tree[lc|1].y);
- sort(a.begin(), a.end());
- tree[node].x = a.back(); tree[node].y = a[2];
- }
- void query(int l, int r, int ql, int qr, int node, bool a){
- if(qr < l or ql > r)return ;
- /* if(l == r and (l == ql || l == qr)){
- if(l == ql)al = true;
- else ar = true;
- return 0;//return tree[node].x;
- }
- // if(l == r){ans.pb(arr[l]);return 0;}
- // if(a and ql <= l and r <= qr)return tree[node].y;
- // if(!a and ql <= l and r <= qr)return tree[node].x;*/
- if(a and ql <= l and r <= qr){
- ans1.pb(tree[node].y);
- ans1.pb(tree[node].x);
- return;
- }
- int mid = (l+r) >> 1, lc = node + node;
- query(l, mid, ql, qr, lc, a), query(mid + 1, r, ql, qr, lc|1, a);
- }
- void upd(int l, int r, int pos, int val, int node){
- if(pos < l or pos > r)return;
- if(l == r){
- tree[node].x = val;
- tree[node].y = 0;
- arr[l] = val;
- return ;
- }
- // if(ql <= l and )
- int mid = (l+r) >> 1, lc = node + node;
- upd(l, mid, pos, val, lc);
- upd(mid + 1, r, pos, val, lc|1);
- vector < int > a;
- a.pb(tree[lc].x),a.pb(tree[lc].y),a.pb(tree[lc|1].x),a.pb(tree[lc|1].y);
- sort(a.begin(), a.end());
- tree[node].x = a.back(); tree[node].y = a[2];
- }
- int main(){
- ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
- int n, i, j, k, l, r, q, x, y;char ch;
- cin >> n;
- for(i = 1; i <= n; i++)cin >> arr[i];
- build(1, n, 1);
- //cout << query(1, n, 1, 4, 1, 0) << ' ' << query(1, n, 1, 4, 1, 1) << '\n';
- cin >> q;
- while(q--){
- ans1.clear();al = 0, ar = 0;//ans2.clear();
- //scanf("%c %d %d", ch, &l, &r);
- cin >> ch >> l >> r;
- //cout << l << ' ' << r << '\n';
- // cout << ch << '\n';
- if(ch == 'U'){upd(1, n, l, r, 1);continue;}
- // cout << query(1, n, l, r, 1, 0) << ' ' << query(1, n, l, r, 1, 1) << '\n';
- // if(r <= n){
- //(query(1, n, l, r, 1, 0)) ;
- (query(1, n, l, r, 1,1));
- sort(ans1.begin(),ans1.end());
- cout << ans1.back() + ans1[ans1.size()-2] << '\n';
- //printf("%d\n", x + y);
- // }
- }
- // rep(i,1,4*n)if(tree[i].x)cout << i << ' ' << tree[i].x << ' ' << tree[i].y << '\n';
- return 0;
- }
Add Comment
Please, Sign In to add comment