#include <bits/stdc++.h>
using namespace std;
void just_do_it();
int main() {
#ifdef KAMIRULEZ
freopen("kamirulez.inp", "r", stdin);
freopen("kamirulez.out", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
just_do_it();
return 0;
}
struct node {
int pref, suf;
vector<pair<int, int>> pref_events;
vector<pair<int, int>> suf_events;
vector<pair<int, pair<int, int>>> queries;
};
const int maxN = 3e5 + 20;
int a[maxN];
int res[maxN];
node tr[maxN * 4];
int sum_single[maxN];
int prv_single[maxN];
void add_event(int id, int T) {
if (tr[id].pref_events.empty() || tr[id].pref != tr[id].pref_events.back().second) {
tr[id].pref_events.push_back({T, tr[id].pref});
}
if (tr[id].suf_events.empty() || tr[id].suf != tr[id].suf_events.back().second) {
tr[id].suf_events.push_back({T, tr[id].suf});
}
}
void merge(int id, int lt, int rt, int mt, int T) {
tr[id].pref = tr[id * 2].pref + (tr[id * 2].pref == mt - lt + 1 ? tr[id * 2 + 1].pref : 0);
tr[id].suf = tr[id * 2 + 1].suf + (tr[id * 2 + 1].suf == rt - mt ? tr[id * 2].suf : 0);
add_event(id, T);
}
void build(int id, int lt, int rt) {
if (lt == rt) {
tr[id].pref = a[lt];
tr[id].suf = a[lt];
return;
}
int mt = (lt + rt) / 2;
build(id * 2, lt, mt);
build(id * 2 + 1, mt + 1, rt);
merge(id, lt, rt, mt, 0);
}
void update(int id, int lt, int rt, int pos, int T) {
if (lt == rt) {
if (a[lt] == 1) {
sum_single[lt] += T - prv_single[lt];
}
prv_single[lt] = T;
a[lt] ^= 1;
tr[id].pref = a[lt];
tr[id].suf = a[lt];
return;
}
int mt = (lt + rt) / 2;
if (pos <= mt) {
update(id * 2, lt, mt, pos, T);
}
else {
update(id * 2 + 1, mt + 1, rt, pos, T);
}
merge(id, lt, rt, mt, T);
}
void add(int id, int lt, int rt, int ql, int qr, int T) {
if (lt == rt) {
res[T] = sum_single[lt];
if (a[lt] == 1) {
res[T] += T - prv_single[lt];
}
return;
}
int mt = (lt + rt) / 2;
if (qr <= mt) {
add(id * 2, lt, mt, ql, qr, T);
}
else if (ql >= mt + 1) {
add(id * 2 + 1, mt + 1, rt, ql, qr, T);
}
else {
tr[id].queries.push_back({T, {ql, qr}});
}
}
void just_do_it() {
int n, q;
cin >> n >> q;
for (int i = 1; i <= n; i++) {
char c;
cin >> c;
a[i] = c - '0';
}
build(1, 1, n);
for (int T = 1; T <= q; T++) {
res[T] = -1;
string s;
cin >> s;
if (s == "toggle") {
int pos;
cin >> pos;
update(1, 1, n, pos, T);
}
if (s == "query") {
int ql, qr;
cin >> ql >> qr;
qr--;
add(1, 1, n, ql, qr, T);
}
}
for (int i = 1; i <= q; i++) {
if (res[i] != -1) {
cout << res[i] << '\n';
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
48 ms |
94280 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
142 ms |
105548 KB |
Output is correct |
2 |
Correct |
162 ms |
105712 KB |
Output is correct |
3 |
Correct |
207 ms |
107172 KB |
Output is correct |
4 |
Correct |
452 ms |
128380 KB |
Output is correct |
5 |
Correct |
464 ms |
129948 KB |
Output is correct |
6 |
Correct |
447 ms |
129484 KB |
Output is correct |
7 |
Correct |
284 ms |
121924 KB |
Output is correct |
8 |
Correct |
341 ms |
123232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
64 ms |
94292 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
63 ms |
94272 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
48 ms |
94280 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |