Advertisement
Korotkodul

ЗОШ ДО присвоение 2

Jan 7th, 2022
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 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. #include <optional>
  11. #define pii pair <int,int>
  12. using namespace std;
  13. using ll = long long;
  14. using ld = long double;
  15. void cv(vector <int> &v){
  16. for (auto x: v) cout<<x<<' ';
  17. cout<<"\n";
  18. }
  19.  
  20. void cvl(vector <ll> &v){
  21. for (auto x: v) cout<<x<<' ';
  22. cout<<"\n";
  23. }
  24.  
  25. int l=0,r=10;
  26. int inter(int tl, int tr){
  27. if (tl >= l && tr <= r) return 2;
  28. else if (tr >= l && tl < l) return 1;
  29. else if (tl <= r && tr > r) return 1;
  30. else return 0;
  31. }
  32. void cvv(vector <vector <int> > &v){
  33. for (auto x: v) cv(x);
  34. cout<<"\n";
  35. }
  36. int n, mxn, K; //mxn - округлена до верхней стпени двойки
  37. //в конце ф-иии build: n = mxn
  38. vector <int> a;
  39. ll inf = 2e9;
  40. struct tree{
  41. vector <ll> t;
  42. vector < optional<ll> > to_push;//куда что присвоить
  43. void bld(){
  44. mxn = log2(n);
  45. //cout<<"n mxn = "<<n<<' '<<mxn<<"\n";
  46. if (pow(2, mxn) < n){
  47. mxn++;
  48. }
  49. mxn = pow(2, mxn);
  50. t.assign(2 * mxn - 1, -inf);
  51. to_push.resize(2 * mxn - 1);
  52. a.resize(mxn, -inf);
  53. for (int i = 0; i < n; ++i){
  54. cin>>a[i];
  55. }
  56. int id = 2*mxn-2;
  57. for (int i = mxn-1; i >= 0; --i){
  58. t[id] = a[i];
  59. id--;
  60. }
  61. for (int i = 2 * mxn - 2; i >= 2; i -= 2){
  62. t[(i-1)/2] = t[i] + t[i-1];
  63. }
  64. n = mxn;
  65. }
  66.  
  67. void sh(){
  68. cout<<"tree\n";
  69. cvl(t);
  70. cout<<"\n\n";
  71. }
  72.  
  73.  
  74. void push(int v, int tl, int tr){
  75. if (!to_push[v]) return;
  76. int to = 2*v+1;
  77. int tm = (tl + tr) / 2;
  78. to_push[to] = to_push[v];
  79. t[to] = to_push[v] * (tm - tl + 1);
  80. to++;
  81. to_push[to] = to_push[v];
  82. t[to] = to_push[v] * (tr - (tm + 1) + 1);
  83. to_push[v] = nullopt;
  84. //t[v] = max(t[to], t[to-1]);
  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 0;
  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. if (to_push[v]) push(v, tl, tr);
  100. int tm = (tl + tr) / 2;
  101. return get(v*2+1,tl, tm) + get(2*v+2, tm+1, tr);
  102. }
  103. }
  104.  
  105. void change(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_push[v] = val;
  114. t[v] = val * (tr - tl + 1);
  115. }
  116. else{
  117. //cout<<"partly intersect\n";
  118. if (to_push[v]) push(v, tl, tr);
  119. int tm = (tl + tr) / 2;
  120. change(2*v+1, tl, tm, val);
  121. change(2*v+2, tm+1, tr, val);
  122. t[v] = 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 == 'Q'){
  133. ll res = get(0,0,n-1);
  134. //cout<<"max("<<l<<","<<r<<") = "<<res<<"\n";
  135. cout<<res<<"\n";
  136. }else{
  137. int val; cin>>val;
  138. //cout<<"val = "<<val<<"\n";
  139. change(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>>K;
  151. tree wrk;
  152. wrk.bld();
  153. //wrk.sh();
  154. //int a,b; cin>>a>>b; cout<<inter(a, b);
  155. for (int i=0;i<K;++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.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement