Korotkodul

ЗОШ_ДО_массовые операции_ОК

Jan 7th, 2022 (edited)
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 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. using namespace std;
  12. using ll = long long;
  13. using ld = long double;
  14. void cv(vector <int> &v){
  15. for (auto x: v) cout<<x<<' ';
  16. cout<<"\n";
  17. }
  18.  
  19. void cvl(vector <ll> &v){
  20. for (auto x: v) cout<<x<<' ';
  21. cout<<"\n";
  22. }
  23.  
  24. int l=0,r=10;
  25. int inter(int tl, int tr){
  26. if (tl >= l && tr <= r) return 2;
  27. else if (tr >= l && tl < l) return 1;
  28. else if (tl <= r && tr > r) return 1;
  29. else return 0;
  30. }
  31. void cvv(vector <vector <int> > &v){
  32. for (auto x: v) cv(x);
  33. cout<<"\n";
  34. }
  35. int n, mxn; //mxn - округлена до верхней стпени двойки
  36. //в конце ф-иии build: n = mxn
  37. vector <int> a;
  38. ll inf = 2e9;
  39. struct tree{
  40. vector <ll> t;
  41. vector <ll> to_add;
  42. void bld(){
  43. mxn = log2(n);
  44. //cout<<"n mxn = "<<n<<' '<<mxn<<"\n";
  45. if (pow(2, mxn) < n){
  46. mxn++;
  47. }
  48. mxn = pow(2, mxn);
  49. t.assign(2 * mxn - 1, -inf);
  50. to_add.assign(2 * mxn - 1, 0);
  51. a.resize(mxn, -inf);
  52. for (int i = 0; i < n; ++i){
  53. cin>>a[i];
  54. }
  55. int id = 2*mxn-2;
  56. for (int i = mxn-1; i >= 0; --i){
  57. t[id] = a[i];
  58. id--;
  59. }
  60. for (int i = 2 * mxn - 2; i >= 2; i -= 2){
  61. t[(i-1)/2] = max(t[i] , t[i-1]);
  62. }
  63. n = mxn;
  64. }
  65.  
  66. void sh(){
  67. cout<<"tree\n";
  68. cvl(t);
  69. cout<<"\n\n";
  70. }
  71.  
  72.  
  73. void push(int v, int tl, int tr){
  74. if (to_add[v] == 0) return;
  75. int to = 2*v+1;
  76. int tm = (tl + tr) / 2;
  77. to_add[to] += to_add[v];
  78. t[to] += to_add[v];
  79. to++;
  80. to_add[to] += to_add[v];
  81. t[to] += to_add[v];
  82. to_add[v] = 0;
  83. //t[v] = max(t[to], t[to-1]);
  84. }
  85.  
  86.  
  87. ll get(int v, int tl, int tr){
  88. //cout<<"tl tr = "<<tl<<' '<<tr<<"\n";
  89. if (inter(tl, tr) == 0) {
  90. //cout<<"not intersect\n";
  91. return -inf;
  92. }
  93. else if (inter(tl, tr) == 2){
  94. //cout<<"contains\n";
  95. return t[v];
  96. }
  97. else if (inter(tl, tr) == 1){
  98. //cout<<"partly intersect\n";
  99. push(v, tl, tr);
  100. int tm = (tl + tr) / 2;
  101. return max(get(v*2+1,tl, tm) , get(2*v+2, tm+1, tr));
  102. }
  103. }
  104.  
  105. void add(int v, int tl, int tr, int val){
  106. //cout<<"tl tr = "<<tl<<' '<<tr<<"\n";
  107. if (inter(tl, tr) == 0) {
  108. //cout<<"not intersect\n";
  109. return;
  110. }
  111. else if (inter(tl, tr) == 2){
  112. //cout<<"contains\n";
  113. to_add[v] += val;
  114. t[v] += val;
  115. }
  116. else{
  117. //cout<<"partly intersect\n";
  118. int tm = (tl + tr) / 2;
  119. push(v, tl, tr);
  120. add(2*v+1, tl, tm, val);
  121. add(2*v+2, tm+1, tr, val);
  122. t[v] = max(t[v*2+1], t[v*2+2]);
  123. }
  124. }
  125.  
  126. void req(){
  127. char x; cin>>x;
  128. cin>>l>>r;
  129. l--; r--;
  130. //cout<<"request = "<<x<<"\n";
  131. //cout<<"l r = "<<l<<" "<<r<<"\n";
  132. if (x == 'm'){
  133. ll res = get(0,0,n-1);
  134. //cout<<"max("<<l<<","<<r<<") = "<<res<<"\n";
  135. cout<<res<<' ';
  136. }else{
  137. int val; cin>>val;
  138. //cout<<"val = "<<val<<"\n";
  139. add(0, 0, n-1, val);
  140. }
  141. //sh();
  142. }
  143. };
  144.  
  145. int main()
  146. {
  147. ios::sync_with_stdio(0);
  148. cin.tie(0);
  149. cout.tie(0);
  150. cin>>n;
  151. tree wrk;
  152. wrk.bld();
  153. //wrk.sh();
  154. //int a,b; cin>>a>>b; cout<<inter(a, b);
  155. int q; cin>>q; for (int i=0;i<q;++i) {
  156. wrk.req();
  157. }
  158. }
  159. /*
  160. 5
  161. 2 4 3 1 5
  162. 5
  163. m 1 3
  164. a 2 4 100
  165. m 1 3
  166. a 5 5 10
  167. m 1 5
  168.  
  169. */
  170.  
Add Comment
Please, Sign In to add comment