Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- using i64 = long long;
- template<class Info, class Tag>
- struct LazySegmentTree {
- const int n;
- std::vector<Info> info;
- std::vector<Tag> tag;
- LazySegmentTree(int n) : n(n), info(4 << std::__lg(n)), tag(4 << std::__lg(n)) {}
- LazySegmentTree(std::vector<Info> init) : LazySegmentTree(init.size()) {
- std::function<void(int, int, int)> build = [&](int p, int l, int r) {
- if (r - l == 1) {
- info[p] = init[l];
- return;
- }
- int m = (l + r) / 2;
- build(2 * p, l, m);
- build(2 * p + 1, m, r);
- pull(p);
- };
- build(1, 0, n);
- }
- void pull(int p) {
- info[p] = info[2 * p] + info[2 * p + 1];
- }
- void apply(int p, const Tag &v) {
- info[p].apply(v);
- tag[p].apply(v);
- }
- void push(int p) {
- apply(2 * p, tag[p]);
- apply(2 * p + 1, tag[p]);
- tag[p] = Tag();
- }
- void modify(int p, int l, int r, int x, const Info &v) {
- if (r - l == 1) {
- info[p] = v;
- return;
- }
- int m = (l + r) / 2;
- push(p);
- if (x < m) {
- modify(2 * p, l, m, x, v);
- } else {
- modify(2 * p + 1, m, r, x, v);
- }
- pull(p);
- }
- void modify(int p, const Info &v) {
- modify(1, 0, n, p, v);
- }
- Info rangeQuery(int p, int l, int r, int x, int y) {
- if (l >= y || r <= x) {
- return Info();
- }
- if (l >= x && r <= y) {
- return info[p];
- }
- int m = (l + r) / 2;
- push(p);
- return rangeQuery(2 * p, l, m, x, y) + rangeQuery(2 * p + 1, m, r, x, y);
- }
- Info rangeQuery(int l, int r) {
- return rangeQuery(1, 0, n, l, r);
- }
- void rangeApply(int p, int l, int r, int x, int y, const Tag &v) {
- if (l >= y || r <= x) {
- return;
- }
- if (l >= x && r <= y) {
- apply(p, v);
- return;
- }
- int m = (l + r) / 2;
- push(p);
- rangeApply(2 * p, l, m, x, y, v);
- rangeApply(2 * p + 1, m, r, x, y, v);
- pull(p);
- }
- void rangeApply(int l, int r, const Tag &v) {
- return rangeApply(1, 0, n, l, r, v);
- }
- int search(int p, int l, int r, int x, int y, i64 v) {
- if (l >= y || r <= x) return y;
- if (info[p].min >= v) return y;
- if (r - l == 1) return l;
- int m = (l + r) / 2;
- push(p);
- int res = search(2 * p, l, m, x, y, v);
- if (res == y) res = search(2 * p + 1, m, r, x, y, v);
- return res;
- }
- int search(int l, int r, i64 v) {
- return search(1, 0, n, l, r, v);
- }
- };
- constexpr i64 inf = 1E18;
- struct Tag {
- int rev;
- Tag(int _rev = 0) : rev{_rev} {}
- void apply(const Tag &t) {
- if (t.rev) {
- rev ^= 1;
- }
- }
- };
- struct Info {
- int up;
- int down;
- Info() : up{0}, down{0} {}
- Info(int _up, int _down) : up{_up}, down{_down} {}
- void apply(const Tag &t) {
- if (t.rev) {
- swap(up, down);
- }
- }
- };
- Info operator+(const Info &a, const Info &b) {
- Info c;
- c.up = a.up + b.up;
- c.down = a.down + b.down;
- return c;
- }
- class Solution {
- public:
- vector<long long> handleQuery(vector<int>& nums1, vector<int>& nums2, vector<vector<int>>& queries) {
- vector<Info> info;
- for (int i = 0; i < (int) nums1.size(); i++) {
- info.push_back(Info(nums1[i], nums1[i] ^ 1));
- }
- long long s = accumulate(nums2.begin(), nums2.end(), 0LL);
- vector<long long> ans;
- LazySegmentTree<Info, Tag> seg(info);
- for (auto &qs: queries) {
- int op = qs[0];
- if (op == 1) {
- seg.rangeApply(qs[1], qs[2] + 1, Tag(1));
- } else if (op == 2) {
- s += seg.info[1].up * (long long) qs[1];
- } else {
- ans.push_back(s);
- }
- }
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement