题意:有两种操作,操作1是将一个区间内的0和1做flip操作,操作2是查询一个区间内的连续的1的个数。
首先,对于操作1的flip操作我们很容易想到给每个数异或一下。
对于操作2我们考虑如何维护两个节点的信息,新节点的答案有三种可能的情况:左儿子的前缀,右儿子的后缀以及左儿子的后缀和右儿子的前缀。
所以一定要维护前缀和后缀的值,但其实考虑到这还不够,因为当左儿子的区间中全部为1时,新节点的前缀就变成了整个左儿子部分与右儿子的前缀相加,所以还需要有一个值来维护这个区间是否全为1。
因此pre\suf\sum\ans
就成了我们必须要维护的信息。
对于异或操作,我们其实可以存储一份反转区间的情况,因此每次懒标记上有信息的时候只要交换一下信息就好了。
#include <bits/stdc++.h>
using i64 = long long;
template<class Info, class Tag>
struct LazySegmentTree {
int n;
std::vector<Info> info;
std::vector<Tag> tag;
LazySegmentTree() : n(0) {}
LazySegmentTree(int n_, Info v_ = Info()) {
init(n_, v_);
}
template<class T>
LazySegmentTree(std::vector<T> init_) {
init(init_);
}
void init(int n_, Info v_ = Info()) {
init(std::vector(n_, v_));
}
template<class T>
void init(std::vector<T> init_) {
n = init_.size();
info.assign(4 << std::__lg(n), Info());
tag.assign(4 << std::__lg(n), Tag());
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);
}
};
constexpr int inf = 1E9;
struct Tag {
int tag = 0;
void apply(const Tag &t) {
if (t.tag != -1) {
if (tag == -1) {
tag = 0;
}
tag ^= t.tag;
}
}
};
struct Info {
i64 pre[2] = {0, 0};
i64 suf[2] = {0, 0};
i64 ans[2] = {0, 0};
i64 sum[2] = {0, 0};
void apply(const Tag &t) {
if (t.tag == 1) {
std::swap(pre[0], pre[1]);
std::swap(suf[0], suf[1]);
std::swap(ans[0], ans[1]);
std::swap(sum[0], sum[1]);
}
}
};
Info operator+(const Info &a, const Info &b) {
Info c;
for (int t = 0; t < 2; t++) {
c.pre[t] = std::max(a.pre[t] , a.sum[t] + b.pre[t]);
c.suf[t] = std::max(b.suf[t], a.suf[t] + b.sum[t]);
c.sum[t] = a.sum[t] + b.sum[t];
c.ans[t] = std::max({a.ans[t], b.ans[t], a.suf[t] + b.pre[t]});
}
return c;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int N, Q;
std::cin >> N >> Q;
std::string s;
std::cin >> s;
std::vector<Info> a(N);
for (int i = 0; i < N; i++) {
a[i] = {{1, 0}, {1, 0}, {1, 0}, {1, -inf}};
if (s[i] == '0') {
a[i].apply({1});
}
}
LazySegmentTree<Info, Tag> seg(a);
// for (int i = 0; i < N; i++) {
// std::cout << seg.rangeQuery(i, i + 1).ans[0] << "\n";
// }
while (Q--) {
int c, L, R;
std::cin >> c >> L >> R;
L--;
if (c == 1) {
seg.rangeApply(L, R, {1});
} else {
std::cout << seg.rangeQuery(L, R).ans[0] << "\n";
}
}
return 0;
}