#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <cstring>
#include <set>
#include <functional>
#warning That's not the baby, that's my baby
#define debug(x) #x << " = " << x << '\n'
using ll = long long;
const int INF = 1e9;
const int NMAX = 3e5;
struct Query {
int x, y;
};
std::string s;
std::set<int> lt;
Query Q[NMAX + 1];
int answer[NMAX + 1];
int rt[NMAX + 1];
int tt[NMAX + 1];
std::vector<std::tuple<int, int, int>> events;
int n;
void finish(int l, int t) {
events.push_back({l, rt[l], t - tt[l]});
tt[l] = t;
}
void printLeft() {
std::cout << "\nLEFT POINTS:\n";
for (const auto &p : lt) {
std::cout << p << ' ';
}
std::cout << '\n';
}
void toggle(int pos, int t) {
// std::cout << "\n\n\n";
// std::cout << debug(t);
// printLeft();
if (s[pos] == 0) {
auto it = lt.lower_bound(pos);
int st = *prev(it);
int dr = *it;
if (st != -1 && dr == pos + 1 && rt[st] == pos - 1) {
finish(st, t);
finish(dr, t);
lt.erase(dr);
rt[st] = rt[dr];
} else if (dr == pos + 1) {
finish(dr, t);
lt.erase(dr);
lt.insert(pos);
rt[pos] = rt[dr];
tt[pos] = t;
} else if (st != -1 && rt[st] == pos - 1) {
finish(st, t);
rt[st] = pos;
} else {
rt[pos] = pos;
lt.insert(pos);
tt[pos] = t;
}
} else {
// std::cout << debug(t);
// std::cout << debug(pos);
// printLeft();
// if (t == 4) {
// std::cout << "HERE\n";
// }
auto it = lt.upper_bound(pos);
it = prev(it);
auto me = *it;
finish(me, t);
if (pos != rt[me]) {
lt.insert(pos + 1);
rt[pos + 1] = rt[me];
tt[pos + 1] = t;
}
if (me == pos) {
lt.erase(pos);
} else {
rt[me] = pos - 1;
tt[me] = t;
}
}
}
void query(int l, int r, int t) {
events.push_back({l, r, -(t + 1)});
int me = *std::prev(lt.upper_bound(l));
if (me != -1 && rt[me] >= r) {
answer[t] += t - tt[me];
}
}
std::vector<int> ys[NMAX + 1];
std::vector<ll> aib[NMAX + 1];
void updateDry(int x, int y, int v = 0) {
for (; x <= NMAX; x += x & -x) {
ys[x].push_back(y);
}
}
void queryDry(int x, int y) {
for (; x > 0; x -= x & -x) {
ys[x].push_back(y);
}
}
void build() {
for (int i = 1; i <= NMAX; i++) {
ys[i].push_back(0);
ys[i].push_back(NMAX + 1);
std::sort(ys[i].begin(), ys[i].end());
ys[i].erase(std::unique(ys[i].begin(), ys[i].end()), ys[i].end());
aib[i].resize((int) ys[i].size() + 1, 0);
}
}
void updateWet(int x, int y, int v) {
for (; x <= NMAX; x += x & -x) {
for (int j = std::lower_bound(ys[x].begin(), ys[x].end(), y) - ys[x].begin(); j > 0; j -= j & -j) {
aib[x][j] += v;
}
}
}
int queryWet(int x, int y) {
int ret = 0;
for (; x > 0; x -= x & -x) {
for (int j = std::lower_bound(ys[x].begin(), ys[x].end(), y) - ys[x].begin(); j <= (int) ys[x].size(); j += j & -j) {
ret += aib[x][j];
}
}
return ret;
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int q;
std::cin >> n >> q;
std::cin >> s;
s = '$' + s;
lt.insert(-1);
lt.insert(-2);
lt.insert(n + 2);
lt.insert(n + 3);
for (int i = 1; i <= n; i++) {
s[i] -= '0';
if (s[i] == 1) {
s[i] ^= 1;
toggle(i, 0);
s[i] ^= 1;
}
}
for (int i = 1; i <= q; i++) {
std::string type;
std::cin >> type;
if (type == "toggle") {
std::cin >> Q[i].x;
Q[i].y = -1;
answer[i] = -1;
toggle(Q[i].x, i);
s[Q[i].x] ^= 1;
} else {
std::cin >> Q[i].x >> Q[i].y;
Q[i].y--;
answer[i] = 0;
query(Q[i].x, Q[i].y, i);
}
// std::cout << "PASS\n";
}
for (const auto &[l, r, t] : events) {
if (t >= 0) { // update
updateDry(l, r, t);
} else { // query
queryDry(l, r);
}
}
build();
for (const auto &[l, r, t] : events) {
if (t >= 0) {
// std::cout << "update " << l << ' ' << r << ' ' << t << '\n';
updateWet(l, r, t);
} else {
assert(1 <= -(t + 1) && -(t + 1) <= q);
answer[-(t + 1)] += queryWet(l, r);
}
}
for (int i = 1; i <= q; i++) {
if (answer[i] != -1) {
std::cout << answer[i] << '\n';
}
}
return 0;
}
Compilation message
street_lamps.cpp:8:2: warning: #warning That's not the baby, that's my baby [-Wcpp]
8 | #warning That's not the baby, that's my baby
| ^~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
33372 KB |
Output is correct |
2 |
Correct |
37 ms |
33144 KB |
Output is correct |
3 |
Correct |
38 ms |
33160 KB |
Output is correct |
4 |
Correct |
40 ms |
33304 KB |
Output is correct |
5 |
Correct |
37 ms |
33360 KB |
Output is correct |
6 |
Correct |
39 ms |
33108 KB |
Output is correct |
7 |
Correct |
39 ms |
33360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
273 ms |
58548 KB |
Output is correct |
2 |
Correct |
302 ms |
58256 KB |
Output is correct |
3 |
Correct |
426 ms |
59336 KB |
Output is correct |
4 |
Correct |
858 ms |
92064 KB |
Output is correct |
5 |
Correct |
1065 ms |
98728 KB |
Output is correct |
6 |
Correct |
885 ms |
91560 KB |
Output is correct |
7 |
Correct |
665 ms |
74420 KB |
Output is correct |
8 |
Correct |
1223 ms |
151216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
33372 KB |
Output is correct |
2 |
Correct |
38 ms |
33372 KB |
Output is correct |
3 |
Correct |
37 ms |
33364 KB |
Output is correct |
4 |
Correct |
40 ms |
33636 KB |
Output is correct |
5 |
Correct |
715 ms |
77996 KB |
Output is correct |
6 |
Correct |
877 ms |
88232 KB |
Output is correct |
7 |
Correct |
1056 ms |
101996 KB |
Output is correct |
8 |
Correct |
1149 ms |
155800 KB |
Output is correct |
9 |
Correct |
125 ms |
46016 KB |
Output is correct |
10 |
Correct |
134 ms |
47132 KB |
Output is correct |
11 |
Correct |
137 ms |
47540 KB |
Output is correct |
12 |
Correct |
633 ms |
79272 KB |
Output is correct |
13 |
Correct |
1139 ms |
155660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
33360 KB |
Output is correct |
2 |
Correct |
40 ms |
33448 KB |
Output is correct |
3 |
Correct |
41 ms |
33360 KB |
Output is correct |
4 |
Correct |
40 ms |
33360 KB |
Output is correct |
5 |
Correct |
810 ms |
95664 KB |
Output is correct |
6 |
Correct |
818 ms |
94120 KB |
Output is correct |
7 |
Correct |
814 ms |
91336 KB |
Output is correct |
8 |
Correct |
883 ms |
87732 KB |
Output is correct |
9 |
Correct |
343 ms |
60472 KB |
Output is correct |
10 |
Correct |
363 ms |
66484 KB |
Output is correct |
11 |
Correct |
344 ms |
60592 KB |
Output is correct |
12 |
Correct |
335 ms |
66524 KB |
Output is correct |
13 |
Correct |
344 ms |
60520 KB |
Output is correct |
14 |
Correct |
343 ms |
66740 KB |
Output is correct |
15 |
Correct |
622 ms |
79308 KB |
Output is correct |
16 |
Correct |
1148 ms |
155676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
33372 KB |
Output is correct |
2 |
Correct |
37 ms |
33144 KB |
Output is correct |
3 |
Correct |
38 ms |
33160 KB |
Output is correct |
4 |
Correct |
40 ms |
33304 KB |
Output is correct |
5 |
Correct |
37 ms |
33360 KB |
Output is correct |
6 |
Correct |
39 ms |
33108 KB |
Output is correct |
7 |
Correct |
39 ms |
33360 KB |
Output is correct |
8 |
Correct |
273 ms |
58548 KB |
Output is correct |
9 |
Correct |
302 ms |
58256 KB |
Output is correct |
10 |
Correct |
426 ms |
59336 KB |
Output is correct |
11 |
Correct |
858 ms |
92064 KB |
Output is correct |
12 |
Correct |
1065 ms |
98728 KB |
Output is correct |
13 |
Correct |
885 ms |
91560 KB |
Output is correct |
14 |
Correct |
665 ms |
74420 KB |
Output is correct |
15 |
Correct |
1223 ms |
151216 KB |
Output is correct |
16 |
Correct |
40 ms |
33372 KB |
Output is correct |
17 |
Correct |
38 ms |
33372 KB |
Output is correct |
18 |
Correct |
37 ms |
33364 KB |
Output is correct |
19 |
Correct |
40 ms |
33636 KB |
Output is correct |
20 |
Correct |
715 ms |
77996 KB |
Output is correct |
21 |
Correct |
877 ms |
88232 KB |
Output is correct |
22 |
Correct |
1056 ms |
101996 KB |
Output is correct |
23 |
Correct |
1149 ms |
155800 KB |
Output is correct |
24 |
Correct |
125 ms |
46016 KB |
Output is correct |
25 |
Correct |
134 ms |
47132 KB |
Output is correct |
26 |
Correct |
137 ms |
47540 KB |
Output is correct |
27 |
Correct |
633 ms |
79272 KB |
Output is correct |
28 |
Correct |
1139 ms |
155660 KB |
Output is correct |
29 |
Correct |
37 ms |
33360 KB |
Output is correct |
30 |
Correct |
40 ms |
33448 KB |
Output is correct |
31 |
Correct |
41 ms |
33360 KB |
Output is correct |
32 |
Correct |
40 ms |
33360 KB |
Output is correct |
33 |
Correct |
810 ms |
95664 KB |
Output is correct |
34 |
Correct |
818 ms |
94120 KB |
Output is correct |
35 |
Correct |
814 ms |
91336 KB |
Output is correct |
36 |
Correct |
883 ms |
87732 KB |
Output is correct |
37 |
Correct |
343 ms |
60472 KB |
Output is correct |
38 |
Correct |
363 ms |
66484 KB |
Output is correct |
39 |
Correct |
344 ms |
60592 KB |
Output is correct |
40 |
Correct |
335 ms |
66524 KB |
Output is correct |
41 |
Correct |
344 ms |
60520 KB |
Output is correct |
42 |
Correct |
343 ms |
66740 KB |
Output is correct |
43 |
Correct |
622 ms |
79308 KB |
Output is correct |
44 |
Correct |
1148 ms |
155676 KB |
Output is correct |
45 |
Correct |
148 ms |
48828 KB |
Output is correct |
46 |
Correct |
189 ms |
49156 KB |
Output is correct |
47 |
Correct |
478 ms |
65808 KB |
Output is correct |
48 |
Correct |
835 ms |
92420 KB |
Output is correct |
49 |
Correct |
155 ms |
48732 KB |
Output is correct |
50 |
Correct |
140 ms |
48564 KB |
Output is correct |
51 |
Correct |
169 ms |
49208 KB |
Output is correct |
52 |
Correct |
177 ms |
51632 KB |
Output is correct |
53 |
Correct |
181 ms |
51632 KB |
Output is correct |
54 |
Correct |
184 ms |
51644 KB |
Output is correct |