#include <bits/stdc++.h>
#define nl '\n'
#ifdef LOCAL
#include "template/debug.hpp"
#else
#define dbg(...) ;
#define timer(...) ;
#endif
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, q;
std::cin >> n >> q;
std::string s;
std::cin >> s;
std::vector<std::set<int>> adj(n + 1);
std::map<std::pair<int, int>, int> when;
std::vector<std::array<int, 3>> queries(q + 1);
std::vector<std::pair<int, int>> toggle_on;
for (int i = 1; i <= q; i++) {
std::string com;
std::cin >> com;
queries[i][0] = com == "toggle";
if (queries[i][0]) {
int id;
std::cin >> id;
queries[i][1] = --id;
toggle_on.emplace_back(id, i);
} else {
int a,b ;
std::cin >> a >> b;
queries[i][1] = --a;
queries[i][2] = --b;
adj[a].insert(b);
adj[b].insert(a);
}
}
std::vector<std::set<int>> elem(n + 1);
std::vector<int> e(n + 1, -1);
for (int i = 0; i <= n; i++) {
elem[i].insert(i);
}
std::function<int(int)> find = [&](int u) {
return e[u] < 0 ? u : e[u] = find(e[u]);
};
auto mark = [&](int x, int y, int t) {
if (x > y) std::swap(x, y);
when[{x, y}] = t;
};
auto join = [&](int t, int u, int v) {
u = find(u);
v = find(v);
if (e[u] > e[v]) std::swap(u, v);
e[u] += e[v];
e[v] = u;
int lu = *elem[u].begin();
int ru = *elem[u].rbegin();
for (int x : elem[v]) {
auto it = adj[x].lower_bound(lu);
while (it != adj[x].end() && *it <= ru) {
int y = *it;
mark(x, y, t);
adj[y].erase(x);
it = adj[x].erase(it);
}
}
elem[u].insert(elem[v].begin(), elem[v].end());
elem[v].clear();
};
for (int i = 0; i < n; i++) {
if (s[i] == '1') join(0, i, i + 1);
}
for (auto [id, t] : toggle_on) {
join(t, id, id + 1);
}
dbg(when);
for (int tt = 1; tt <= q; tt++) {
if (queries[tt][0] == 0) {
auto [x, y, z] = queries[tt];
int start = when[{y, z}];
if (tt < start) {
std::cout << 0 << nl;
} else {
std::cout << tt - start << nl;
}
}
}
}
/*
5 7
11011
query 3 4
toggle 3
query 3 4
query 1 2
toggle 1
query 3 4
query 1 2
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
412 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
341 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
568 ms |
59412 KB |
Output is correct |
6 |
Correct |
709 ms |
67032 KB |
Output is correct |
7 |
Correct |
894 ms |
73672 KB |
Output is correct |
8 |
Correct |
838 ms |
83708 KB |
Output is correct |
9 |
Correct |
79 ms |
7356 KB |
Output is correct |
10 |
Correct |
83 ms |
7864 KB |
Output is correct |
11 |
Correct |
86 ms |
8532 KB |
Output is correct |
12 |
Incorrect |
451 ms |
102508 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
412 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |