#include <iostream>
#include <set>
#include <unordered_map>
#include <ext/pb_ds/assoc_container.hpp>
#define pii pair<int, int>
#define ppi pair<pii, int>
#define fst first
#define snd second
using namespace std;
using namespace __gnu_pbds;
struct chash {
const int RANDOM = 6969420;
static unsigned long long hash_f(unsigned long long x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
static unsigned hash_combine(unsigned a, unsigned b) { return a * 31 + b; }
int operator()(const pii& x) const { return (hash_f(x.fst) * 31 + hash_f(x.snd))^RANDOM; }
};
gp_hash_table<pii, int, chash> fwk;
int n, q;
string ar;
set<ppi> s;
inline void upd(int i, int j, int v)
{
for (; i <= n; i += i & (-i))
{
for (int k = j; k <= n; k += k & (-k))
{
fwk[make_pair(i, k)] += v;
}
}
}
inline int qry(int i, int j)
{
int ret = 0;
for (; i; i -= i & (-i))
{
for (int k = j; k; k -= k & (-k))
{
ret += fwk[make_pair(i, k)];
}
}
return ret;
}
int main()
{
ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> q;
cin >> ar;
int p = -1;
for (int i = 0; i < n; i++)
{
if (ar[i] == '0')
{
if (p >= 0) s.insert({{p + 1, i}, 0});
p = -1;
}
else if (p == -1) {p = i;}
}
if (p >= 0) {s.insert({{p + 1, n}, 0});}
//for (auto x : s) cout << x.fst.fst << ", " << x.fst.snd << "\n";
for (int i = 1; i <= q; i++)
{
string qq; cin >> qq;
if (qq == "toggle")
{
int x; cin >> x;
if (ar[x - 1] == '1')
{
auto it = prev(s.upper_bound(make_pair(make_pair(x + 1, -1), -1)));
ppi dt = *it; s.erase(it);
upd(dt.fst.fst, n + 1 - dt.fst.snd, i - dt.snd);
if (dt.fst.fst < x) s.insert(make_pair(make_pair(dt.fst.fst, x - 1), i));
if (x < dt.fst.snd) s.insert(make_pair(make_pair(x + 1, dt.fst.snd), i));
ar[x - 1] = '0';
}
else
{
int l = x, r = x;
auto it = s.upper_bound(make_pair(make_pair(x + 1, -1), -1));
if (it != s.end() && it -> fst.fst == r + 1)
{
r = it -> fst.snd;
upd(it -> fst.fst, n + 1 - it -> fst.snd, i - it -> snd);
it = s.erase(it);
}
if (it != s.begin())
{
it = prev(it);
if (it -> fst.snd == l - 1)
{
l = it -> fst.fst;
upd(it -> fst.fst, n + 1 - it -> fst.snd, i - it -> snd);
it = s.erase(it);
}
}
s.insert(make_pair(make_pair(l, r), i));
ar[x - 1] = '1';
}
}
else
{
int l, r, res = 0; cin >> l >> r; r--;
if (ar[l - 1] == '1')
{
auto it = prev(s.upper_bound(make_pair(make_pair(l + 1, -1), -1)));
if (it -> fst.snd >= r) res += i - it -> snd;
}
//cout << res << "\n";
res += qry(l, n + 1 - r);
cout << res << "\n";
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
162 ms |
4596 KB |
Output is correct |
2 |
Correct |
201 ms |
5240 KB |
Output is correct |
3 |
Correct |
422 ms |
11660 KB |
Output is correct |
4 |
Correct |
2099 ms |
405344 KB |
Output is correct |
5 |
Correct |
2310 ms |
405616 KB |
Output is correct |
6 |
Correct |
2105 ms |
404904 KB |
Output is correct |
7 |
Correct |
1325 ms |
204316 KB |
Output is correct |
8 |
Correct |
1350 ms |
205632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
896 KB |
Output is correct |
2 |
Correct |
3 ms |
1284 KB |
Output is correct |
3 |
Correct |
3 ms |
1280 KB |
Output is correct |
4 |
Correct |
3 ms |
1280 KB |
Output is correct |
5 |
Correct |
2155 ms |
403724 KB |
Output is correct |
6 |
Runtime error |
2368 ms |
524292 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1280 KB |
Output is correct |
2 |
Correct |
3 ms |
1280 KB |
Output is correct |
3 |
Correct |
4 ms |
1284 KB |
Output is correct |
4 |
Correct |
2 ms |
896 KB |
Output is correct |
5 |
Runtime error |
1559 ms |
524292 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
162 ms |
4596 KB |
Output is correct |
9 |
Correct |
201 ms |
5240 KB |
Output is correct |
10 |
Correct |
422 ms |
11660 KB |
Output is correct |
11 |
Correct |
2099 ms |
405344 KB |
Output is correct |
12 |
Correct |
2310 ms |
405616 KB |
Output is correct |
13 |
Correct |
2105 ms |
404904 KB |
Output is correct |
14 |
Correct |
1325 ms |
204316 KB |
Output is correct |
15 |
Correct |
1350 ms |
205632 KB |
Output is correct |
16 |
Correct |
3 ms |
896 KB |
Output is correct |
17 |
Correct |
3 ms |
1284 KB |
Output is correct |
18 |
Correct |
3 ms |
1280 KB |
Output is correct |
19 |
Correct |
3 ms |
1280 KB |
Output is correct |
20 |
Correct |
2155 ms |
403724 KB |
Output is correct |
21 |
Runtime error |
2368 ms |
524292 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
22 |
Halted |
0 ms |
0 KB |
- |