sacgajcvs

Untitled

Oct 13th, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. /*
  2. _____ _ _ _ _
  3. |_ _| |__ ___ / \ _ __ ___| |__ _ _| |
  4. | | | '_ \ / _ \ / _ \ | '_ \/ __| '_ \| | | | |
  5. | | | | | | __// ___ \| | | \__ \ | | | |_| | |
  6. |_| |_| |_|\___/_/ \_\_| |_|___/_| |_|\__,_|_|
  7.  
  8. */
  9. #include<bits/stdc++.h>
  10. #include <ext/pb_ds/assoc_container.hpp>
  11. #include <ext/pb_ds/tree_policy.hpp>
  12. #define ll int
  13. #define pb push_back
  14. #define ppb pop_back
  15. #define endl '\n'
  16. #define mii map<ll,ll>
  17. #define msi map<string,ll>
  18. #define mis map<ll, string>
  19. #define rep(i,a,b) for(ll i=a;i<b;i++)
  20. #define repr(i,a,b) for(ll i=b-1;i>=a;i--)
  21. #define trav(a, x) for(auto& a : x)
  22. #define pii pair<ll,ll>
  23. #define vi vector<ll>
  24. #define vii vector<pair<ll, ll>>
  25. #define vs vector<string>
  26. #define all(a) (a).begin(),(a).end()
  27. #define F first
  28. #define S second
  29. #define sz(x) (ll)x.size()
  30. #define hell 1000000007
  31. #define lbnd lower_bound
  32. #define ubnd upper_bound
  33. #define max(a,b) (a>b?a:b)
  34. #define min(a,b) (a<b?a:b)
  35.  
  36. /* For Debugging */
  37. #define DEBUG cerr<<"\n>>>I'm Here<<<\n"<<endl;
  38. #define display(x) trav(a,x) cout<<a<<" ";cout<<endl;
  39. #define what_iss(x) cerr << #x << " = " << x << endl;
  40. #define what_is(x) cerr << #x << " = " << x << " ";
  41.  
  42. std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
  43. #define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
  44. #define TIME cerr << "\nTime elapsed: " << setprecision(5) <<1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
  45. #define DECIMAL(n) cout << fixed ; cout << setprecision(n);
  46. #define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  47. using namespace __gnu_pbds;
  48. using namespace std;
  49. #define PI 3.141592653589793
  50. #define N 100005
  51. ll n,q;
  52. ll srt;
  53. vi v;
  54.  
  55. class Block {
  56. public:
  57. vi tmp;
  58. Block(ll l,ll r) {
  59. rep(i,l,r+1) {
  60. tmp.pb(v[i]);
  61. }
  62. sort(all(tmp));
  63. }
  64.  
  65. ll query(ll k) {
  66. return (ll)(ubnd(all(tmp),k) - tmp.begin());
  67. }
  68. };
  69.  
  70. vector<Block> blocks;
  71.  
  72.  
  73. void preprocess() {
  74. srt = sqrt(n);
  75. for(ll i = 0; i < n; i+=srt) {
  76. blocks.pb(Block(i,min(n - 1,i + srt - 1)));
  77. }
  78. }
  79.  
  80.  
  81. ll manualCalculate(ll l,ll r,ll x) {
  82. ll cnt = 0;
  83. rep(i,l,r+1) {
  84. cnt += (v[i] <= x);
  85. }
  86. return cnt;
  87. }
  88.  
  89. ll findOut(ll l,ll r,ll x) {
  90. if((l / srt) == (r / srt)) {
  91. return manualCalculate(l,r,x);
  92. }
  93.  
  94. ll cnt = 0;
  95. ll L,R;
  96. L = (l / srt);
  97. R = (r / srt);
  98. if(l == L*srt) {
  99. cnt += blocks[L].query(x);
  100. } else {
  101. // what_is(l);
  102. // what_is(min(n - 1,L * srt + srt - 1));
  103. // what_iss(manualCalculate(l,min(n - 1,L * srt + srt - 1), x));
  104. cnt += manualCalculate(l,min(n - 1,L * srt + srt - 1), x);
  105. }
  106. if(r == min(n - 1,R * srt + srt - 1)) {
  107. // what_iss(r);
  108. // what_iss(blocks[R].query(x));
  109. cnt += blocks[R].query(x);
  110. } else {
  111. cnt += manualCalculate(R * srt, r, x);
  112. }
  113.  
  114. rep(i,L+1,R) {
  115. cnt+=blocks[i].query(x);
  116. }
  117. return cnt;
  118. }
  119.  
  120. void solve()
  121. {
  122. cin >> n >> q;
  123. v.resize(n);
  124. rep(i,0,n) {
  125. cin >> v[i];
  126. }
  127. preprocess();
  128. // what_iss(srt);
  129. char c;
  130. ll l,r,x;
  131. ll L,R;
  132. rep(i,0,q) {
  133. cin >> c;
  134. if(c == 'M') {
  135. cin >> l >> x;
  136. l --;
  137. v[l] = x;
  138. L = (l / srt) * srt;
  139. blocks[l / srt] = Block(L, min(n - 1, L + srt - 1));
  140. // what_is(l);
  141. // what_is(L);
  142. // what_iss(min(n - 1, L + srt - 1));
  143. } else {
  144. cin >> l >> r >> x;
  145. cout << findOut(l - 1,r - 1,x) << endl;
  146. }
  147.  
  148. }
  149. return;
  150. }
  151. int main()
  152. {
  153. FAST
  154. int TESTS=1;
  155. // cin>>TESTS;
  156. rep(i,0,TESTS)
  157. {
  158. // cout<<"Case #"<<i+1<<": ";
  159. solve();
  160. }
  161. // TIME
  162. return 0;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment