#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <chrono>
#include <random>
#include <unordered_map>
#include <unordered_set>
 
#define ll long long
#define ld long double
#define pl pair<ll, ll>
#define vi vector<ll>
#define vii vector<vi>
#define vc vector<char>
#define vcc vector<vc>
#define vp vector<pl>
#define mi map<ll,ll>
#define mc map<char,int>
#define sortx(X) sort(X.begin(),X.end());
#define all(X) X.begin(),X.end()
#define allr(X) X.rbegin(),X.rend()
#define ln '\n'
#define YES {cout << "YES\n"; return;}
#define NO {cout << "NO\n"; return;}
#define MUN {cout << "-1\n"; return;}
using namespace std;
/**
 * usage:-
 * creat tree element.
 * SegmentTree sg;
 * 
 * Functions you can use:
 * @set: set index or range to value.
 * @geteange: get value of given range.
 * @build: build tree with given vector or size.
 * 
 * make sure to look at item typedef.
 * you can change merge function to change it's oppration.
 * it you want to make change to segment work in checkLazy().
*/
typedef long long item;
/*
struct item
{
    int val;
    item(){
        val = 0;
    }
};
*/
class SegmentTree
{
public:
    ll get(int value) {
        return get(0, 0, size - 1, value);
    }
    void set(int l, int r, int value) {
        set(0, 0, size - 1, l, r, value);
    }
    item getrange(int l, int r) {
        return (getrange(0, 0, size - 1, l, r));
    }
    void build(int n) {
        size = 1;
        while (size < n)
            size *= 2;
        tree.assign(size * 2, item());
        lazy.assign(size * 2, 0);
    }
    void build(vector<item>& X) {
        size = 1;
        while (size < X.size())
            size *= 2;
        tree.assign(size * 2, item());
        lazy.assign(size * 2, 0);
        build(X, 0, 0, size - 1);
    }
private:
    int size;
    vector<item> tree;
    vector<long long> lazy;
    item merge(item a, item b) {
        item res = max(a, b);
        return (res);
    }
    void checkLazy(int m, int lx, int rx) {
        if (!lazy[m]) return;
        tree[m] += lazy[m];
        
        if (lx != rx) {
            lazy[2 * m + 1] += lazy[m];
            lazy[2 * m + 2] += lazy[m];
        }
        lazy[m] = 0;
    }
    ll get(int m, int lx, int rx, int val) {
        checkLazy(m, lx, rx);
        if (tree[m] < val) return -1;
        if (lx == rx) return lx;
        int mid = (lx + rx) / 2;
        item s1, s2;
        ll re = get(m*2+1, lx, mid,val);
        if (re != -1) return re;
        return get(m*2+2, mid+1, rx, val);
    }
    void set(int m, int lx, int rx, int l, int r, int val) {
        checkLazy(m, lx, rx);
        if (rx < l || r < lx) return;
        if (l <= lx && rx <= r)
        {
            lazy[m] = val;
            checkLazy(m, lx, rx);
            return;
        }
        int mid = (lx + rx) / 2;
        item s1, s2;
        set(m * 2 + 1, lx, mid, l, r, val);
        set(m * 2 + 2, mid + 1, rx, l, r, val);
        s1 = tree[m * 2 + 1], s2 = tree[m * 2 + 2];
        tree[m] = merge(s1, s2);
    }
    item getrange(int m, int lx, int rx, int l, int r) {
        checkLazy(m, lx, rx);
        if (rx < l || r < lx) return (0);
        if (l <= lx && rx <= r) return (tree[m]);
        int mid = (lx + rx) / 2;
        item s1, s2;
        s1 = getrange(m * 2 + 1, lx, mid, l, r);
        s2 = getrange(m * 2 + 2, mid + 1, rx, l, r);
        return merge(s1, s2);
    }
    void build(vector<item>& X, int m, int lx, int rx) {
        if (lx == rx) {
            if (lx < X.size()) tree[m] = X[lx];
            return;
        }
        int mid = (lx + rx) / 2;
        item s1, s2;
        build(X, m * 2 + 1, lx, mid);
        build(X, m * 2 + 2, mid + 1, rx);
        s1 = tree[m * 2 + 1], s2 = tree[m * 2 + 2];
        tree[m] = merge(s1, s2);
    }
};
void solve(int tc) {
    ll n, q;
    cin >> n >> q;
    vi X(n);
    for (int i = 0; i < n; i++)
    {
        cin >> X[i];
    }
    sortx(X);
    X.push_back(1e12);
    SegmentTree sg;
    sg.build(X);
    while (q--)
    {
        char ty; cin >> ty;
        if (ty == 'F') {
            ll c, k; cin >> c >> k;
            ll re = sg.get(k);
            if (re + c >= n) {
                sg.set(re, n-1, 1);
                continue;
            }
            ll pc = re + c - 1;
            ll vl = sg.getrange(pc, pc);
            ll sc = sg.get(vl);
            ll ec = sg.get(vl + 1) - 1;
            
            sg.set(re, sc-1, 1);
            ll rem = c - sc + re;
            sg.set(ec-rem+1, ec, 1);
        } else {
            ll a, b; cin >> a >> b;
            ll l = sg.get(a);
            ll r = sg.get(b + 1);
            cout << r - l << '\n';
        }
    }
    
}
 
int main()
{
    ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    int size = 1;
    // freopen("boards.in", "r", stdin);
    // freopen("boards.out", "w", stdout);
    // cin >> size;
    for (int i = 1; i <= size; i++)
        solve(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... | 
| # | 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... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |