#include <bits/stdc++.h>
#define pii pair<int, int>
#define ti3 tuple<int, int, int>
#define ti4 tuple<int, int, int, int>
#define int long long
// Honestly, I love you, to the mysterious (SinB) place ~ SinB, Sunny Summer
using namespace std;
const int N = 300005;
int fw[N+1];
void update(int x, int v) {
for (; x<=N; x+=x&(-x)) fw[x] += v;
}
int sum(int x) {
int res = 0;
for(; x; x-=x&(-x)) res += fw[x];
return res;
}
set<pii> ranges;
vector<ti4> queries; // l,r,v/idx,isquery
int ans[300005];
void dnc(int s, int e){
if (s >= e) return;
int m = (s+e)/2;
vector<ti3> upds;
vector<ti3> qry;
for (int i = s; i <= m; i++){
auto [a, b, v, t] = queries[i];
if (t == 0){
upds.push_back({a, a, v});
upds.push_back({b+1, a, -v});
}
}
for (int i = m+1; i <= e; i++){
auto [a, b, v, t] = queries[i];
if (t == 1){
qry.push_back({b, a, v});
}
}
if (upds.empty() || qry.empty()){
if (upds.empty() && !qry.empty()){
// left side all queries, no use go there
dnc(m+1, e);
}
if (qry.empty() && !upds.empty()){
// right side all updates, no use go there
dnc(s, m);
}
return;
} else {
sort(qry.begin(), qry.end());
sort(upds.begin(), upds.end());
int uidx = 0;
for (auto [e, s, idx] : qry){
while (uidx != upds.size() && get<0>(upds[uidx]) <= e){
update(get<1>(upds[uidx]), get<2>(upds[uidx]));
uidx++;
}
ans[idx] += sum(s);
}
// revert changes
for (int i = uidx - 1; i >= 0; i--){
update(get<1>(upds[i]), -get<2>(upds[i]));
}
dnc(s, m); dnc(m+1, e);
}
}
main(){
ios_base::sync_with_stdio(0); cin.tie(0);
memset(ans, -1, sizeof ans);
int n, q; cin >> n >> q;
string s; cin >> s; s += '0';
int lst = -1;
for (int i = 1; i <= n+1; i++){
if (s[i-1] == '1'){
if (lst == -1) lst = i;
} else {
if (lst == -1) continue;
ranges.insert({lst, i-1});
// queries.push_back({lst, i-1, -0, 0}); // no effect
lst = -1;
}
}
for (int t = 1; t <= q; t++){
string evt; cin >> evt;
if (evt[0] == 'q'){
ans[t] = 0;
int a, b; cin >> a >> b; b--;
// check if {a, b} is in some range alr
auto itr = ranges.lower_bound({a, INT_MAX});
if (itr != ranges.begin()){
itr--;
// itr->first <= a
if (itr->second >= b){
ans[t] += t;
}
}
queries.push_back({a, b, t, 1});
} else {
int x; cin >> x;
if (s[x-1] == '0'){
auto itr = ranges.lower_bound({x, 0});
int ns = x, ne = x;
if (itr != ranges.begin()){
auto prv = prev(itr);
if (prv->second == x - 1){
ns = prv->first;
queries.push_back({ns, prv->second, t, 0});
ranges.erase(prv);
}
}
if (itr != ranges.end()){
if (itr->first == x + 1){
ne = itr->second;
queries.push_back({itr->first, ne, t, 0});
ranges.erase(itr);
}
}
queries.push_back({ns, ne, -t, 0});
ranges.insert({ns, ne});
} else {
auto itr = ranges.lower_bound({x, INT_MAX});
itr--;
if ((x-1) - itr->first >= 0){
queries.push_back({itr->first, x-1, -t, 0});
ranges.insert({itr->first, x-1});
}
if (itr->second - (x + 1) >= 0){
queries.push_back({x+1, itr->second, -t, 0});
ranges.insert({x+1, itr->second});
}
queries.push_back({itr->first, itr->second, t, 0});
ranges.erase(itr);
}
s[x-1] = (s[x-1] == '0'? '1': '0');
}
}
//for (auto [a, b, c, d]: queries) cout << a << ' ' << b << ' ' << c << ' ' << d << endl;
dnc(0, queries.size()-1);
for (int i = 1; i <= q; i++){
if (ans[i] != -1) cout << ans[i] << '\n';
}
}
Compilation message
street_lamps.cpp: In function 'void dnc(long long int, long long int)':
street_lamps.cpp:54:25: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::tuple<long long int, long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | while (uidx != upds.size() && get<0>(upds[uidx]) <= e){
| ~~~~~^~~~~~~~~~~~~~
street_lamps.cpp: At global scope:
street_lamps.cpp:67:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
67 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2656 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
1 ms |
2636 KB |
Output is correct |
6 |
Correct |
1 ms |
2672 KB |
Output is correct |
7 |
Correct |
2 ms |
2680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1181 ms |
44892 KB |
Output is correct |
2 |
Correct |
1084 ms |
44504 KB |
Output is correct |
3 |
Correct |
885 ms |
46004 KB |
Output is correct |
4 |
Correct |
885 ms |
53488 KB |
Output is correct |
5 |
Correct |
911 ms |
50880 KB |
Output is correct |
6 |
Correct |
411 ms |
60424 KB |
Output is correct |
7 |
Correct |
109 ms |
28496 KB |
Output is correct |
8 |
Correct |
116 ms |
29952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2804 KB |
Output is correct |
2 |
Correct |
4 ms |
2812 KB |
Output is correct |
3 |
Correct |
3 ms |
2816 KB |
Output is correct |
4 |
Correct |
2 ms |
2684 KB |
Output is correct |
5 |
Correct |
1014 ms |
70264 KB |
Output is correct |
6 |
Correct |
1126 ms |
60412 KB |
Output is correct |
7 |
Correct |
889 ms |
50948 KB |
Output is correct |
8 |
Correct |
127 ms |
29872 KB |
Output is correct |
9 |
Correct |
142 ms |
21388 KB |
Output is correct |
10 |
Correct |
181 ms |
25796 KB |
Output is correct |
11 |
Correct |
157 ms |
25876 KB |
Output is correct |
12 |
Correct |
116 ms |
28480 KB |
Output is correct |
13 |
Correct |
113 ms |
29904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2676 KB |
Output is correct |
2 |
Correct |
2 ms |
2892 KB |
Output is correct |
3 |
Correct |
2 ms |
2800 KB |
Output is correct |
4 |
Correct |
3 ms |
2804 KB |
Output is correct |
5 |
Correct |
296 ms |
37716 KB |
Output is correct |
6 |
Correct |
375 ms |
55320 KB |
Output is correct |
7 |
Correct |
434 ms |
60404 KB |
Output is correct |
8 |
Correct |
466 ms |
75316 KB |
Output is correct |
9 |
Correct |
375 ms |
55568 KB |
Output is correct |
10 |
Correct |
397 ms |
66152 KB |
Output is correct |
11 |
Correct |
393 ms |
55472 KB |
Output is correct |
12 |
Correct |
381 ms |
66180 KB |
Output is correct |
13 |
Correct |
351 ms |
55500 KB |
Output is correct |
14 |
Correct |
377 ms |
66352 KB |
Output is correct |
15 |
Correct |
110 ms |
28596 KB |
Output is correct |
16 |
Correct |
111 ms |
29936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2656 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
1 ms |
2636 KB |
Output is correct |
6 |
Correct |
1 ms |
2672 KB |
Output is correct |
7 |
Correct |
2 ms |
2680 KB |
Output is correct |
8 |
Correct |
1181 ms |
44892 KB |
Output is correct |
9 |
Correct |
1084 ms |
44504 KB |
Output is correct |
10 |
Correct |
885 ms |
46004 KB |
Output is correct |
11 |
Correct |
885 ms |
53488 KB |
Output is correct |
12 |
Correct |
911 ms |
50880 KB |
Output is correct |
13 |
Correct |
411 ms |
60424 KB |
Output is correct |
14 |
Correct |
109 ms |
28496 KB |
Output is correct |
15 |
Correct |
116 ms |
29952 KB |
Output is correct |
16 |
Correct |
2 ms |
2804 KB |
Output is correct |
17 |
Correct |
4 ms |
2812 KB |
Output is correct |
18 |
Correct |
3 ms |
2816 KB |
Output is correct |
19 |
Correct |
2 ms |
2684 KB |
Output is correct |
20 |
Correct |
1014 ms |
70264 KB |
Output is correct |
21 |
Correct |
1126 ms |
60412 KB |
Output is correct |
22 |
Correct |
889 ms |
50948 KB |
Output is correct |
23 |
Correct |
127 ms |
29872 KB |
Output is correct |
24 |
Correct |
142 ms |
21388 KB |
Output is correct |
25 |
Correct |
181 ms |
25796 KB |
Output is correct |
26 |
Correct |
157 ms |
25876 KB |
Output is correct |
27 |
Correct |
116 ms |
28480 KB |
Output is correct |
28 |
Correct |
113 ms |
29904 KB |
Output is correct |
29 |
Correct |
2 ms |
2676 KB |
Output is correct |
30 |
Correct |
2 ms |
2892 KB |
Output is correct |
31 |
Correct |
2 ms |
2800 KB |
Output is correct |
32 |
Correct |
3 ms |
2804 KB |
Output is correct |
33 |
Correct |
296 ms |
37716 KB |
Output is correct |
34 |
Correct |
375 ms |
55320 KB |
Output is correct |
35 |
Correct |
434 ms |
60404 KB |
Output is correct |
36 |
Correct |
466 ms |
75316 KB |
Output is correct |
37 |
Correct |
375 ms |
55568 KB |
Output is correct |
38 |
Correct |
397 ms |
66152 KB |
Output is correct |
39 |
Correct |
393 ms |
55472 KB |
Output is correct |
40 |
Correct |
381 ms |
66180 KB |
Output is correct |
41 |
Correct |
351 ms |
55500 KB |
Output is correct |
42 |
Correct |
377 ms |
66352 KB |
Output is correct |
43 |
Correct |
110 ms |
28596 KB |
Output is correct |
44 |
Correct |
111 ms |
29936 KB |
Output is correct |
45 |
Correct |
680 ms |
28128 KB |
Output is correct |
46 |
Correct |
688 ms |
27692 KB |
Output is correct |
47 |
Correct |
528 ms |
32952 KB |
Output is correct |
48 |
Correct |
870 ms |
54380 KB |
Output is correct |
49 |
Correct |
186 ms |
27556 KB |
Output is correct |
50 |
Correct |
184 ms |
27576 KB |
Output is correct |
51 |
Correct |
177 ms |
27796 KB |
Output is correct |
52 |
Correct |
126 ms |
28008 KB |
Output is correct |
53 |
Correct |
123 ms |
28048 KB |
Output is correct |
54 |
Correct |
126 ms |
28048 KB |
Output is correct |