Submission #259958

#TimeUsernameProblemLanguageResultExecution timeMemory
259958islingrStreet Lamps (APIO19_street_lamps)C++17
100 / 100
4022 ms64948 KiB
#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[5 * N], b[5 * 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, *zeroes.rbegin() + 1, i, -1, -1}; a[p++] = {0, *zeroes.rbegin() + 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...