From e2f644516efa2bbb76ec26c8d784cb7c6a1371ce Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Sat, 3 Jul 2021 22:06:12 +0800 Subject: [PATCH] =?UTF-8?q?P3870=20[TJOI2009]=E5=BC=80=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit R52356783 --- Luogu/problem/P3870/P3870.cpp | 79 +++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 Luogu/problem/P3870/P3870.cpp diff --git a/Luogu/problem/P3870/P3870.cpp b/Luogu/problem/P3870/P3870.cpp new file mode 100644 index 00000000..a93bdbbf --- /dev/null +++ b/Luogu/problem/P3870/P3870.cpp @@ -0,0 +1,79 @@ +#include + +using namespace std; + +struct node { + int l, r, s, d; + + node() { + l = r = s = d = 0; + } + + node(int _l, int _r) { + l = _l; + r = _r; + s = d = 0; + } +} tr[100005 << 2]; +int n, m, op, a, b; + +void pushup(int u) { + tr[u].s = tr[u << 1].s + tr[u << 1 | 1].s; +} + +void pushdown(int u) { + if (!tr[u].d) return; + tr[u << 1].d = !tr[u << 1].d; + tr[u << 1 | 1].d = !tr[u << 1 | 1].d; + tr[u << 1].s = tr[u << 1].r - tr[u << 1].l + 1 - tr[u << 1].s; + tr[u << 1 | 1].s = tr[u << 1 | 1].r - tr[u << 1 | 1].l + 1 - tr[u << 1 | 1].s; + tr[u].d = 0; +} + +void build(int u, int l, int r) { + tr[u] = node(l, r); + if (l == r) return; + int mid = l + r >> 1; + build(u << 1, l, mid); + build(u << 1 | 1, mid + 1, r); + pushup(u); +} + +void modify(int u, int l, int r) { + if (l <= tr[u].l && tr[u].r <= r) { + tr[u].d = !tr[u].d; + tr[u].s = tr[u].r - tr[u].l + 1 - tr[u].s; + return; + } + int mid = tr[u].l + tr[u].r >> 1; + pushdown(u); + if (l <= mid) modify(u << 1, l, r); + if (r > mid) modify(u << 1 | 1, l, r); + pushup(u); +} + +int query(int u, int l, int r) { + if (l <= tr[u].l && tr[u].r <= r) { + return tr[u].s; + } + int mid = tr[u].l + tr[u].r >> 1; + int s = 0; + pushdown(u); + if (l <= mid) s += query(u << 1, l, r); + if (r > mid) s += query(u << 1 | 1, l, r); + return s; +} + +int main() { + cin >> n >> m; + build(1, 1, n); + for (int i = 0; i < m; i++) { + cin >> op >> a >> b; + if (op == 0) { + modify(1, a, b); + } else if (op == 1) { + cout << query(1, a, b) << endl; + } + } + return 0; +}