Advertisement
Korotkodul

Section Tree. index of maximum

Dec 16th, 2021
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <string>
  7. #include <stack>
  8. #include <set>
  9. #include <map>
  10. #define pii pair <int,int>
  11. #define pli pair <long long, int>
  12. using namespace std;
  13. using ll = long long;
  14. using ld = long double;
  15. void cv(vector <ll> &v){
  16. for (auto x: v) cout<<x<<' ';
  17. cout<<"\n\n";
  18. }
  19. ld n;
  20. int N;
  21. vector <ll> a;
  22. int L,R;
  23.  
  24. int inter(int l, int r){
  25. //cout<<"LR= "<<L<<' '<<R<<'\n';
  26. //cout<<"lr= "<<l<<' '<<r<<'\n';
  27. if (l >= L && r <= R) {//cout<<"ONE\n";
  28. return 2;
  29. }
  30. else if (l < L && r >= L) {//cout<<"TWO\n";
  31. return 1;
  32. }
  33. else if (r > R && l <= R){//cout<<"THREE\n";
  34. return 1;
  35. }
  36. else return 0;
  37. }
  38. ll inf = pow(2, 63) - 100;
  39. struct tree{
  40. vector <ll> tot;//sum
  41. vector <ll> MIN;
  42. vector <pli> MAX;
  43. void bld(){
  44.  
  45. tot.assign(2*N-1,0);
  46. MIN.assign(2*N-1, inf);
  47. MAX.assign(2*N-1, {-inf, -1} );
  48. for (int i = 0; i < n; ++i){
  49. tot[N - 1 + i] = a[i];
  50. MIN[N - 1 + i] = a[i];
  51. MAX[N - 1 + i] = {a[i], i};
  52. }
  53. int id;
  54. for (int i = N * 2 - 2; i >= 2; i-=2){
  55. id = (i - 1) / 2;
  56. tot[id] = tot[i] + tot[i-1];
  57. MIN[id] = min(MIN[i], MIN[i-1]);
  58. if (MAX[i-1].first >= MAX[i].first) MAX[id] = MAX[i-1];
  59. else MAX[id] = MAX[i];
  60. }
  61. }
  62. void sh(){
  63. //cv(tot);
  64. //cv(MIN);
  65. for (int i = 0; i < MAX.size();++i){
  66. cout<<i<<' '<<MAX[i].first<<' '<<MAX[i].second<<'\n';
  67. }
  68. }
  69. ll getS(int l, int r, int id){
  70. //cout<<"id = "<<id<<"\n";
  71. int how = inter(l, r);
  72. //cout<<"how= "<<how<<'\n';
  73. if (how == 2) return tot[id];
  74. else if (how == 0) return 0;
  75. else{
  76. int m = (l + r) / 2;
  77. return getS(l, m, id*2+1) + getS(m+1, r, id*2+2);
  78. }
  79. }
  80. ll getMin(int l, int r, int id){
  81. int how = inter(l, r);
  82. if (how == 2) return MIN[id];
  83. else if (how == 0) return inf;
  84. else{
  85. int m = (l + r) / 2;
  86. return min(getMin(l, m, id*2+1) , getMin(m+1, r, id*2+2));
  87. }
  88. }
  89. pli getMax(int l, int r, int id){
  90. //cout<<"id= "<<id<<"\n";
  91. int how = inter(l, r);
  92. if (how == 2) return MAX[id];
  93. else if (how == 0) return {-inf, -1};
  94. else{
  95. int m = (l + r) / 2;
  96. pli a,b;
  97. a = getMax(l, m, id*2+1);
  98. b = getMax(m+1, r, id*2+2);
  99. if (a.first >= b.first) return a;
  100. else return b;
  101. }
  102. }
  103. void chg(int id){
  104. //cout<<"id = "<<id<<"\n";
  105. tot[id] = tot[id*2 + 1] + tot[id*2 + 2];
  106. MIN[id] = min(tot[id*2 + 1], tot[id*2 + 2]);
  107. if (MAX[id*2 + 1].first >= MAX[id*2 + 2].first){
  108. MAX[id] = MAX[id*2 + 1];
  109. }
  110. else MAX[id] = MAX[id*2 + 2];
  111.  
  112. if (id == 0) return;
  113. chg( (id - 1) / 2);
  114. }
  115. };
  116.  
  117. tree wrk;
  118.  
  119. int main()
  120. {
  121. ios::sync_with_stdio(0);
  122. cin.tie(0);
  123. cout.tie(0);
  124. cin>>n;
  125. N = pow(2, ceil(log2(n)));
  126. a.assign(n, 0);
  127. for (ll &x: a) cin>>x;
  128. wrk.bld();
  129. //wrk.sh();
  130. int m; cin>>m; char wht;
  131. pli res;
  132. for (int i=0;i<m;++i){
  133. cin>>wht;
  134. if (wht == 's') {
  135. cin>>L>>R;
  136. L--; R--;
  137. res = wrk.getMax(0, N-1, 0);
  138. cout<<res.second+1<<' ';
  139. }else{
  140. int x,y;
  141. cin>>x>>y;
  142. x--;
  143. x += N - 1;
  144. wrk.MAX[x].first = y;
  145. wrk.chg( (x - 1) / 2);
  146. //wrk.sh();
  147. }
  148. }
  149.  
  150. }
  151. /*
  152. 5
  153. 2 4 1 3 5
  154. 3
  155. s 2 4
  156. u 1 10
  157. s 1 3
  158. */
  159.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement