Advertisement
Korotkodul

ДО A OK

Sep 28th, 2022 (edited)
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.96 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 pb(x) push_back(x)
  12. using namespace std;
  13. using ll = long long;
  14. using ld = long double;
  15. using db = double;
  16. void cv(vector <int> &v) {
  17.     for (auto x : v) cout << x << ' ';
  18.     cout << "\n";
  19. }
  20.  
  21. void cvl(vector <ll> &v) {
  22.     for (auto x : v) cout << x << ' ';
  23.     cout << "\n";
  24. }
  25.  
  26.  
  27. void cvv(vector <vector <int> > &v) {
  28.     for (auto x : v) cv(x);
  29.     cout << "\n";
  30. }
  31.  
  32. void cvb(vector <bool> v) {
  33.     for (bool x : v) cout << x << ' ';
  34.     cout << "\n";
  35. }
  36.  
  37. void cvs(vector <string>  v) {
  38.     for (auto a : v) {
  39.         cout << a << "\n";
  40.     }
  41. }
  42.  
  43. void cvp(vector <pii> a) {
  44.     for (auto p : a) {
  45.         cout << p.first << ' ' << p.second << "\n";
  46.     }
  47.     cout << "\n";
  48. }
  49.  
  50. int n, N, L, R,k;
  51.  
  52.  
  53. bool sh = 0;
  54.  
  55. struct tree{
  56.     vector <ll> v;
  57.     vector <ll> d;
  58.     //d[id] - какое значение надо присвоить всем эелементам на отрезке в-ны id
  59.     void bld(){
  60.         cin >> n >> k;
  61.         N = log2(n);
  62.         if (pow(2, N) < n) N++;
  63.         N = pow(2, N);
  64.         v.assign(2 * N - 1, 0);
  65.         d.assign(2 * N - 1, -1);
  66.     }
  67.  
  68.     void seeT() {
  69.         cout << "v\n";
  70.         int cnt = -1;
  71.         for (int i = 0; i <= log2(N); ++i) {
  72.             for (int j = 0; j < pow(2, i); ++ j) {
  73.                 cnt ++;
  74.                 cout << v[cnt] << ' ';
  75.             } cout << "\n";
  76.         }
  77.         cout << "\n";
  78.  
  79.         cout << "d\n";
  80.         cnt = -1;
  81.         for (int i = 0; i <= log2(N); ++i) {
  82.             for (int j = 0; j < pow(2, i); ++ j) {
  83.                 cnt ++;
  84.                 cout << d[cnt] << ' ';
  85.             } cout << "\n";
  86.         }
  87.         cout << "\n";
  88.     }
  89.  
  90.     void push(int id, int l, int r) {
  91.         if (sh) {
  92.             cout << "push\n";
  93.             cout << "l r = " << l << ' ' << r << "\n";
  94.         }
  95.         if (d[id] == -1) {
  96.             return;
  97.         }
  98.         v[id] = d[id] * (r - l + 1);
  99.         if (r > l) {
  100.             d[2 * id + 1] = d[id];
  101.             d[2 * id + 2] = d[id];
  102.         }
  103.         d[id] = -1;
  104.     }
  105.  
  106.     ll get(int id, int l, int r) {
  107.         push(id, l, r);
  108.         if (l >= L && r <= R) {
  109.             return v[id];
  110.         }
  111.         else if (max(l, L) <= min(r, R)) {
  112.             int m = (l + r) / 2;
  113.             return get(2 * id + 1, l, m) + get(2 * id + 2, m + 1, r);
  114.         }
  115.         return 0;
  116.     }
  117.  
  118.  
  119.  
  120.     void upd(int id, int l, int r, ll x) {
  121.         push(id, l, r);
  122.         if (sh) {
  123.             cout << "upd\n";
  124.             cout << "l r = " << l << ' ' << r << "\n";
  125.         }
  126.         //не вошел
  127.         if (r < L || l > R) {
  128.             if (sh) {
  129.                 cout << "NO INTERSECTION\n";
  130.             }
  131.             return;
  132.         }
  133.         else if (l >= L && r <= R) {//полностью вошел
  134.             if (sh) {
  135.                 cout << "COMPLETE intersetion\n";
  136.             }
  137.             d[id] = x;
  138.             push(id, l, r);
  139.             return;
  140.         }
  141.         //частичное пересечение
  142.         if (sh) {
  143.             cout << "part intersection\n";
  144.         }
  145.  
  146.         int m = (l + r) / 2;
  147.         upd(2 * id + 1, l, m, x);
  148.         upd(2 * id + 2, m + 1, r, x);
  149.         v[id] = v[id * 2 + 1] + v[id * 2 + 2];
  150.     }
  151. };
  152. /*
  153. 5 9
  154. A 2 3 2
  155. A 3 5 1
  156. A 4 5 2
  157. Q 1 3
  158. Q 2 2
  159. Q 3 4
  160. Q 4 5
  161. Q 5 5
  162. Q 1 5
  163.  
  164. */
  165.  
  166. int main() {
  167.     ios::sync_with_stdio(0);
  168.     cin.tie(0);
  169.     cout.tie(0);
  170.     tree T;
  171.     T.bld();
  172.     for (int i = 0; i < k ; ++ i) {
  173.         char q; cin >> q;
  174.         cin >> L >> R;
  175.         L--; R--;
  176.         if (q == 'A') {
  177.             ll x; cin >> x;
  178.             T.upd(0, 0, N - 1, x);
  179.         }
  180.         else{
  181.             cout << T.get(0, 0, N - 1) << "\n";
  182.         }
  183.         if (sh) {
  184.             T.seeT();
  185.         }
  186.     }
  187. }
  188.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement