[题目通道]([TJOI2009] 开关 - 洛谷)
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int a[N], b[N], tag[N], ans[N];
int n, m, sq;
void upd(const int &x, const int &y) {
for (int i = x; i <= min(y, b[x] * sq); ++i) {
ans[b[x]] -= (a[i] ^ tag[b[x]]);
a[i] ^= 1;
ans[b[x]] += (a[i] ^ tag[b[x]]);
}
if (b[x] != b[y]) {
for (int i = (b[y] - 1) * sq + 1; i <= y; ++i) {
ans[b[y]] -= (a[i] ^ tag[b[y]]);
a[i] ^= 1;
ans[b[y]] += (a[i] ^ tag[b[y]]);
}
}
for (int i = b[x] + 1; i <= b[y] - 1; ++i) {
tag[i] ^= 1;
ans[i] = sq - ans[i];
}
}
int query(const int x, const int &y) {
int res = 0;
for (int i = x; i <= min(y, b[x] * sq); ++i) {
res += (a[i] ^ tag[b[x]]);
}
if (b[x] != b[y]) {
for (int i = (b[y] - 1) * sq + 1; i <= y; ++i) {
res += (a[i] ^ tag[b[y]]);
}
}
for (int i = b[x] + 1; i <= b[y] - 1; ++i) {
res += ans[i];
}
return res;
}
int main() {
cin >> n >> m;
sq = sqrt(n);
for (int i = 1; i <= n; ++i) {
b[i] = (i - 1) / sq + 1;
}
for (int i = 1, t, x, y; i <= m; ++i) {
cin >> t >> x >> y;
if (t == 0) {
upd(x, y);
} else {
cout << query(x, y) << endl;
}
}
return 0;
}