Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- _____ _ _ _ _
- |_ _| |__ ___ / \ _ __ ___| |__ _ _| |
- | | | '_ \ / _ \ / _ \ | '_ \/ __| '_ \| | | | |
- | | | | | | __// ___ \| | | \__ \ | | | |_| | |
- |_| |_| |_|\___/_/ \_\_| |_|___/_| |_|\__,_|_|
- */
- #include<bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- #define ll int
- #define pb push_back
- #define ppb pop_back
- #define endl '\n'
- #define mii map<ll,ll>
- #define msi map<string,ll>
- #define mis map<ll, string>
- #define rep(i,a,b) for(ll i=a;i<b;i++)
- #define repr(i,a,b) for(ll i=b-1;i>=a;i--)
- #define trav(a, x) for(auto& a : x)
- #define pii pair<ll,ll>
- #define vi vector<ll>
- #define vii vector<pair<ll, ll>>
- #define vs vector<string>
- #define all(a) (a).begin(),(a).end()
- #define F first
- #define S second
- #define sz(x) (ll)x.size()
- #define hell 1000000007
- #define lbnd lower_bound
- #define ubnd upper_bound
- #define max(a,b) (a>b?a:b)
- #define min(a,b) (a<b?a:b)
- /* For Debugging */
- #define DEBUG cerr<<"\n>>>I'm Here<<<\n"<<endl;
- #define display(x) trav(a,x) cout<<a<<" ";cout<<endl;
- #define what_iss(x) cerr << #x << " = " << x << endl;
- #define what_is(x) cerr << #x << " = " << x << " ";
- std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
- #define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
- #define TIME cerr << "\nTime elapsed: " << setprecision(5) <<1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
- #define DECIMAL(n) cout << fixed ; cout << setprecision(n);
- #define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
- using namespace __gnu_pbds;
- using namespace std;
- #define PI 3.141592653589793
- #define N 100005
- ll n,q;
- ll srt;
- vi v;
- class Block {
- public:
- vi tmp;
- Block(ll l,ll r) {
- rep(i,l,r+1) {
- tmp.pb(v[i]);
- }
- sort(all(tmp));
- }
- ll query(ll k) {
- return (ll)(ubnd(all(tmp),k) - tmp.begin());
- }
- };
- vector<Block> blocks;
- void preprocess() {
- srt = sqrt(n);
- for(ll i = 0; i < n; i+=srt) {
- blocks.pb(Block(i,min(n - 1,i + srt - 1)));
- }
- }
- ll manualCalculate(ll l,ll r,ll x) {
- ll cnt = 0;
- rep(i,l,r+1) {
- cnt += (v[i] <= x);
- }
- return cnt;
- }
- ll findOut(ll l,ll r,ll x) {
- if((l / srt) == (r / srt)) {
- return manualCalculate(l,r,x);
- }
- ll cnt = 0;
- ll L,R;
- L = (l / srt);
- R = (r / srt);
- if(l == L*srt) {
- cnt += blocks[L].query(x);
- } else {
- // what_is(l);
- // what_is(min(n - 1,L * srt + srt - 1));
- // what_iss(manualCalculate(l,min(n - 1,L * srt + srt - 1), x));
- cnt += manualCalculate(l,min(n - 1,L * srt + srt - 1), x);
- }
- if(r == min(n - 1,R * srt + srt - 1)) {
- // what_iss(r);
- // what_iss(blocks[R].query(x));
- cnt += blocks[R].query(x);
- } else {
- cnt += manualCalculate(R * srt, r, x);
- }
- rep(i,L+1,R) {
- cnt+=blocks[i].query(x);
- }
- return cnt;
- }
- void solve()
- {
- cin >> n >> q;
- v.resize(n);
- rep(i,0,n) {
- cin >> v[i];
- }
- preprocess();
- // what_iss(srt);
- char c;
- ll l,r,x;
- ll L,R;
- rep(i,0,q) {
- cin >> c;
- if(c == 'M') {
- cin >> l >> x;
- l --;
- v[l] = x;
- L = (l / srt) * srt;
- blocks[l / srt] = Block(L, min(n - 1, L + srt - 1));
- // what_is(l);
- // what_is(L);
- // what_iss(min(n - 1, L + srt - 1));
- } else {
- cin >> l >> r >> x;
- cout << findOut(l - 1,r - 1,x) << endl;
- }
- }
- return;
- }
- int main()
- {
- FAST
- int TESTS=1;
- // cin>>TESTS;
- rep(i,0,TESTS)
- {
- // cout<<"Case #"<<i+1<<": ";
- solve();
- }
- // TIME
- return 0;
- }
Add Comment
Please, Sign In to add comment