Guest User

Untitled

a guest
Nov 9th, 2024
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. struct sqrt_block{
  2. ll n , b;
  3. vector<vector<ll>>blocks;
  4. vector<ll>A , Shift;
  5.  
  6. void init(ll _n){
  7. n = _n;
  8. b = ceil(sqrt(n));
  9. blocks.clear();
  10. Shift.clear();
  11. A.clear();
  12.  
  13. for(int i=0;i<n;i+=b){
  14. vector<ll>temp1;
  15. for(int j=i;j<min(i+b ,n);j++){
  16. temp1.pb(0);
  17. }
  18. if(sz(temp1)){
  19. blocks.pb(temp1);
  20. }
  21. }
  22. Shift.assign(b , 0);
  23. A.assign(n , 0);
  24. }
  25.  
  26. void rebuild(ll ind, ll l1 , ll r1 , ll add){
  27. blocks[ind].clear();
  28. for(int j = (ind)*b ; j < min(n , (ind+1)*b) ;j++){
  29. if(l1 <= j && j <= r1) A[j] += add;
  30. blocks[ind].push_back(A[j]);
  31. }
  32. sort(all(blocks[ind]));
  33. }
  34.  
  35. void add_new_range(ll l , ll r , ll add){
  36. ll ind1 = l/b;
  37. ll ind2 = r/b;
  38.  
  39. if(ind1==ind2){
  40. //in the same block
  41. rebuild(ind1 , l , r , add);
  42. }
  43. else{
  44. //some blocks
  45. rebuild(ind1 , l , ((ind1+1)*b) - 1 , add);
  46.  
  47. for(int j=ind1+1;j<ind2;j++){
  48. Shift[j] += add;
  49. }
  50.  
  51. rebuild(ind2 , ind2*b , r , add);
  52. }
  53. }
  54.  
  55. ll calculate(){ //count of zeros
  56. ll ans = 0;
  57. for(int i=0;i<blocks.size();i++){
  58. ans += (lb(all(blocks[i]) , 1 - Shift[i]) - blocks[i].begin());
  59. }
  60. return n - ans;
  61. }
  62.  
  63. }st;
Advertisement
Add Comment
Please, Sign In to add comment