#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define f first
#define s second
#define pb push_back
#define ep emplace
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define mem(f,x) memset(f , x , sizeof(f))
#define sz(x) (int)(x).size()
#define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b))
#define mxx *max_element
#define mnn *min_element
#define cntbit(x) __builtin_popcountll(x)
#define len(x) (int)(x.length())
const int N = 3e5 + 100;
struct fen{
const int N = 1e6 + 10;
vector <ll> f;
fen() {
f.assign(N, 0);
}
void upd(int i, ll x) {
for (; i < N; i += i & (-i))
f[i] += x;
}
ll get(int i) {
ll s = 0;
for (; i; i -= i & (-i))
s += f[i];
return s;
}
};
fen fsum, fneg, fpo;
vector < vector <int>> all;
ll ans[N];
bool cmp(vector <int>&a, vector <int>&b) {
if (a[0] == b[0]) {
if (abs(a[1] != abs(b[1]))) {
return abs(a[1]) < abs(b[1]);
}
} else {
return a[0] > b[0];
}
return a[2] > b[2];
}
void dnc(int l, int r) {
if (l >= r)
return;
int m = (l + r) / 2;
dnc(l, m);
dnc(m + 1, r);
vector < vector <int>> save;
for (int i = l; i <= m; i++) {
if (all[i].back() == 1) {
save.pb({all[i][1], all[i][2], all[i][3]});
}
}
for (int i = m + 1; i <= r; i++) {
if (all[i].back() == 0) {
save.pb({all[i][1], all[i][2], all[i][3]});
}
}
sort(all(save), cmp);
for (auto x : save) {
if (x.back() == 1) {
fsum.upd(abs(x[1]), x[1]);
if (x[1] >= 0) {
fpo.upd(abs(x[1]), 1);
} else {
fneg.upd(abs(x[1]), 1);
}
} else {
ll cur = fsum.get(x[1]);
if (fpo.get(x[1]) != fneg.get(x[1])) {
cur += x[1];
}
ans[x[1]] = cur;
}
}
for (auto x : save) {
if (x.back() == 1) {
fsum.upd(abs(x[1]), -x[1]);
if (x[1] >= 0) {
fpo.upd(abs(x[1]), -1);
} else {
fneg.upd(abs(x[1]), -1);
}
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, q;
cin >> n >> q;
string s;
cin >> s;
s = "0" + s + "0";
set <int> st;
int f = 0;
for (int i = 0; i < len(s); i++) {
if (s[i] == '1') {
if (f == 0)
f = i;
} else {
if (f) {
all.pb({f, i - 1, -1, 1});
f = 0;
}
st.insert(i);
}
}
for (int i = 1; i <= q; i++) {
string t;
cin >> t;
if (t[0] == 't') {
int p;
cin >> p;
if (s[p] == '0') {
st.erase(p);
s[p] = '1';
auto it = st.lower_bound(p);
int r = *it - 1, l = *prev(it) + 1;
all.pb({l, r, -i - 1, 1});
if (p - 1 >= l) {
all.pb({l, p - 1, i + 1, 1});
}
if (p + 1 <= r) {
all.pb({p + 1, r, i + 1, 1});
}
} else {
auto it = st.lower_bound(p);
int r = *it - 1, l = *prev(it) + 1;
all.pb({l, r, i + 1, 1});
if (p - 1 >= l) {
all.pb({l, p - 1, -i - 1, 1});
}
if (p + 1 <= r) {
all.pb({p + 1, r, -i - 1, 1});
}
s[p] = '0';
st.insert(p);
}
} else {
int a, b;
cin >> a >> b;
all.pb({a, b - 1, i + 1, 0});
}
}
mem(ans, -1);
sort(all(all), [&](vector <int> &a, vector <int> &b) {return a[0] == b[0] ? a.back() > b.back() : a[0] < b[0];});
dnc(0, sz(all) - 1);
for (int i = 2; i <= q + 1; i++) {
if (ans[i] != -1) {
cout << ans[i] << '\n';
}
}
}
/*
5 7
11011
query 1 2
query 1 2
query 1 6
query 3 4
toggle 3
query 3 4
query 1 6
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
26200 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1331 ms |
65008 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
26460 KB |
Output is correct |
2 |
Incorrect |
9 ms |
26456 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
26460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
26200 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |