sacgajcvs

Untitled

Oct 14th, 2020
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 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 long long
  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;
  52. vi v;
  53.  
  54. struct FenwickTree {
  55. vector<ll> bit; // binary indexed tree
  56. ll n;
  57. map<ll,ll> ma;
  58.  
  59. FenwickTree() {
  60. // this->n = n;
  61. // bit.assign(n, 0);
  62. }
  63.  
  64. void addNumber(ll x) {
  65. ma[x] = 1;
  66. }
  67.  
  68. void process() {
  69. ll cnt = 0;
  70. trav(i,ma) {
  71. i.S = cnt;
  72. cnt++;
  73. }
  74. this->n = sz(ma);
  75. bit.assign(this->n,0);
  76. }
  77.  
  78. // FenwickTree(vector<ll> a) : FenwickTree(a.size()) {
  79. // for (size_t i = 0; i < a.size(); i++)
  80. // add(i, a[i]);
  81. // }
  82.  
  83. ll sum(ll r) {
  84. r = ma[r];
  85. ll ret = 0;
  86. for (; r >= 0; r = (r & (r + 1)) - 1)
  87. ret += bit[r];
  88. return ret;
  89. }
  90.  
  91. ll sum(ll l, ll r) {
  92. return sum(r) - sum(l - 1);
  93. }
  94.  
  95. void add(ll idx, ll delta) {
  96. idx = ma[idx];
  97. for (; idx < n; idx = idx | (idx + 1))
  98. bit[idx] += delta;
  99. }
  100. };
  101. /*
  102. 0 - 0 -> 3
  103. 1 - 1 -> 4 10
  104. 0 - 1 -> 3 4 9 10
  105. 2 - 2 -> 1 9
  106. 3 - 3 -> 1 7
  107. 2 - 3 -> 1 4 7 9
  108. 0 - 3 -> 1 3 4 5 7 9 10
  109. */
  110.  
  111. vector<FenwickTree> seg(4*N);
  112. void build(ll cur,ll st,ll end)
  113. {
  114. if(st==end)
  115. {
  116. seg[cur].process();
  117. // cerr << st << " - " << end << " -> ";
  118. // trav(i,seg[cur].ma) {
  119. // cerr << i.S << " ";
  120. // }
  121. // cerr << endl;
  122. return;
  123. }
  124. ll mid=(st+end)>>1;
  125. build(2*cur,st,mid);
  126. build(2*cur+1,mid+1,end);
  127. seg[cur].process();
  128. // cerr << st << " - " << end << " -> ";
  129. // trav(i,seg[cur].ma) {
  130. // cerr << i.S << " ";
  131. // }
  132. // cerr << endl;
  133. }
  134. void build(ll cur,ll st,ll end,ll pos,ll upd)
  135. {
  136. if(st==end)
  137. {
  138. seg[cur].addNumber(upd);
  139. return;
  140. }
  141. ll mid = (st+end) >> 1;
  142. if(st<=pos&&pos<=mid)
  143. build(2*cur,st,mid,pos,upd);
  144. else
  145. build(2*cur+1,mid+1,end,pos,upd);
  146. seg[cur].addNumber(upd);
  147. }
  148. void build(ll cur,ll st,ll end,ll l,ll r,ll k)
  149. {
  150. if(l<=st&&r>=end)
  151. return seg[cur].addNumber(k);
  152. if(r<st||l>end)
  153. return; /* 2-change here */
  154. ll mid=(st+end)>>1;
  155. build(2*cur,st,mid,l,r,k);
  156. build(2*cur+1,mid+1,end,l,r,k);
  157. seg[cur].addNumber(k);
  158. // return (ans1+ans2); /* 3-change here */
  159. }
  160.  
  161. ll query(ll cur,ll st,ll end,ll l,ll r,ll k)
  162. {
  163. if(l<=st&&r>=end)
  164. return seg[cur].sum(k);
  165. if(r<st||l>end)
  166. return 0; /* 2-change here */
  167. ll mid=(st+end)>>1;
  168. ll ans1=query(2*cur,st,mid,l,r,k);
  169. ll ans2=query(2*cur+1,mid+1,end,l,r,k);
  170. return (ans1+ans2); /* 3-change here */
  171. }
  172. void update(ll cur,ll st,ll end,ll pos,ll upd,ll vl)
  173. {
  174. if(st==end)
  175. {
  176. seg[cur].add(upd,vl);
  177. return;
  178. }
  179. ll mid=(st+end)>>1;
  180. if(st<=pos&&pos<=mid)
  181. update(2*cur,st,mid,pos,upd,vl);
  182. else
  183. update(2*cur+1,mid+1,end,pos,upd,vl);
  184. seg[cur].add(upd,vl);
  185. }
  186.  
  187. void solve()
  188. {
  189. ll q;
  190. cin >> n >> q;
  191. v.resize(n);
  192. rep(i,0,n) {
  193. cin >> v[i];
  194. build(1, 0, n - 1, i, v[i]);
  195. }
  196. vector<pair<pii,pii>> data;
  197. char c;
  198. ll l,r,x;
  199. rep(i,0,q) {
  200. cin >> c;
  201. if(c == 'M') {
  202. cin >> l >> x;
  203. l--;
  204. build(1, 0, n - 1, l, x);
  205. data.pb({{1, l}, {x, -1}});
  206. } else {
  207. cin >> l >> r >> x;
  208. l--,r--;
  209. build(1, 0, n-1, l, r, x);
  210. data.pb({{2, l}, {r, x}});
  211. }
  212. }
  213. build(1,0,n-1);
  214. rep(i,0,n) {
  215. update(1, 0, n - 1, i, v[i], 1);
  216. }
  217. // rep(i, 0, n) {
  218. // cerr << query(1, 0, n - 1, 0, n - 1, v[i]) << " ";
  219. // }
  220. // cerr << endl;
  221. trav(i,data) {
  222. if(i.F.F == 1) {
  223. l = i.F.S, x = i.S.F;
  224. update(1, 0, n - 1, l, v[l], -1);
  225. v[l] = x;
  226. update(1, 0, n - 1, l, v[l], 1);
  227. // rep(i, 0, n) {
  228. // cerr << query(1, 0, n - 1, 0, n - 1, v[i]) << " ";
  229. // }
  230. // cerr << endl;
  231. } else {
  232. l = i.F.S, r = i.S.F, x = i.S.S;
  233. // what_is(l);
  234. // what_is(r);
  235. // what_is(x);
  236. cout << query(1, 0, n - 1, l, r, x) << endl;
  237. }
  238. }
  239. return;
  240. }
  241. int main()
  242. {
  243. FAST
  244. int TESTS=1;
  245. // cin>>TESTS;
  246. rep(i,0,TESTS)
  247. {
  248. // cout<<"Case #"<<i+1<<": ";
  249. solve();
  250. }
  251. // TIME
  252. return 0;
  253. }
Advertisement
Add Comment
Please, Sign In to add comment