Advertisement
Snapper_001

Untitled

Mar 5th, 2023
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. struct segTree{
  5. ll size;
  6. vector<ll>tree , lazy;
  7. void init(ll n){
  8. tree.clear();
  9. size =1;
  10. while(size < n){
  11. size*=2;
  12. }
  13. tree.assign(2*size ,0);
  14. lazy.assign(2*size ,0);
  15. }
  16.  
  17. void update(ll x , ll lx , ll rx , ll q_lx , ll q_rx){
  18. if(q_lx<=lx && q_rx>=rx){
  19. lazy[x]++;
  20. return;
  21. }
  22. if(q_lx > rx || q_rx < lx) return;
  23.  
  24. lazy[2*x+1] += lazy[x];
  25. lazy[2*x] += lazy[x];
  26. lazy[x] = 0;
  27.  
  28. ll mid = (lx+rx)>>1;
  29. update(2*x , lx , mid , q_lx , q_rx);
  30. update(2*x+1 , mid+1 , rx , q_lx , q_rx);
  31. }
  32.  
  33. void update(ll q_lx , ll q_rx){
  34. update(1 , 0, size-1 , q_lx , q_rx);
  35. }
  36.  
  37. ll query(ll x ,ll lx ,ll rx , ll ind){
  38. if(lx==rx){
  39. tree[x] += lazy[x];
  40. lazy[x] = 0;
  41. return tree[x];
  42. }
  43.  
  44. lazy[2*x+1] += lazy[x];
  45. lazy[2*x] += lazy[x];
  46. lazy[x] = 0;
  47.  
  48. ll mid = (lx+rx)>>1;
  49. if(ind<=mid && ind >= lx){
  50. return query(2*x , lx , mid , ind);
  51. }
  52. else{
  53. return query(2*x+1 , mid+1 , rx , ind);
  54. }
  55. }
  56.  
  57. ll query(ll i){
  58. return query(1 ,0 , size-1, i);
  59. }
  60.  
  61. }st;
  62.  
  63.  
  64. vector<ll> Solve(vector<vector<ll>>&A, vector<vector<ll>>&B) {
  65. ll total_monster =0;
  66. vector<ll>Power;
  67. for(int i=0;i<A.size();i++){
  68. swap(A[i][2] , A[i][0]);
  69. swap(A[i][2] , A[i][1]);
  70. }
  71.  
  72. sort(A.begin() , A.end());
  73. for(auto it : A){
  74. Power.push_back(it[0]);
  75. }
  76.  
  77. //Given ranges we have to find number of time B[i][0] occur in [o...i]
  78. //we can make segtree and point query can be done
  79. st.init(100000+7);
  80. map<ll, vector<ll>>LST_LIMIT;
  81. for(int i=0;i<B.size();i++){
  82. auto it = lower_bound(Power.begin() , Power.end() , B[i][1]);
  83. if(it!=Power.begin()) LST_LIMIT[(it-Power.begin()) - 1].push_back(i);
  84. }
  85.  
  86. vector<ll>Ans(B.size() , 0);
  87. for(int i=0;i<A.size();i++){
  88. ll l = A[i][1];
  89. ll r = A[i][2];
  90. total_monster += (r-l+1);
  91. st.update(l ,r);
  92.  
  93. if(LST_LIMIT.count(i)){
  94. //update answer for those
  95. for(auto x : LST_LIMIT[i]){
  96. //we want number of elements that are present at its position
  97. Ans[x] = st.query(B[x][0]);
  98. }
  99. }
  100. }
  101. //Now we have our answer
  102.  
  103. unordered_map<ll ,ll>pre_Ans;
  104. vector<ll>Finals(B.size());
  105.  
  106. ll prev_ans = total_monster;
  107. for(int i=0;i<B.size();i++){
  108. if(pre_Ans.count(B[i][0])){
  109. //the hero that deletes some at this position is let x
  110. ll x = pre_Ans[B[i][0]];
  111. if(B[x][1] < B[i][1]){
  112. //remove old ans and add new
  113. prev_ans += Ans[x];
  114. prev_ans -= Ans[i];
  115. pre_Ans[B[i][0]]= i;
  116. Finals[i] = prev_ans;
  117. }
  118. else{
  119. Finals[i] = prev_ans;
  120. }
  121. }
  122. else{
  123. pre_Ans[B[i][0]] = i;
  124. prev_ans -= Ans[i];
  125. Finals[i] = prev_ans;
  126. }
  127. }
  128. return Finals;
  129. }
  130.  
  131. int main(){
  132. vector<vector<ll>>A = {{2 ,5 ,8} , {5 ,8 ,6} , {3 , 6 ,9} , {7 ,10, 10}};
  133. vector<vector<ll>>B = {{2 ,7} , {7 ,9} , {7 ,11}};
  134.  
  135. vector<ll>Ans = Solve(A , B);
  136. for(auto it :Ans){
  137. cout<<it<<" ";
  138. }
  139. cout<<endl;
  140.  
  141. return 0;
  142. }
  143.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement