Advertisement
newb_ie

new_tree

Aug 9th, 2021
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int maxN = 100010;
  5. int n;
  6. int64_t t[4 * maxN];
  7. pair<int,int> minimum[4 * maxN];
  8. pair<int,int> maximum[4 * maxN];
  9. int ans = -1;
  10.  
  11. pair<int,int> combine (pair<int,int> a,pair<int,int> b) {
  12. if (a.first < b.first) return a;
  13. if (b.first < a.first) return b;
  14. return make_pair(a.first,b.second + a.second);
  15. }
  16.  
  17. pair<int,int> combine_maximum(pair<int,int> a,pair<int,int> b) {
  18. if (a.first > b.first) return a;
  19. if (b.first > a.first) return b;
  20. return make_pair(a.first,a.second + b.second);
  21. }
  22.  
  23. void build (int64_t a[],int v,int tl,int tr) {
  24. if (tl == tr) {
  25. t[v] = a[tl];
  26. minimum[v] = make_pair(a[tl],1);
  27. maximum[v] = make_pair(a[tl],1);
  28. } else {
  29. int tm = tl + (tr - tl) / 2;
  30. build(a,v * 2 + 1,tl,tm);
  31. build(a,v * 2 + 2,tm + 1,tr);
  32. t[v] = t[v * 2 + 1] + t[v * 2 + 2];
  33. minimum[v] = combine(minimum[2 * v + 1],minimum[2 * v + 2]);
  34. maximum[v] = combine_maximum(maximum[2 * v + 1],maximum[2 * v + 2]);
  35. }
  36. }
  37.  
  38. void point_update (int v,int tl,int tr,int pos,int new_val) {
  39. if (tl == tr) {
  40. t[v] = new_val;
  41. minimum[v] = make_pair(new_val,1);
  42. maximum[v] = make_pair(new_val,1);
  43. } else {
  44. int tm = tl + (tr - tl) / 2;
  45. if (pos <= tm) {
  46. point_update(v * 2 + 1,tl,tm,pos,new_val);
  47. } else {
  48. point_update(v * 2 + 2,tm + 1,tr,pos,new_val);
  49. }
  50. t[v] = t[v * 2 + 1] + t[v * 2 + 2];
  51. minimum[v] = combine(minimum[v * 2 + 1],minimum[v * 2 + 2]);
  52. maximum[v] = combine_maximum(maximum[2 * v + 1],maximum[2 * v + 2]);
  53. }
  54. }
  55.  
  56. int64_t sum_query (int v,int tl,int tr,int l,int r) {
  57. if (l > r) return 0;
  58. if (l == tl and r == tr) return t[v];
  59. int tm = tl + (tr - tl) / 2;
  60. return sum_query(v * 2 + 1,tl,tm,l,min(r,tm)) + sum_query(v * 2 + 2,tm + 1,tr,max(l,tm + 1),r);
  61. }
  62.  
  63. pair<int,int> minimum_query (int v,int tl,int tr,int l,int r) {
  64. if (l > r) return make_pair(INT_MAX,0);
  65. if (l == tl and r == tr) return minimum[v];
  66. int tm = tl + (tr - tl) / 2;
  67. return combine(minimum_query(v * 2 + 1,tl,tm,l,min(r,tm)),minimum_query(v * 2 + 2,tm + 1,tr,max(l,tm + 1),r));
  68. }
  69.  
  70. pair<int,int> maximum_query (int v,int tl,int tr,int l,int r) {
  71. if (l > r) return make_pair(INT_MIN,0);
  72. if (l == tl and r == tr) return maximum[v];
  73. int tm = tl + (tr - tl) / 2;
  74. return combine_maximum(maximum_query(v * 2 + 1,tl,tm,l,min(r,tm)),maximum_query(v * 2 + 2,tm + 1,tr,max(l,tm + 1),r));
  75. }
  76.  
  77. int find_kth_one_idx_query (int v,int tl,int tr,int k) {
  78. if (k > t[v]) return -1; //if input[] contains less then k ones
  79. if (tl == tr) {
  80. if (t[v] == k) return tl;
  81. else return n;
  82. }
  83. int tm = tl + (tr - tl) / 2;
  84. if (t[2 * v + 1] >= k) {
  85. return find_kth_one_idx_query(2 * v + 1,tl,tm,k);
  86. } else {
  87. return find_kth_one_idx_query(2 * v + 2,tm + 1,tr,k - t[2 * v + 1]);
  88. }
  89. }
  90.  
  91. int find_kth_idx (int v,int tl,int tr,int k) {
  92. if (tl == tr) return tl;
  93. int tm = tl + (tr - tl) / 2;
  94. if (maximum[2 * v].first < k) {
  95. return find_kth_idx(2 * v + 1,tm + 1,tr,k);
  96. } else {
  97. return find_kth_idx(2 * v,tl,tm,k);
  98. }
  99. }
  100.  
  101. void find_kth_idx_greater_equal_to_given_idx (int v,int tl,int tr,int k,int l) {
  102. if (maximum[v].first < k) return;
  103. if (ans >= 0 or tr < l) return;
  104. if (tl == tr) {
  105. if (maximum[v].first >= k) ans = tl;
  106. return;
  107. }
  108. int tm = tl + (tr - tl) / 2;
  109. find_kth_idx_greater_equal_to_given_idx(v * 2 + 1,tl,tm,k,l);
  110. find_kth_idx_greater_equal_to_given_idx(v * 2 + 2,tm + 1,tr,k,l);
  111. }
  112.  
  113. int main () {
  114. ios::sync_with_stdio(false);
  115. cin.tie(nullptr);
  116. cout.tie(nullptr);
  117. int T = 1;
  118. //~ cin >> T;
  119. for (int test_case = 1; test_case <= T; ++test_case) {
  120. cin >> n;
  121. int64_t a[n];
  122. for (int i = 0; i < n; ++i) cin >> a[i];
  123. int64_t b[n];
  124. int res[n];
  125. fill(b,b + n,1);
  126. build(b,0,0,n - 1);
  127. for (int i = n - 1; i >= 0; --i) {
  128. int idx = find_kth_one_idx_query(0,0,n - 1,t[0] - a[i]);
  129. res[i] = idx + 1;
  130. point_update(0,0,n - 1,max(idx,0),0);
  131. }
  132. for (int i = 0; i < n; ++i) cout << res[i] << ' ';
  133. //~ //for sum_query sum_query (1,0,n - 1,l,r)
  134.  
  135. //for minimum / maximum query pair<int,int> res = minimum / maximum_query(1,0,n - 1,l,r);
  136.  
  137. //res.first -> min on the given segement and res.second -> count of min/max on a given segment
  138.  
  139. //to find kth one find_kth_one_idx_query(1,0,n - 1,k)
  140.  
  141. //to find kth idx where input[idx] >= x -> find_kth_idx(1,0,n - 1,x)
  142. //to find kth idx where idx >= l and input[idx] >= x -> find_kth_idx_greater_equal_to_given_idx(1,0,n - 1,k,l)
  143. }
  144. //cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
  145. }
  146.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement