Advertisement
pb_jiang

LC biweek 98 jls-template

Feb 18th, 2023
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.15 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. using i64 = long long;
  6.  
  7. template<class Info, class Tag>
  8. struct LazySegmentTree {
  9.     const int n;
  10.     std::vector<Info> info;
  11.     std::vector<Tag> tag;
  12.     LazySegmentTree(int n) : n(n), info(4 << std::__lg(n)), tag(4 << std::__lg(n)) {}
  13.     LazySegmentTree(std::vector<Info> init) : LazySegmentTree(init.size()) {
  14.         std::function<void(int, int, int)> build = [&](int p, int l, int r) {
  15.             if (r - l == 1) {
  16.                 info[p] = init[l];
  17.                 return;
  18.             }
  19.             int m = (l + r) / 2;
  20.             build(2 * p, l, m);
  21.             build(2 * p + 1, m, r);
  22.             pull(p);
  23.         };
  24.         build(1, 0, n);
  25.     }
  26.     void pull(int p) {
  27.         info[p] = info[2 * p] + info[2 * p + 1];
  28.     }
  29.     void apply(int p, const Tag &v) {
  30.         info[p].apply(v);
  31.         tag[p].apply(v);
  32.     }
  33.     void push(int p) {
  34.         apply(2 * p, tag[p]);
  35.         apply(2 * p + 1, tag[p]);
  36.         tag[p] = Tag();
  37.     }
  38.     void modify(int p, int l, int r, int x, const Info &v) {
  39.         if (r - l == 1) {
  40.             info[p] = v;
  41.             return;
  42.         }
  43.         int m = (l + r) / 2;
  44.         push(p);
  45.         if (x < m) {
  46.             modify(2 * p, l, m, x, v);
  47.         } else {
  48.             modify(2 * p + 1, m, r, x, v);
  49.         }
  50.         pull(p);
  51.     }
  52.     void modify(int p, const Info &v) {
  53.         modify(1, 0, n, p, v);
  54.     }
  55.     Info rangeQuery(int p, int l, int r, int x, int y) {
  56.         if (l >= y || r <= x) {
  57.             return Info();
  58.         }
  59.         if (l >= x && r <= y) {
  60.             return info[p];
  61.         }
  62.         int m = (l + r) / 2;
  63.         push(p);
  64.         return rangeQuery(2 * p, l, m, x, y) + rangeQuery(2 * p + 1, m, r, x, y);
  65.     }
  66.     Info rangeQuery(int l, int r) {
  67.         return rangeQuery(1, 0, n, l, r);
  68.     }
  69.     void rangeApply(int p, int l, int r, int x, int y, const Tag &v) {
  70.         if (l >= y || r <= x) {
  71.             return;
  72.         }
  73.         if (l >= x && r <= y) {
  74.             apply(p, v);
  75.             return;
  76.         }
  77.         int m = (l + r) / 2;
  78.         push(p);
  79.         rangeApply(2 * p, l, m, x, y, v);
  80.         rangeApply(2 * p + 1, m, r, x, y, v);
  81.         pull(p);
  82.     }
  83.     void rangeApply(int l, int r, const Tag &v) {
  84.         return rangeApply(1, 0, n, l, r, v);
  85.     }
  86.    
  87.     int search(int p, int l, int r, int x, int y, i64 v) {
  88.         if (l >= y || r <= x) return y;
  89.         if (info[p].min >= v) return y;
  90.         if (r - l == 1) return l;
  91.         int m = (l + r) / 2;
  92.         push(p);
  93.         int res = search(2 * p, l, m, x, y, v);
  94.         if (res == y) res = search(2 * p + 1, m, r, x, y, v);
  95.         return res;
  96.     }
  97.    
  98.     int search(int l, int r, i64 v) {
  99.         return search(1, 0, n, l, r, v);
  100.     }
  101. };
  102.  
  103. constexpr i64 inf = 1E18;
  104.  
  105. struct Tag {
  106.     int rev;
  107.  
  108.     Tag(int _rev = 0) : rev{_rev} {}
  109.    
  110.     void apply(const Tag &t) {
  111.         if (t.rev) {
  112.             rev ^= 1;
  113.         }
  114.     }
  115. };
  116.  
  117. struct Info {
  118.     int up;
  119.     int down;
  120.  
  121.     Info() : up{0}, down{0} {}
  122.     Info(int _up, int _down) : up{_up}, down{_down} {}
  123.    
  124.     void apply(const Tag &t) {
  125.         if (t.rev) {
  126.             swap(up, down);
  127.         }
  128.     }
  129. };
  130.  
  131. Info operator+(const Info &a, const Info &b) {
  132.     Info c;
  133.     c.up = a.up + b.up;
  134.     c.down = a.down + b.down;
  135.     return c;
  136. }
  137.  
  138. class Solution {
  139. public:
  140.     vector<long long> handleQuery(vector<int>& nums1, vector<int>& nums2, vector<vector<int>>& queries) {
  141.         vector<Info> info;
  142.         for (int i = 0; i < (int) nums1.size(); i++) {
  143.             info.push_back(Info(nums1[i], nums1[i] ^ 1));
  144.         }
  145.         long long s = accumulate(nums2.begin(), nums2.end(), 0LL);
  146.  
  147.         vector<long long> ans;
  148.         LazySegmentTree<Info, Tag> seg(info);
  149.         for (auto &qs: queries) {
  150.             int op = qs[0];
  151.             if (op == 1) {
  152.                 seg.rangeApply(qs[1], qs[2] + 1, Tag(1));
  153.             } else if (op == 2) {
  154.                 s += seg.info[1].up * (long long) qs[1];
  155.             } else {
  156.                 ans.push_back(s);
  157.             }
  158.         }
  159.  
  160.         return ans;
  161.     }
  162. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement