This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
const int N = 1 << 19;
struct { bool ask; int l, r, t, del; } a[4 * N], b[4 * N];
int q, ans[N];
void cdq2(int l, int r) {
if (r - l == 1) return;
int m = (l + r) >> 1;
cdq2(l, m); cdq2(m, r);
rep(i, l, m) b[i].r = 0; rep(i, m, r) b[i].r = 1;
inplace_merge(b + l, b + m, b + r, [&](const auto &a, const auto &b) { return a.t < b.t; });
int s = 0;
rep(i, l, r) {
if (!b[i].ask && !b[i].l && !b[i].r) s += b[i].del * (q - b[i].t - 1);
if (b[i].ask && b[i].l && b[i].r) ans[b[i].t] += s;
}
}
void cdq1(int l, int r) {
if (r - l == 1) return;
int m = (l + r) >> 1;
cdq1(l, m); cdq1(m, r);
rep(i, l, m) a[i].l = 0; rep(i, m, r) a[i].l = 1;
merge(a + l, a + m, a + m, a + r, b + l, [&](const auto &a, const auto &b) { return pair(-a.r, a.ask) < pair(-b.r, b.ask); });
rep(i, l, r) a[i] = b[i];
cdq2(l, r);
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, p = 0; cin >> n >> q; int pre = -1;
set<int> zeroes = {-1};
rep(i, 0, n) {
char c; cin >> c;
if (c != '0') {
if (p && a[p - 1].r == i) a[p - 1].r = i + 1;
else a[p++] = {0, pre + 1, i + 1, -1, +1};
} else zeroes.insert(pre = i);
}
zeroes.insert(n);
rep(t, 0, q) {
string s; cin >> s;
if (s == "toggle") {
int i; cin >> i; --i;
auto [it, f] = zeroes.insert(i);
int d = f ? -1 : +1;
a[p++] = {0, *prev(it) + 1, *next(it), t, +d};
a[p++] = {0, *prev(it) + 1, i, t, -d};
a[p++] = {0, i + 1, *next(it), t, -d};
if (d > 0) zeroes.erase(it);
ans[t] = -1;
} else {
int l, r; cin >> l >> r; --l; --r;
a[p++] = {1, l, r, t, 0};
if (r <= *zeroes.lower_bound(l)) ans[t] -= q - t - 1;
}
}
sort(a, a + p, [&](const auto &a, const auto &b) { return pair(a.l, a.ask) < pair(b.l, b.ask); });
cdq1(0, p);
rep(t, 0, q) if (0 <= ans[t]) cout << ans[t] << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |