#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e5+10;
set<int> lpos, rpos;
int n, q;
vector<int> pfw[maxn];
vector<long long> FW[maxn];
int a[maxn], org[maxn];
int U[maxn], V[maxn], ty[maxn];
void fadd(int x, int y, bool type, int t){
for(x; x <= n+1; x+=x&(-x)){
if(!type)pfw[x].push_back(y);
else for(int idx = lower_bound(pfw[x].begin(), pfw[x].end(), y) - pfw[x].begin(); idx < pfw[x].size(); idx += idx&(-idx))FW[x][idx]+=t;
}
}
long long query(int x, int y){
long long re = 0;
for(x; x > 0; x-=x&(-x)){
for(int idx = upper_bound(pfw[x].begin(), pfw[x].end(), y) - pfw[x].begin() - 1; idx > 0; idx -= idx&(-idx))re += FW[x][idx];
}
return re;
}
void fadd_range(int a, int b, bool type, int t){
// cout << "SUS " << a << " " << b << " " << t << endl;
fadd(a, a, type, t);
fadd(a, b+1, type, -t);
fadd(b+1, a, type, -t);
fadd(b+1, b+1, type, t);
}
void new_range(int l, int r, int type, int t){
int fr = *prev(rpos.lower_bound(l)), fl = *lpos.upper_bound(r);
fadd_range(fr+1, fl, type, t);
fadd_range(fr+1, l, type, -t);
fadd_range(r+1, fl, type, -t);
lpos.insert(l);
rpos.insert(r);
}
void change_l(int l_old, int l_new, int type, int t){
int fr = *prev(rpos.lower_bound(l_old));
fadd_range(fr+1, l_old, type, t);
fadd_range(fr+1, l_new, type, -t);
lpos.erase(l_old);
lpos.insert(l_new);
}
void change_r(int r_old, int r_new, int type, int t){
int fl = *lpos.upper_bound(r_old);
fadd_range(r_old+1, fl, type, t);
fadd_range(r_new+1, fl, type, -t);
rpos.erase(r_old);
rpos.insert(r_new);
}
void del_range(int l, int r, int type, int t){
int fr = *prev(rpos.lower_bound(l)), fl = *lpos.upper_bound(r);
fadd_range(fr+1, fl, type, -t);
fadd_range(fr+1, l, type, t);
fadd_range(r+1, fl, type, t);
lpos.erase(l);
rpos.erase(r);
}
void turn_on(int pos, bool type, int t){
if(!a[pos-1] && !a[pos+1])new_range(pos, pos, type, t);
else if(a[pos-1] && !a[pos+1])change_r(pos-1, pos, type, t);
else if(!a[pos-1] && a[pos+1])change_l(pos+1, pos, type, t);
else {
fadd_range(pos, pos+1, type, t);
lpos.erase(pos+1);
rpos.erase(pos-1);
}
}
void turn_off(int pos, bool type, int t){
if(!a[pos-1] && !a[pos+1])del_range(pos, pos, type, t);
else if(a[pos-1] && !a[pos+1])change_r(pos, pos-1, type, t);
else if(!a[pos-1] && a[pos+1])change_l(pos, pos+1, type, t);
else {
fadd_range(pos, pos+1, type, -t);
lpos.insert(pos+1);
rpos.insert(pos-1);
}
}
void toggle(int pos, bool type, int t){
// cout << "TOGGLE " << pos << endl;
if(a[pos])turn_off(pos, type, t);
else turn_on(pos, type, t);
a[pos]^=1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> q;
a[0] = org[0] = 1;
a[n+1] = org[n+1] = 1;
for(int i = 1; i <= n; i++){
char x;
cin >> x;
a[i] = (x-'0')^1;
org[i] = a[i];
}
int last = -1;
for(int i = 0; i <= n+1; i++){
if(!a[i]){
if(a[i-1]){
lpos.insert(last);
rpos.insert(i-1);
last = -1;
}
}
else if(last == -1)last = i;
}
lpos.insert(last);
rpos.insert(n+1);
for(int i = 0; i < q; i++){
string str;
cin >> str;
if(str == "query"){
ty[i] = 1;
cin >> U[i] >> V[i];
}
else {
cin >> V[i];
toggle(V[i], 0, 0);
}
}
lpos.clear();
rpos.clear();
last = -1;
for(int i = 0; i <= n+1; i++){
a[i] = org[i];
if(!a[i]){
if(a[i-1]){
lpos.insert(last);
rpos.insert(i-1);
last = -1;
}
}
else if(last == -1)last = i;
}
lpos.insert(last);
rpos.insert(n+1);
for(int i = 1; i <= n+1; i++){
pfw[i].push_back(0);
sort(pfw[i].begin(), pfw[i].end());
pfw[i].erase(unique(pfw[i].begin(), pfw[i].end()), pfw[i].end());
FW[i].assign(pfw[i].size(), 0);
}
for(int i = 0; i < q; i++){
// cout << i << endl;
// for(int j = 1; j <= n; j++)cout << a[j] << " ";
// cout << endl;
// for(auto v: lpos)cout << v << " ";
// cout << endl;
// for(auto v: rpos)cout << v << " ";
if(!ty[i]){
toggle(V[i], 1, i+1);
}
else {
long long ans = query(U[i], V[i]);
if(U[i] >= *rpos.begin() + 1){
int fr = *prev(rpos.lower_bound(U[i])), fl = *lpos.upper_bound(fr);
if(fr+1 <= U[i] && V[i] <= fl)ans += i+1;
}
cout << ans << "\n";
}
}
}
Compilation message
street_lamps.cpp: In function 'void fadd(int, int, bool, int)':
street_lamps.cpp:15:9: warning: statement has no effect [-Wunused-value]
15 | for(x; x <= n+1; x+=x&(-x)){
| ^
street_lamps.cpp:17:95: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | else for(int idx = lower_bound(pfw[x].begin(), pfw[x].end(), y) - pfw[x].begin(); idx < pfw[x].size(); idx += idx&(-idx))FW[x][idx]+=t;
| ~~~~^~~~~~~~~~~~~~~
street_lamps.cpp: In function 'long long int query(int, int)':
street_lamps.cpp:23:9: warning: statement has no effect [-Wunused-value]
23 | for(x; x > 0; x-=x&(-x)){
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
20056 KB |
Output is correct |
2 |
Correct |
5 ms |
20060 KB |
Output is correct |
3 |
Correct |
5 ms |
20060 KB |
Output is correct |
4 |
Correct |
5 ms |
20244 KB |
Output is correct |
5 |
Correct |
4 ms |
20060 KB |
Output is correct |
6 |
Correct |
4 ms |
20060 KB |
Output is correct |
7 |
Correct |
4 ms |
20060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
350 ms |
42668 KB |
Output is correct |
2 |
Correct |
475 ms |
49328 KB |
Output is correct |
3 |
Correct |
858 ms |
62828 KB |
Output is correct |
4 |
Correct |
2369 ms |
126000 KB |
Output is correct |
5 |
Correct |
2158 ms |
132640 KB |
Output is correct |
6 |
Correct |
2513 ms |
142504 KB |
Output is correct |
7 |
Correct |
169 ms |
45652 KB |
Output is correct |
8 |
Correct |
178 ms |
46872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
20568 KB |
Output is correct |
2 |
Correct |
7 ms |
20528 KB |
Output is correct |
3 |
Correct |
7 ms |
20316 KB |
Output is correct |
4 |
Correct |
5 ms |
20324 KB |
Output is correct |
5 |
Correct |
3743 ms |
179884 KB |
Output is correct |
6 |
Correct |
3051 ms |
158020 KB |
Output is correct |
7 |
Correct |
2094 ms |
132008 KB |
Output is correct |
8 |
Correct |
185 ms |
46928 KB |
Output is correct |
9 |
Correct |
67 ms |
23888 KB |
Output is correct |
10 |
Correct |
73 ms |
24344 KB |
Output is correct |
11 |
Correct |
74 ms |
24432 KB |
Output is correct |
12 |
Correct |
176 ms |
45728 KB |
Output is correct |
13 |
Correct |
165 ms |
47028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
20312 KB |
Output is correct |
2 |
Correct |
7 ms |
20316 KB |
Output is correct |
3 |
Correct |
7 ms |
20316 KB |
Output is correct |
4 |
Correct |
8 ms |
20572 KB |
Output is correct |
5 |
Correct |
439 ms |
54304 KB |
Output is correct |
6 |
Correct |
1504 ms |
100128 KB |
Output is correct |
7 |
Correct |
2548 ms |
142764 KB |
Output is correct |
8 |
Correct |
3759 ms |
181616 KB |
Output is correct |
9 |
Correct |
642 ms |
72772 KB |
Output is correct |
10 |
Correct |
591 ms |
66412 KB |
Output is correct |
11 |
Correct |
668 ms |
73904 KB |
Output is correct |
12 |
Correct |
564 ms |
68888 KB |
Output is correct |
13 |
Correct |
635 ms |
71960 KB |
Output is correct |
14 |
Correct |
587 ms |
67392 KB |
Output is correct |
15 |
Correct |
168 ms |
45848 KB |
Output is correct |
16 |
Correct |
177 ms |
47016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
20056 KB |
Output is correct |
2 |
Correct |
5 ms |
20060 KB |
Output is correct |
3 |
Correct |
5 ms |
20060 KB |
Output is correct |
4 |
Correct |
5 ms |
20244 KB |
Output is correct |
5 |
Correct |
4 ms |
20060 KB |
Output is correct |
6 |
Correct |
4 ms |
20060 KB |
Output is correct |
7 |
Correct |
4 ms |
20060 KB |
Output is correct |
8 |
Correct |
350 ms |
42668 KB |
Output is correct |
9 |
Correct |
475 ms |
49328 KB |
Output is correct |
10 |
Correct |
858 ms |
62828 KB |
Output is correct |
11 |
Correct |
2369 ms |
126000 KB |
Output is correct |
12 |
Correct |
2158 ms |
132640 KB |
Output is correct |
13 |
Correct |
2513 ms |
142504 KB |
Output is correct |
14 |
Correct |
169 ms |
45652 KB |
Output is correct |
15 |
Correct |
178 ms |
46872 KB |
Output is correct |
16 |
Correct |
8 ms |
20568 KB |
Output is correct |
17 |
Correct |
7 ms |
20528 KB |
Output is correct |
18 |
Correct |
7 ms |
20316 KB |
Output is correct |
19 |
Correct |
5 ms |
20324 KB |
Output is correct |
20 |
Correct |
3743 ms |
179884 KB |
Output is correct |
21 |
Correct |
3051 ms |
158020 KB |
Output is correct |
22 |
Correct |
2094 ms |
132008 KB |
Output is correct |
23 |
Correct |
185 ms |
46928 KB |
Output is correct |
24 |
Correct |
67 ms |
23888 KB |
Output is correct |
25 |
Correct |
73 ms |
24344 KB |
Output is correct |
26 |
Correct |
74 ms |
24432 KB |
Output is correct |
27 |
Correct |
176 ms |
45728 KB |
Output is correct |
28 |
Correct |
165 ms |
47028 KB |
Output is correct |
29 |
Correct |
5 ms |
20312 KB |
Output is correct |
30 |
Correct |
7 ms |
20316 KB |
Output is correct |
31 |
Correct |
7 ms |
20316 KB |
Output is correct |
32 |
Correct |
8 ms |
20572 KB |
Output is correct |
33 |
Correct |
439 ms |
54304 KB |
Output is correct |
34 |
Correct |
1504 ms |
100128 KB |
Output is correct |
35 |
Correct |
2548 ms |
142764 KB |
Output is correct |
36 |
Correct |
3759 ms |
181616 KB |
Output is correct |
37 |
Correct |
642 ms |
72772 KB |
Output is correct |
38 |
Correct |
591 ms |
66412 KB |
Output is correct |
39 |
Correct |
668 ms |
73904 KB |
Output is correct |
40 |
Correct |
564 ms |
68888 KB |
Output is correct |
41 |
Correct |
635 ms |
71960 KB |
Output is correct |
42 |
Correct |
587 ms |
67392 KB |
Output is correct |
43 |
Correct |
168 ms |
45848 KB |
Output is correct |
44 |
Correct |
177 ms |
47016 KB |
Output is correct |
45 |
Correct |
128 ms |
33472 KB |
Output is correct |
46 |
Correct |
231 ms |
35768 KB |
Output is correct |
47 |
Correct |
1128 ms |
74140 KB |
Output is correct |
48 |
Correct |
2318 ms |
127116 KB |
Output is correct |
49 |
Correct |
76 ms |
24356 KB |
Output is correct |
50 |
Correct |
79 ms |
24488 KB |
Output is correct |
51 |
Correct |
79 ms |
24656 KB |
Output is correct |
52 |
Correct |
79 ms |
24760 KB |
Output is correct |
53 |
Correct |
79 ms |
24872 KB |
Output is correct |
54 |
Correct |
79 ms |
25200 KB |
Output is correct |