Korotkodul

ЗОШ ДО OK

Jan 8th, 2022 (edited)
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 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 pbl pair <bool, long long>
  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 < 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, 0);
  51. to_push.resize(2 * mxn - 1, -1 );
  52. a.resize(mxn, 0);
  53. for (int i = 0; i < n; ++i){
  54. //cin>>a[i];
  55. a[i] = 0;
  56. }
  57. int id = 2*mxn-2;
  58. for (int i = mxn-1; i >= 0; --i){
  59. t[id] = a[i];
  60. id--;
  61. }
  62. for (int i = 2 * mxn - 2; i >= 2; i -= 2){
  63. t[(i-1)/2] = t[i] + t[i-1];
  64. }
  65. n = mxn;
  66. }
  67.  
  68. void sh(){
  69. cout<<"tree\n";
  70. cvl(t);
  71. cout<<"\n\n";
  72. }
  73.  
  74.  
  75. void push(int v, int tl, int tr){
  76. if (to_push[v] == -1) return;
  77. t[v] = (tr - tl + 1) * to_push[v];
  78. if (tl != tr){
  79. to_push[2*v+1] = to_push[v];
  80. to_push[2*v + 2] = to_push[v];
  81. }
  82. to_push[v] = -1;
  83. }
  84.  
  85. ll get(int v, int tl, int tr){
  86. push(v, tl, tr);
  87. //cout<<"tl tr = "<<tl<<' '<<tr<<"\n";
  88. if (inter(tl, tr) == 0) {
  89. //cout<<"not intersect\n";
  90. return 0;
  91. }
  92. else if (inter(tl, tr) == 2){
  93. //cout<<"contains\n";
  94. return t[v];
  95. }
  96. else if (inter(tl, tr) == 1){
  97. //cout<<"partly intersect\n";
  98. int tm = (tl + tr) / 2;
  99. return get(v*2+1,tl, tm) + get(2*v+2, tm+1, tr);
  100. }
  101. }
  102.  
  103. void change(int v, int tl, int tr, int val){
  104. //cout<<"tl tr = "<<tl<<' '<<tr<<"\n";
  105. push(v, tl, tr);
  106. if (inter(tl, tr) == 0) {
  107. //cout<<"not intersect\n";
  108. return;
  109. }
  110. else if (inter(tl, tr) == 2){
  111. //cout<<"contains\n";
  112. to_push[v] = val;
  113. push(v, tl, tr);
  114. return;
  115. }
  116. else{
  117. //cout<<"partly intersect\n";
  118. int tm = (tl + tr) / 2;
  119. change(2*v+1, tl, tm, val);
  120. change(2*v+2, tm+1, tr, val);
  121. t[v] = t[v*2+1] + t[v*2+2];
  122. }
  123. }
  124.  
  125. void req(){
  126. char x; cin>>x;
  127. cin>>l>>r;
  128. l--; r--;
  129. //cout<<"request = "<<x<<"\n";
  130. //cout<<"l r = "<<l<<" "<<r<<"\n";
  131. if (x == 'Q'){
  132. ll res = get(0,0,n-1);
  133. //cout<<"max("<<l<<","<<r<<") = "<<res<<"\n";
  134. cout<<res<<"\n";
  135. }else{
  136. int val; cin>>val;
  137. //cout<<"val = "<<val<<"\n";
  138. change(0, 0, n-1, val);
  139. //sh();
  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.  
  161. 10 5
  162. Q 1 10
  163. A 1 10 10
  164. Q 1 10
  165. A 1 2 5
  166. Q 1 10
  167.  
  168.  
  169. 10 100
  170.  
  171. 1 4
  172. 2
  173. */
  174.  
Add Comment
Please, Sign In to add comment