Link to this code: https://cses.fi/paste/4bd65da956041662b55493/
#include<bits/stdc++.h>
using namespace std;
 
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T> using oset =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define int long long 

const int infinity = 1e15;
const int N = 4 * (1e5 +1); 
 
int arr[N];
 
void solve(){
    int n, q, a, b;
    char ch;
    cin >> n >> q;
    oset<pair<int,int>> s;
    for(int i=1;i<=n;i++) {
        cin >> arr[i];
        s.insert({arr[i], i});
    }
    while(q--) {
        cin >> ch >> a >> b;
        if(ch == '?') {
            int n1 = s.order_of_key({a-1, infinity});
            int n2 = s.order_of_key({b, infinity});
            cout << n2-n1 << "\n";
        } else {
            s.erase({arr[a], a});
            arr[a] = b; 
            s.insert({arr[a], a});
        }
    }
}
#undef int
 
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int t=1;
    //cin >> t;
    while(t--){
        solve();
    }
    return 0;
}