Submission #260106

#TimeUsernameProblemLanguageResultExecution timeMemory
260106islingrStreet Lamps (APIO19_street_lamps)C++17
100 / 100
3842 ms64632 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[3 * N], b[3 * 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, r) b[i].r = i < m; 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 * b[i].t; 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, r) a[i].l = i < m; 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> z = {-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 z.insert(pre = i); } z.insert(n); rep(t, 0, q) { string s; cin >> s; if (s == "toggle") { int i; cin >> i; --i; auto [it, f] = z.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) z.erase(it); ans[t] = -1; } else { int l, r; cin >> l >> r; --l; --r; a[p++] = {1, l, r, t, 0}; ans[t] = *z.lower_bound(l) < r ? 0 : t; } } 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...