Advertisement
Korotkodul

ЗОШ ДО ПРИСВОЕНИЕ осталось отдебажить

Jan 7th, 2022 (edited)
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 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. int to = 2*v+1;
  78. int tm = (tl + tr) / 2;
  79. to_push[to] = to_push[v];
  80. t[to] = to_push[v] * (tm - tl + 1);
  81. to++;
  82. to_push[to] = to_push[v];
  83. t[to] = to_push[v] * (tr - (tm + 1) + 1);
  84. to_push[v] = -1;
  85. //t[v] = max(t[to], t[to-1]);
  86. }
  87.  
  88. ll get(int v, int tl, int tr){
  89. //cout<<"tl tr = "<<tl<<' '<<tr<<"\n";
  90. if (inter(tl, tr) == 0) {
  91. //cout<<"not intersect\n";
  92. return 0;
  93. }
  94. else if (inter(tl, tr) == 2){
  95. //cout<<"contains\n";
  96. return t[v];
  97. }
  98. else if (inter(tl, tr) == 1){
  99. //cout<<"partly intersect\n";
  100. if (to_push[v]) push(v, tl, tr);
  101. int tm = (tl + tr) / 2;
  102. return get(v*2+1,tl, tm) + get(2*v+2, tm+1, tr);
  103. }
  104. }
  105.  
  106. void change(int v, int tl, int tr, int val){
  107. //cout<<"tl tr = "<<tl<<' '<<tr<<"\n";
  108. if (inter(tl, tr) == 0) {
  109. //cout<<"not intersect\n";
  110. return;
  111. }
  112. else if (inter(tl, tr) == 2){
  113. //cout<<"contains\n";
  114. to_push[v] = val;
  115. t[v] = val * (tr - tl + 1);
  116. }
  117. else{
  118. //cout<<"partly intersect\n";
  119. if (to_push[v] != -1) push(v, tl, tr);
  120. int tm = (tl + tr) / 2;
  121. change(2*v+1, tl, tm, val);
  122. change(2*v+2, tm+1, tr, val);
  123. t[v] = t[v*2+1] + t[v*2+2];
  124. }
  125. }
  126.  
  127. void req(){
  128. char x; cin>>x;
  129. cin>>l>>r;
  130. l--; r--;
  131. //cout<<"request = "<<x<<"\n";
  132. //cout<<"l r = "<<l<<" "<<r<<"\n";
  133. if (x == 'Q'){
  134. ll res = get(0,0,n-1);
  135. //cout<<"max("<<l<<","<<r<<") = "<<res<<"\n";
  136. cout<<res<<"\n";
  137. }else{
  138. int val; cin>>val;
  139. //cout<<"val = "<<val<<"\n";
  140. change(0, 0, n-1, val);
  141. sh();
  142. }
  143. //sh();
  144. }
  145. };
  146.  
  147. int main()
  148. {
  149. /*ios::sync_with_stdio(0);
  150. cin.tie(0);
  151. cout.tie(0);*/
  152. cin>>n>>K;
  153. tree wrk;
  154. wrk.bld();
  155. //wrk.sh();
  156. //int a,b; cin>>a>>b; cout<<inter(a, b);
  157. for (int i=0;i<K;++i) {
  158. wrk.req();
  159. }
  160. }
  161. /*
  162.  
  163. 10 5
  164. Q 1 10
  165. A 1 10 10
  166. Q 1 10
  167. A 1 2 5
  168. Q 1 10
  169.  
  170.  
  171. 10 100
  172.  
  173. */
  174.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement