#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
using namespace std;
const int N = 2e5+1;
int n, a, d[N];
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
//using namespace __gnu_pbds; 
template<typename T>
using ordered_set = __gnu_pbds::tree<T,__gnu_pbds::null_type,less<T>,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>;
#define seg pii
seg tree[N];
seg INF = {1e8, 1e8};
void update(int idx, seg val, int l=1, int r=n, int node=1) {
	if (l == r) {
		tree[node] = val;
		return;
	}
	const int mid = (l+r)/2;
	if (idx <= mid)
		update(idx, val, l, mid, node*2);
	else
		update(idx, val, mid+1, r, node*2+1);
	tree[node] = min(tree[node*2], tree[node*2+1]);
}
seg query(int ql, int qr, int l=1, int r=n, int node=1) {
	if (ql <= l && r <= qr) return tree[node];
	if (l > qr || r < ql) return INF;
	const int mid = (l+r)/2;
	return min(query(ql, qr, l, mid, node*2), query(ql, qr, mid+1, r, node*2+1));
}
seg cc[N];
int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin >> n >> a;
	for (int i = 1; i <= n; cin >> d[i++]);
	ordered_set<pair<int, int>> oleft, oright;
	for (int i = 1; i < a; i++) {
		cc[i] = {n-d[i]+1, 0};
		oleft.insert(cc[i]);
		update(i, cc[i]);	
	}
	for (int i = a+1; i <= n; i++) {
		cc[i] = {n-d[i]+1, 0};
		oright.insert(cc[i]);
		update(i, cc[i]);
	}
	int q;
	cin >> q;
	int ttt = 0;
	while (q--) {
		ttt++;
		char t;
		int i, e;
		cin >> t >> i;
		if (t == 'F') {
			if (i == a) cout << "0\n";
			if (i < a) {
				auto suff_max = query(i, a-1);
				cout << (a-i) + oright.size()-oright.order_of_key(suff_max) << '\n';
			}
			if (i > a) {
				auto pref_max = query(a+1, i);
				cout << (i-a) + oleft.size()-oleft.order_of_key(pref_max) << '\n';
			}
		} else {
			cin >> e;
			if (i < a) oleft.erase(cc[i]);
			if (i > a) oright.erase(cc[i]);
			cc[i] = {e, -ttt};
			update(i, cc[i]);
			if (i < a) oleft.insert(cc[i]);
			if (i > a) oright.insert(cc[i]);
		}
	}
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |