Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define mp make_pair
- #define mt make_tuple
- #define pb push_back
- #define ll long long int
- using namespace std;
- const int N = 100009;
- struct node{
- int max1, max2, summ;
- node(){
- max1 = INT_MIN;
- max2 = INT_MIN;
- summ = INT_MIN;
- }
- };
- node tree[4*N];
- int a[N];
- node combine(const node nd1, const node nd2){
- node temp;
- int arr[] = {nd1.max1, nd1.max2, nd2.max1, nd2.max2};
- sort(arr, arr+4);
- temp.max1 = arr[3];
- temp.max2 = arr[2];
- temp.summ = temp.max1 + temp.max2;
- return temp;
- }
- void build(int ni, int ns, int ne){
- node &val = tree[ni];
- if(ns == ne){
- val.max1 = a[ns];
- val.summ = 0;
- return;
- }
- int lf = 2*ni +1, rt = lf+1, mid = ns + ((ne - ns)/2);
- build(lf, ns, mid);
- build(rt, mid+1, ne);
- val = combine(tree[rt], tree[lf]);
- }
- node query(int ql, int qr, int ni, int ns, int ne){
- if(ql > qr)
- return node();
- if(ql == ns and qr == ne)
- return tree[ni];
- int lf = 2*ni +1, rt = lf + 1, mid = ns + ((ne-ns)/2);
- return combine( query(ql, min(qr, mid), lf, ns, mid),
- query(max(ql, mid+1), qr, rt, mid+1, ne));
- }
- void update(int idx, int v, int ni, int ns, int ne){
- node & val = tree[ni];
- if(ns == ne){
- val.max1 = v;
- return;
- }
- int lf = 2*ni + 1, rt = lf+1, mid = ns + ((ne-ns)/2);
- if(idx <= mid)
- update(idx, v, lf, ns, mid);
- else
- update(idx, v, rt, mid+1, ne);
- val = combine(tree[lf], tree[rt]);
- }
- int main(){
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- int n, q, xi, yi;
- char op;
- cin >> n;
- for(int i = 0; i < n; i++)
- cin >> a[i];
- build(0, 0, n-1);
- cin >> q;
- while(q--){
- cin >> op >> xi >> yi;
- if(op == 'Q')
- cout << (query(xi-1, yi-1, 0, 0, n-1)).summ << endl;
- else
- update(xi-1, yi, 0, 0, n-1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement