#include <bits/stdc++.h>
using namespace std;
const int SQ = 700, N = 3e5 + 7;
int n, q;
string s;
int a[N], b[N];
string typ[N];
vector<int> v[N];
vector<int> qr[N];
vector<int> bl[N];
int l[N], r[N];
int clr[N], lst[N];
int ans[N];
void psh(int k) {
if (clr[k] != -1) {
for (int pos = l[k]; pos < r[k]; pos++) lst[pos] = clr[k];
clr[k] = -1;
bl[k].clear();
for (int pos = l[k]; pos < r[k]; pos++) bl[k].push_back(lst[pos]);
}
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> q >> s;
for (int i = 0; i < n; i++)
v[i].push_back(0);
for (int i = 0; i < q; i++) {
cin >> typ[i];
if (typ[i] == "toggle") {
int pos;
cin >> pos;
pos--;
v[pos].push_back(i + 1);
} else {
cin >> a[i] >> b[i];
a[i]--;
b[i]--;
qr[a[i]].push_back(i);
}
}
int blcks = 0;
for (int i = 0, pos = 0; i <= q; i += SQ, pos += 1) {
blcks++;
l[pos] = i, r[pos] = min(q + 1, i + SQ);
for (int j = l[pos]; j < r[pos]; j++) {
lst[j] = n;
bl[pos].push_back(n);
}
clr[pos] = -1;
}
for (int i = n - 1; i >= 0; i--) {
int cur = s[i] - '0';
for (int j = 0; j < v[i].size(); j++) {
if (cur == 0) {
int L = v[i][j], R = j == (int)v[i].size() - 1 ? q + 1 : v[i][j + 1];
for (int k = 0; k < blcks; k++) {
// psh(k);
if (l[k] >= R || r[k] <= L) continue;
if (l[k] >= L && r[k] <= R) {
clr[k] = i;
} else {
psh(k);
for (int pos = max(l[k], L); pos < min(r[k], R); pos++) lst[pos] = i;
bl[k].clear();
for (int pos = l[k]; pos < r[k]; pos++) bl[k].push_back(lst[pos]);
sort(bl[k].begin(), bl[k].end());
}
}
}
cur ^= 1;
}
for (int x : qr[i]) {
for (int pos = 0; pos < blcks; pos++) {
// psh(pos);
if (l[pos] > x) break;
if (r[pos] <= x + 1) {
if (clr[pos] == -1) {
ans[x] += bl[pos].end() - lower_bound(bl[pos].begin(), bl[pos].end(), b[x]);
} else {
if (clr[pos] >= b[x]) ans[x] += (int)bl[pos].size();
}
} else {
psh(pos);
for (int j = l[pos]; j <= x; j++) {
// cout << lst[j] << " " << x << "\n";
if (lst[j] >= x) ans[x]++;
}
}
}
}
}
for (int i = 0; i < q; i++) {
if (typ[i] == "query") cout << ans[i] << "\n";
}
return 0;
}
Compilation message
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:62:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for (int j = 0; j < v[i].size(); j++) {
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
22 ms |
30956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1982 ms |
40428 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
29 ms |
30956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
23 ms |
31012 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
22 ms |
30956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |