#include<bits/stdc++.h>
using namespace std;
#define pb emplace_back
#define ld double
#define ll __int128
mt19937 rnd(51);
const int N = 3e5 + 10;
vector<int> arr[N];
struct Fenwick {
vector<int> t;
void build(int n) {
t.resize(n);
}
void inc(int i, int d) {
for (; i < t.size(); i = (i | (i + 1))) {
t[i] += d;
}
}
int get(int r) {
int ans = 0;
for (; r >= 0; r = (r & (r + 1)) - 1) {
ans += t[r];
}
return ans;
}
} t[N];
void fake_get(int i, int j) {
for (; i >= 0; i = (i & (i + 1)) - 1) {
arr[i].pb(j);
}
}
void add(int i, int j, int val) {
for (; i < N; i = (i | (i + 1))) {
int ind = lower_bound(arr[i].begin(), arr[i].end(), j) - arr[i].begin();
t[i].inc(ind, val);
}
}
int get(int i, int j) {
int ans = 0;
for (; i >= 0; i = (i & (i + 1)) - 1) {
int ind = upper_bound(arr[i].begin(), arr[i].end(), j) - arr[i].begin() - 1;
ans += t[i].get(ind);
}
return ans;
}
signed main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif // LOCAL
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, q;
cin >> n >> q;
vector<int> s(n);
set<int> st{-1, n};
for (int i = 0; i < n; i++) {
char c;
cin >> c;
s[i] = c - '0';
if (s[i] == 0) {
st.insert(i);
}
}
vector<vector<int>> u;
for (int i = 0; i < q; i++) {
string t;
cin >> t;
if (t == "toggle") {
int j;
cin >> j;
j--;
if (st.find(j) == st.end()) {
auto l = *prev(st.lower_bound(j)) + 1, r = *st.upper_bound(j) - 1;
u.push_back({l, r, i + 1, j});
st.insert(j);
} else {
st.erase(j);
auto l = *prev(st.lower_bound(j)) + 1, r = *st.upper_bound(j) - 1;
u.push_back({l, r, -(i + 1), j});
}
} else {
int l, r;
cin >> l >> r;
l--, r -= 2;
fake_get(l, r);
auto val = *st.lower_bound(l);
if (val > r) {
u.push_back({l, r, 1});
} else {
u.push_back({l, r, 0});
}
}
}
for (int i = 0; i < N; i++) {
sort(arr[i].begin(), arr[i].end());
arr[i].erase(unique(arr[i].begin(), arr[i].end()), arr[i].end());
t[i].build(arr[i].size());
}
for (int i = 0; i < u.size(); i++) {
if (u[i].size() == 3) {
int l = u[i][0], r = u[i][1];
cout << get(l, r) + (i + 1) * u[i][2] << endl;
} else {
int l = u[i][0], r = u[i][1], val = u[i][2], j = u[i][3];
add(l, j, val);
add(l, r + 1, -val);
add(j + 1, j, -val);
add(j + 1, r + 1, val);
}
}
return 0;
}
Compilation message
street_lamps.cpp: In member function 'void Fenwick::inc(int, int)':
street_lamps.cpp:20:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | for (; i < t.size(); i = (i | (i + 1))) {
| ~~^~~~~~~~~~
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:108:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
108 | for (int i = 0; i < u.size(); i++) {
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14292 KB |
Output is correct |
2 |
Correct |
8 ms |
14404 KB |
Output is correct |
3 |
Correct |
8 ms |
14352 KB |
Output is correct |
4 |
Correct |
8 ms |
14420 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
7 |
Correct |
8 ms |
14412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
414 ms |
36944 KB |
Output is correct |
2 |
Correct |
411 ms |
37228 KB |
Output is correct |
3 |
Correct |
603 ms |
38852 KB |
Output is correct |
4 |
Correct |
1228 ms |
64204 KB |
Output is correct |
5 |
Correct |
1261 ms |
62156 KB |
Output is correct |
6 |
Correct |
1095 ms |
60704 KB |
Output is correct |
7 |
Correct |
1703 ms |
83416 KB |
Output is correct |
8 |
Correct |
1323 ms |
70844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14420 KB |
Output is correct |
2 |
Correct |
9 ms |
14420 KB |
Output is correct |
3 |
Correct |
9 ms |
14420 KB |
Output is correct |
4 |
Correct |
11 ms |
14420 KB |
Output is correct |
5 |
Correct |
771 ms |
46552 KB |
Output is correct |
6 |
Correct |
1055 ms |
52860 KB |
Output is correct |
7 |
Correct |
1206 ms |
60656 KB |
Output is correct |
8 |
Correct |
1386 ms |
71276 KB |
Output is correct |
9 |
Correct |
413 ms |
34444 KB |
Output is correct |
10 |
Correct |
456 ms |
41196 KB |
Output is correct |
11 |
Correct |
508 ms |
41708 KB |
Output is correct |
12 |
Correct |
1596 ms |
83836 KB |
Output is correct |
13 |
Correct |
1387 ms |
71344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14548 KB |
Output is correct |
2 |
Correct |
10 ms |
14548 KB |
Output is correct |
3 |
Correct |
10 ms |
14492 KB |
Output is correct |
4 |
Correct |
9 ms |
14548 KB |
Output is correct |
5 |
Correct |
1558 ms |
76724 KB |
Output is correct |
6 |
Correct |
1290 ms |
69008 KB |
Output is correct |
7 |
Correct |
1107 ms |
60064 KB |
Output is correct |
8 |
Correct |
560 ms |
47152 KB |
Output is correct |
9 |
Correct |
417 ms |
38852 KB |
Output is correct |
10 |
Correct |
252 ms |
37880 KB |
Output is correct |
11 |
Correct |
417 ms |
38844 KB |
Output is correct |
12 |
Correct |
245 ms |
37640 KB |
Output is correct |
13 |
Correct |
428 ms |
38896 KB |
Output is correct |
14 |
Correct |
246 ms |
37744 KB |
Output is correct |
15 |
Correct |
1860 ms |
83904 KB |
Output is correct |
16 |
Correct |
1418 ms |
71104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14292 KB |
Output is correct |
2 |
Correct |
8 ms |
14404 KB |
Output is correct |
3 |
Correct |
8 ms |
14352 KB |
Output is correct |
4 |
Correct |
8 ms |
14420 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
7 |
Correct |
8 ms |
14412 KB |
Output is correct |
8 |
Correct |
414 ms |
36944 KB |
Output is correct |
9 |
Correct |
411 ms |
37228 KB |
Output is correct |
10 |
Correct |
603 ms |
38852 KB |
Output is correct |
11 |
Correct |
1228 ms |
64204 KB |
Output is correct |
12 |
Correct |
1261 ms |
62156 KB |
Output is correct |
13 |
Correct |
1095 ms |
60704 KB |
Output is correct |
14 |
Correct |
1703 ms |
83416 KB |
Output is correct |
15 |
Correct |
1323 ms |
70844 KB |
Output is correct |
16 |
Correct |
10 ms |
14420 KB |
Output is correct |
17 |
Correct |
9 ms |
14420 KB |
Output is correct |
18 |
Correct |
9 ms |
14420 KB |
Output is correct |
19 |
Correct |
11 ms |
14420 KB |
Output is correct |
20 |
Correct |
771 ms |
46552 KB |
Output is correct |
21 |
Correct |
1055 ms |
52860 KB |
Output is correct |
22 |
Correct |
1206 ms |
60656 KB |
Output is correct |
23 |
Correct |
1386 ms |
71276 KB |
Output is correct |
24 |
Correct |
413 ms |
34444 KB |
Output is correct |
25 |
Correct |
456 ms |
41196 KB |
Output is correct |
26 |
Correct |
508 ms |
41708 KB |
Output is correct |
27 |
Correct |
1596 ms |
83836 KB |
Output is correct |
28 |
Correct |
1387 ms |
71344 KB |
Output is correct |
29 |
Correct |
10 ms |
14548 KB |
Output is correct |
30 |
Correct |
10 ms |
14548 KB |
Output is correct |
31 |
Correct |
10 ms |
14492 KB |
Output is correct |
32 |
Correct |
9 ms |
14548 KB |
Output is correct |
33 |
Correct |
1558 ms |
76724 KB |
Output is correct |
34 |
Correct |
1290 ms |
69008 KB |
Output is correct |
35 |
Correct |
1107 ms |
60064 KB |
Output is correct |
36 |
Correct |
560 ms |
47152 KB |
Output is correct |
37 |
Correct |
417 ms |
38852 KB |
Output is correct |
38 |
Correct |
252 ms |
37880 KB |
Output is correct |
39 |
Correct |
417 ms |
38844 KB |
Output is correct |
40 |
Correct |
245 ms |
37640 KB |
Output is correct |
41 |
Correct |
428 ms |
38896 KB |
Output is correct |
42 |
Correct |
246 ms |
37744 KB |
Output is correct |
43 |
Correct |
1860 ms |
83904 KB |
Output is correct |
44 |
Correct |
1418 ms |
71104 KB |
Output is correct |
45 |
Correct |
230 ms |
28404 KB |
Output is correct |
46 |
Correct |
339 ms |
29168 KB |
Output is correct |
47 |
Correct |
611 ms |
41212 KB |
Output is correct |
48 |
Correct |
1294 ms |
63060 KB |
Output is correct |
49 |
Correct |
509 ms |
41468 KB |
Output is correct |
50 |
Correct |
516 ms |
41236 KB |
Output is correct |
51 |
Correct |
547 ms |
41712 KB |
Output is correct |
52 |
Correct |
562 ms |
43376 KB |
Output is correct |
53 |
Correct |
540 ms |
43408 KB |
Output is correct |
54 |
Correct |
624 ms |
43392 KB |
Output is correct |