Submission #259986

# Submission time Handle Problem Language Result Execution time Memory
259986 2020-08-08T21:36:17 Z islingr Street Lamps (APIO19_street_lamps) C++17
100 / 100
4137 ms 60852 KB
#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, 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
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2503 ms 34024 KB Output is correct
2 Correct 2570 ms 33940 KB Output is correct
3 Correct 2667 ms 34268 KB Output is correct
4 Correct 3071 ms 45052 KB Output is correct
5 Correct 2964 ms 40068 KB Output is correct
6 Correct 3002 ms 48124 KB Output is correct
7 Correct 1445 ms 32020 KB Output is correct
8 Correct 1171 ms 17896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 512 KB Output is correct
2 Correct 5 ms 512 KB Output is correct
3 Correct 5 ms 512 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 3925 ms 59656 KB Output is correct
6 Correct 3291 ms 49756 KB Output is correct
7 Correct 2816 ms 40212 KB Output is correct
8 Correct 1434 ms 17944 KB Output is correct
9 Correct 794 ms 14512 KB Output is correct
10 Correct 869 ms 15840 KB Output is correct
11 Correct 834 ms 15716 KB Output is correct
12 Correct 1749 ms 31964 KB Output is correct
13 Correct 1481 ms 17720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 512 KB Output is correct
2 Correct 4 ms 512 KB Output is correct
3 Correct 5 ms 512 KB Output is correct
4 Correct 6 ms 512 KB Output is correct
5 Correct 1892 ms 29224 KB Output is correct
6 Correct 2506 ms 38512 KB Output is correct
7 Correct 3016 ms 48168 KB Output is correct
8 Correct 3978 ms 60852 KB Output is correct
9 Correct 3328 ms 41396 KB Output is correct
10 Correct 3761 ms 49496 KB Output is correct
11 Correct 3125 ms 41336 KB Output is correct
12 Correct 4137 ms 49476 KB Output is correct
13 Correct 3478 ms 41344 KB Output is correct
14 Correct 3574 ms 49476 KB Output is correct
15 Correct 1752 ms 31860 KB Output is correct
16 Correct 1686 ms 17708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 2503 ms 34024 KB Output is correct
9 Correct 2570 ms 33940 KB Output is correct
10 Correct 2667 ms 34268 KB Output is correct
11 Correct 3071 ms 45052 KB Output is correct
12 Correct 2964 ms 40068 KB Output is correct
13 Correct 3002 ms 48124 KB Output is correct
14 Correct 1445 ms 32020 KB Output is correct
15 Correct 1171 ms 17896 KB Output is correct
16 Correct 6 ms 512 KB Output is correct
17 Correct 5 ms 512 KB Output is correct
18 Correct 5 ms 512 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 3925 ms 59656 KB Output is correct
21 Correct 3291 ms 49756 KB Output is correct
22 Correct 2816 ms 40212 KB Output is correct
23 Correct 1434 ms 17944 KB Output is correct
24 Correct 794 ms 14512 KB Output is correct
25 Correct 869 ms 15840 KB Output is correct
26 Correct 834 ms 15716 KB Output is correct
27 Correct 1749 ms 31964 KB Output is correct
28 Correct 1481 ms 17720 KB Output is correct
29 Correct 5 ms 512 KB Output is correct
30 Correct 4 ms 512 KB Output is correct
31 Correct 5 ms 512 KB Output is correct
32 Correct 6 ms 512 KB Output is correct
33 Correct 1892 ms 29224 KB Output is correct
34 Correct 2506 ms 38512 KB Output is correct
35 Correct 3016 ms 48168 KB Output is correct
36 Correct 3978 ms 60852 KB Output is correct
37 Correct 3328 ms 41396 KB Output is correct
38 Correct 3761 ms 49496 KB Output is correct
39 Correct 3125 ms 41336 KB Output is correct
40 Correct 4137 ms 49476 KB Output is correct
41 Correct 3478 ms 41344 KB Output is correct
42 Correct 3574 ms 49476 KB Output is correct
43 Correct 1752 ms 31860 KB Output is correct
44 Correct 1686 ms 17708 KB Output is correct
45 Correct 1573 ms 22656 KB Output is correct
46 Correct 1640 ms 22748 KB Output is correct
47 Correct 2036 ms 26452 KB Output is correct
48 Correct 3763 ms 45000 KB Output is correct
49 Correct 1062 ms 17852 KB Output is correct
50 Correct 1312 ms 17776 KB Output is correct
51 Correct 1168 ms 17668 KB Output is correct
52 Correct 937 ms 17864 KB Output is correct
53 Correct 904 ms 17816 KB Output is correct
54 Correct 897 ms 17684 KB Output is correct