#include<bits/stdc++.h>
#define ll long long
#define fr(a,b) for(int i = a; i < b; i++)
using namespace std;
#define left(i) (2*i + 1)
#define right(i) (2*i + 2)
#define parent(i) ((i-1)/2)
template<class T>
class SegmentTree
{
public:
//tree constructors.
SegmentTree(std::vector<T> data, T value, T (*combine)(T obj1, T obj2));
SegmentTree(T ar[], int n, T value, T (*combine)(T obj1, T obj2));
//query the range l to r, 0 based array indexing.
T query(int l, int r);
//update the element at index idx to val.
void update(int idx, T val);
///TODO lazy propagation
private:
//represents the segment tree.
T *tree;
//builds the segment tree.
void buildTree(std::vector<T> data);
//size of the segment tree array.
int segTreeSize;
//extra nodes must be added to array to make its size a power of 2
//this is the value to be filled for the those nodes.
T valueForExtraNodes;
//specifies how to combine child node results to form parent node result.
T (*combine)(T obj1, T obj2);
//used to calculate the size of array needed to store the tree.
int calculateSize(int n);
//helps to solve a range query.
T queryHelper(int l,int r, int st, int ed, int node);
};
template<class T> SegmentTree<T>::SegmentTree(std::vector<T> data,
T value, T (*combine)(T obj1, T obj2))
{
this->combine = combine;
valueForExtraNodes = value;
segTreeSize = calculateSize(data.size());
buildTree(data);
}
template<class T> SegmentTree<T>::SegmentTree(T ar[], int n,
T value, T (*combine)(T obj1, T obj2))
{
this->combine = combine;
valueForExtraNodes = value;
segTreeSize = calculateSize(n);
std::vector<T> data;
for(int i = 0; i < n; i++)
data.push_back(ar[i]);
buildTree(data);
}
template<class T> int SegmentTree<T>::calculateSize(int n)
{
int pow2 = 1;
while( pow2 < n)
{
pow2 = pow2 << 1;
}
return 2*pow2 - 1;
}
template<class T> T SegmentTree<T>::query(int l, int r)
{
int st = 0, ed = segTreeSize/2;
return queryHelper(l, r, st, ed, 0);
}
template<class T> T SegmentTree<T>::queryHelper(int l,int r, int st, int ed, int node)
{
if( (r < st) || (l > ed) || (l > r) )
return valueForExtraNodes;
if(st >= l && ed <= r)
return tree[node];
T leftVal = queryHelper(l, r, st, (st + ed)/2, left(node));
T rightVal = queryHelper(l, r, (st+ed)/2 + 1, ed, right(node));
return combine(leftVal, rightVal);
}
template<class T> void SegmentTree<T>::buildTree(std::vector<T> data)
{
int n = data.size();
tree = new T[segTreeSize];
int extraNodes = (segTreeSize/2 + 1) - n;
for(int i = segTreeSize - 1; i >= 0; i--)
{
if(extraNodes>0)
{
tree[i] = valueForExtraNodes;
extraNodes--;
}
else if(n>0)
{
tree[i] = data[n-1];
n--;
}
else
tree[i] = combine(tree[left(i)], tree[right(i)]);
}
}
template<class T> void SegmentTree<T>::update(int idx, T val)
{
int segTreeIdx = (segTreeSize/2) + idx;
tree[segTreeIdx] = val;
while(parent(segTreeIdx) >= 0)
{
segTreeIdx = parent(segTreeIdx);
if(right(segTreeIdx) < segTreeSize)
tree[segTreeIdx] = combine(tree[left(segTreeIdx)], tree[right(segTreeIdx)]);
if(segTreeIdx == 0)
break;
}
}
struct node
{
node(int x, int y):m1(x),m2(y){}
node(){}
int m1, m2;
};
node combine(node x, node y)
{
node temp;
vector<int> s = {x.m1,x.m2,y.m1,y.m2};
sort(s.begin(),s.end(),greater<int>());
temp.m1 = s[0];
temp.m2 = s[1];
return temp;
}
int main(){
int n,i,j,q;
cin>>n;
vector<node> ar(n);
for(int i = 0; i < n; i++)
{
cin>>ar[i].m1;
ar[i].m2 = INT_MIN;
}
node smallest(INT_MIN, INT_MIN);
SegmentTree<node> sg(ar, smallest, combine);
cin>>q;
while(q--)
{
char ch;
int x,y;
cin>>ch>>x>>y;
if(ch == 'Q')
{
node ans = sg.query(x-1,y-1);
cout<<ans.m1+ans.m2<<'\n';
}
else
{
node upd(y, INT_MIN);
sg.update(x-1, upd);
}
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBsbCBsb25nIGxvbmcKI2RlZmluZSBmcihhLGIpIGZvcihpbnQgaSA9IGE7IGkgPCBiOyBpKyspCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojZGVmaW5lIGxlZnQoaSkgKDIqaSArIDEpCiNkZWZpbmUg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ZXJ5KHgtMSx5LTEpOwogICAgICAgICAgICBjb3V0PDxhbnMubTErYW5zLm0yPDwnXG4nOwogICAgICAgIH0KICAgICAgICBlbHNlCiAgICAgICAgewogICAgICAgICAgICBub2RlIHVwZCh5LCBJTlRfTUlOKTsKICAgICAgICAgICAgc2cudXBkYXRlKHgtMSwgdXBkKTsKICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gMDsKfQo=