有n盞燈,從左到右編號依次為1~n,有m次操作:
- 1 a b,表示修改區間[a,b]內燈的狀態。
- 2 a b,查詢區間[a,b]內有多少盞燈是開啟的。
初始時所有燈都是關著的。
分析:線段樹維護區間內開啟燈的數目,涉及到區間更新,要用懶標記。
#include <bits/stdc++.h>
using llong = 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> v) {
init(v);
}
void init(int _n, Info v=Info()) {
init(std::vector<Info>(_n, v));
}
template<class T>
void init(std::vector<T> v) {
n = v.size();
info.assign(4 << std::__lg(n), Info());
tag.assign(4 << std::__lg(n), Tag());
std::function<void(int,int,int)> build = [&](int x, int l, int r) {
if (l + 1 == r) {
info[x] = v[l]; // fixme
return;
}
int m = (l + r) / 2;
build(2*x+1, l, m);
build(2*x+2, m, r);
pushup(x);
};
build(0, 0, n);
}
void pushup(int x) {
info[x] = info[2*x+1] + info[2*x+2];
}
void apply(int x, const Tag &t) {
info[x].apply(t);
tag[x].apply(t);
}
void pushdown(int x) {
apply(2*x+1, tag[x]);
apply(2*x+2, tag[x]);
tag[x] = Tag();
}
void modify(int x, int l, int r, int p, const Info &v) {
if (l + 1 == r) {
info[x] = v; //fixme
return;
}
int m = (l + r) / 2;
pushdown(x);
if (p < m) {
modify(2*x+1, l, m, p, v);
} else {
modify(2*x+2, m, r, p, v);
}
pushup(x);
}
void modify(int p, const Info &v) {
modify(0, 0, n, p, v);
}
Info rangeQuery(int x, int l, int r, int L, int R) {
if (R <= l || r <= L) {
return Info();
}
if (L <= l && r <= R) {
return info[x];
}
int m = (l + r) / 2;
pushdown(x);
return rangeQuery(2*x+1, l, m, L, R) + rangeQuery(2*x+2, m, r, L, R);
}
Info rangeQuery(int L, int R) {
return rangeQuery(0, 0, n, L, R);
}
void rangeApply(int x, int l, int r, int L, int R, const Tag &t) {
if (R <= l || r <= L) {
return;
}
if (L <= l && r <= R) {
apply(x, t);
return;
}
int m = (l + r) / 2;
pushdown(x);
rangeApply(2*x+1, l, m, L, R, t);
rangeApply(2*x+2, m, r, L, R, t);
pushup(x);
}
void rangeApply(int L, int R, const Tag &t) {
return rangeApply(0, 0, n, L, R, t);
}
template<class F>
int findFirst(int x, int l, int r, int L, int R, F && pred) {
if (R <= l || r <= L) {
return -1;
}
if (L <= l && r <= R && !pred(info[x])) {
return -1;
}
if (l + 1 == r) {
return l;
}
int m = (l + r) / 2;
pushdown(x);
int res = findFirst(2*x+1, l, m, L, R, pred);
if (res == -1) {
res = findFirst(2*x+2, m, r, L, R, pred);
}
return res;
}
template<class F>
int findFirst(int L, int R, F &&pred) {
return findFirst(0, 0, n, L, R, pred);
}
template<class F>
int findLast(int x, int l, int r, int L, int R, F &&pred) {
if (R <= l || r <= L) {
return -1;
}
if (L <= l && r <= R && !pred(info[x])) {
return -1;
}
if (l + 1 == r) {
return l;
}
int m = (l + r) / 2;
pushdown(x);
int res = findLast(2*x+2, m, r, L, R, pred);
if (res == -1) {
res = findLast(2*x+1, l, m, L, R, pred);
}
return res;
}
template<class F>
int findLast(int L, int R, F &&pred) {
return findLast(0, 0, n, L, R, pred);
}
void dfs(int x, int l, int r) {
std::cerr << x << "(" << l << "," << r << "): " << info[x] << " " << tag[x] << "\n";
if (l + 1 != r) {
int m = (l + r) / 2;
dfs(2*x+1, l, m);
dfs(2*x+2, m, r);
}
}
void dfs() {
std::cerr << "========================\n";
dfs(0, 0, n);
}
};
struct Tag {
int flip;
Tag(int t=0):flip(t) {}
void apply(Tag t) {
flip ^= t.flip;
}
friend std::ostream& operator<<(std::ostream &out, Tag &tag) {
out << "tag:(flip:" << tag.flip << ")";
return out;
}
};
struct Info {
int cnt;
int len;
Info(int s=0):cnt(s),len(1) {}
void apply(Tag t) {
if (t.flip) {
cnt = len - cnt;
}
}
friend Info operator+(const Info &a, const Info &b) {
Info ans;
ans.cnt = a.cnt + b.cnt;
ans.len = a.len + b.len;
return ans;
}
friend std::ostream& operator<<(std::ostream &out, Info &info) {
out << "info:(cnt:" << info.cnt << ",len:" << info.len << ")";
return out;
}
};
void solve() {
int n, m;
std::cin >> n >> m;
std::vector<int> A(n);
LazySegmentTree<Info,Tag> tr(A);
for (int i = 0; i < m; i++) {
int c, a, b;
std::cin >> c >> a >> b;
if (c == 0) {
tr.rangeApply(a-1, b, Tag(1));
} else if (c == 1) {
std::cout << tr.rangeQuery(a-1, b).cnt << "\n";
}
}
}
int main() {
std::cin.tie(0)->sync_with_stdio(0);
int t = 1;
while (t--) solve();
return 0;
}