#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300005;
using lint = long long;
using pi = pair<int, int>;
struct bit{
int tree[MAXN];
void add(int x, int v){
while(x < MAXN){
tree[x] += v;
x += x & -x;
}
}
int query(int x){
int ret = 0;
while(x){
ret += tree[x];
x -= x & -x;
}
return ret;
}
}bit;
struct intv{
int s, e, x;
bool operator<(const intv &i)const{
return s < i.s;
}
};
struct event{
int x, y, t, type;
bool operator<(const event &e)const{
return pi(x, y) < pi(e.x, e.y);
}
};
int ret[MAXN];
vector<event> ev;
set<intv> s;
void add_rect(int s, int e, int x){
ev.push_back({s, s, +x, 1});
ev.push_back({s, e + 1, -x, 1});
ev.push_back({e + 1, s, -x, 1});
ev.push_back({e + 1, e + 1, +x, 1});
}
void solve(int s, int e){
if(s == e) return;
int m = (s+e)/2;
solve(s, m);
solve(m + 1, e);
vector<event> pupd, quer;
for(int i=s; i<=m; i++){
if(ev[i].type == 1){
pupd.push_back(ev[i]);
}
}
for(int i=m+1; i<=e; i++){
if(ev[i].type == 2){
quer.push_back(ev[i]);
}
}
sort(pupd.begin(), pupd.end());
sort(quer.begin(), quer.end());
int ptr = 0;
for(auto &i : quer){
while(ptr < pupd.size() && pupd[ptr].x <= i.x){
bit.add(pupd[ptr].y, pupd[ptr].t);
ptr++;
}
ret[i.t] += bit.query(i.y);
}
for(int i=0; i<ptr; i++){
bit.add(pupd[i].y, -pupd[i].t);
}
}
int n, q;
char str[MAXN];
int main(){
scanf("%d %d",&n,&q);
scanf("%s", str + 1);
for(int i=1; i<=n; ){
if(str[i] == '0'){
i++;
continue;
}
int e = i;
while(e <= n && str[e] == '1') e++;
s.insert({i, e - 1, 1});
i = e;
}
char buf[10];
for(int i=1; i<=q; i++){
scanf("%s", buf);
if(*buf == 't'){
int t; scanf("%d",&t);
if(str[t] == '0'){
int st = t, ed = t;
auto l = s.lower_bound({t + 1, -1, -1});
if(l != s.end() && l->s == t + 1){
add_rect(l->s, l->e, i + 1 - l->x);
ed = l->e;
l = s.erase(l);
}
if(l != s.begin() && prev(l)->e == t - 1){
l--;
add_rect(l->s, l->e, i + 1 - l->x);
st = l->s;
s.erase(l);
}
s.insert({st, ed, i + 1});
}
if(str[t] == '1'){
auto l = --s.lower_bound({t + 1, -1, -1});
add_rect(l->s, l->e, i + 1 - l->x);
vector<intv> newV;
if(l->s < t){
newV.push_back({l->s, t - 1, i + 1});
}
if(t < l->e){
newV.push_back({t + 1, l->e, i + 1});
}
s.erase(l);
for(auto &i : newV) s.insert(i);
}
str[t] ^= 1;
ret[i] = -1;
}
else{
int a, b; scanf("%d %d",&a,&b);
b--;
ev.push_back({a, b, i, 2});
auto l = s.lower_bound({a + 1, -1, -1});
if(l != s.begin()){
l--;
if(l->s <= a && b <= l->e){
ret[i] += i + 1 - l->x;
}
}
}
}
solve(0, ev.size() - 1);
for(int i=1; i<=q; i++){
if(ret[i] >= 0) printf("%d\n", ret[i]);
}
}
Compilation message
street_lamps.cpp: In function 'void solve(int, int)':
street_lamps.cpp:70:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<event>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | while(ptr < pupd.size() && pupd[ptr].x <= i.x){
| ~~~~^~~~~~~~~~~~~
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:85:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
85 | scanf("%d %d",&n,&q);
| ~~~~~^~~~~~~~~~~~~~~
street_lamps.cpp:86:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
86 | scanf("%s", str + 1);
| ~~~~~^~~~~~~~~~~~~~~
street_lamps.cpp:99:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
99 | scanf("%s", buf);
| ~~~~~^~~~~~~~~~~
street_lamps.cpp:101:16: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
101 | int t; scanf("%d",&t);
| ~~~~~^~~~~~~~~
street_lamps.cpp:135:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
135 | int a, b; scanf("%d %d",&a,&b);
| ~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
508 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1054 ms |
26164 KB |
Output is correct |
2 |
Correct |
1170 ms |
26164 KB |
Output is correct |
3 |
Correct |
1072 ms |
26356 KB |
Output is correct |
4 |
Correct |
1187 ms |
32328 KB |
Output is correct |
5 |
Correct |
1423 ms |
40708 KB |
Output is correct |
6 |
Correct |
982 ms |
40584 KB |
Output is correct |
7 |
Correct |
410 ms |
18900 KB |
Output is correct |
8 |
Correct |
377 ms |
18944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
492 KB |
Output is correct |
2 |
Correct |
4 ms |
492 KB |
Output is correct |
3 |
Correct |
4 ms |
448 KB |
Output is correct |
4 |
Correct |
2 ms |
364 KB |
Output is correct |
5 |
Correct |
1459 ms |
49552 KB |
Output is correct |
6 |
Correct |
1555 ms |
53456 KB |
Output is correct |
7 |
Correct |
1304 ms |
40068 KB |
Output is correct |
8 |
Correct |
425 ms |
18888 KB |
Output is correct |
9 |
Correct |
242 ms |
11860 KB |
Output is correct |
10 |
Correct |
254 ms |
14920 KB |
Output is correct |
11 |
Correct |
228 ms |
14920 KB |
Output is correct |
12 |
Correct |
460 ms |
18900 KB |
Output is correct |
13 |
Correct |
442 ms |
18988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
492 KB |
Output is correct |
2 |
Correct |
3 ms |
492 KB |
Output is correct |
3 |
Correct |
3 ms |
492 KB |
Output is correct |
4 |
Correct |
3 ms |
492 KB |
Output is correct |
5 |
Correct |
576 ms |
19388 KB |
Output is correct |
6 |
Correct |
884 ms |
34748 KB |
Output is correct |
7 |
Correct |
1016 ms |
40544 KB |
Output is correct |
8 |
Correct |
1248 ms |
56368 KB |
Output is correct |
9 |
Correct |
942 ms |
35680 KB |
Output is correct |
10 |
Correct |
1038 ms |
48656 KB |
Output is correct |
11 |
Correct |
974 ms |
35644 KB |
Output is correct |
12 |
Correct |
1030 ms |
48748 KB |
Output is correct |
13 |
Correct |
914 ms |
35632 KB |
Output is correct |
14 |
Correct |
1046 ms |
48808 KB |
Output is correct |
15 |
Correct |
449 ms |
18936 KB |
Output is correct |
16 |
Correct |
428 ms |
18880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
508 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1054 ms |
26164 KB |
Output is correct |
9 |
Correct |
1170 ms |
26164 KB |
Output is correct |
10 |
Correct |
1072 ms |
26356 KB |
Output is correct |
11 |
Correct |
1187 ms |
32328 KB |
Output is correct |
12 |
Correct |
1423 ms |
40708 KB |
Output is correct |
13 |
Correct |
982 ms |
40584 KB |
Output is correct |
14 |
Correct |
410 ms |
18900 KB |
Output is correct |
15 |
Correct |
377 ms |
18944 KB |
Output is correct |
16 |
Correct |
3 ms |
492 KB |
Output is correct |
17 |
Correct |
4 ms |
492 KB |
Output is correct |
18 |
Correct |
4 ms |
448 KB |
Output is correct |
19 |
Correct |
2 ms |
364 KB |
Output is correct |
20 |
Correct |
1459 ms |
49552 KB |
Output is correct |
21 |
Correct |
1555 ms |
53456 KB |
Output is correct |
22 |
Correct |
1304 ms |
40068 KB |
Output is correct |
23 |
Correct |
425 ms |
18888 KB |
Output is correct |
24 |
Correct |
242 ms |
11860 KB |
Output is correct |
25 |
Correct |
254 ms |
14920 KB |
Output is correct |
26 |
Correct |
228 ms |
14920 KB |
Output is correct |
27 |
Correct |
460 ms |
18900 KB |
Output is correct |
28 |
Correct |
442 ms |
18988 KB |
Output is correct |
29 |
Correct |
3 ms |
492 KB |
Output is correct |
30 |
Correct |
3 ms |
492 KB |
Output is correct |
31 |
Correct |
3 ms |
492 KB |
Output is correct |
32 |
Correct |
3 ms |
492 KB |
Output is correct |
33 |
Correct |
576 ms |
19388 KB |
Output is correct |
34 |
Correct |
884 ms |
34748 KB |
Output is correct |
35 |
Correct |
1016 ms |
40544 KB |
Output is correct |
36 |
Correct |
1248 ms |
56368 KB |
Output is correct |
37 |
Correct |
942 ms |
35680 KB |
Output is correct |
38 |
Correct |
1038 ms |
48656 KB |
Output is correct |
39 |
Correct |
974 ms |
35644 KB |
Output is correct |
40 |
Correct |
1030 ms |
48748 KB |
Output is correct |
41 |
Correct |
914 ms |
35632 KB |
Output is correct |
42 |
Correct |
1046 ms |
48808 KB |
Output is correct |
43 |
Correct |
449 ms |
18936 KB |
Output is correct |
44 |
Correct |
428 ms |
18880 KB |
Output is correct |
45 |
Correct |
621 ms |
17700 KB |
Output is correct |
46 |
Correct |
691 ms |
18352 KB |
Output is correct |
47 |
Correct |
658 ms |
21472 KB |
Output is correct |
48 |
Correct |
1105 ms |
37564 KB |
Output is correct |
49 |
Correct |
280 ms |
16328 KB |
Output is correct |
50 |
Correct |
269 ms |
16200 KB |
Output is correct |
51 |
Correct |
257 ms |
16328 KB |
Output is correct |
52 |
Correct |
254 ms |
16712 KB |
Output is correct |
53 |
Correct |
251 ms |
16840 KB |
Output is correct |
54 |
Correct |
253 ms |
16712 KB |
Output is correct |