#include <bits/stdc++.h>
#define forf(i,a,b) for(int i = a; i<=b; i++)
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
int N,M,Q;
char S[300001];
struct Interval{
int s,e,ts,te;
}; vector<Interval> ints;
map<int,int> stkeyf,stkeyb;
map<int,int> enkeyf,enkeyb;
void ins(int s, int e, int t){
int now = ints.size();
ints.push_back({s,e, t, -1});
stkeyf.insert({s, now});
stkeyb.insert({-s, now});
enkeyf.insert({e, now});
enkeyb.insert({-e, now});
}
void ers(int ind ,int t){
ints[ind].te = t;
int a = ints[ind].s;
int b = ints[ind].e;
stkeyf.erase(stkeyf.lower_bound(a));
stkeyb.erase(stkeyb.lower_bound(-a));
enkeyf.erase(enkeyf.lower_bound(b));
enkeyb.erase(enkeyb.lower_bound(-b));
}
struct Event{
int t;
int q;
int s,e,v;
bool operator<(const Event &r) const{
if(t==r.t){
if(q==r.q) return s<r.s;
return q>r.q;
}
return t<r.t;
}
}; vector<Event> events;
int isq[300001];
int ans[300001];
struct Seg{
int arr[300005];
void update(int f, int x){ for(int i = f; i<=Q+3; i += i&-i) arr[i]+=x;}
int query(int f){
int ret = 0;
for(int i = f; i; i-=i&-i) ret += arr[i];
return ret;
}
int query(int s, int e){ return query(e)-query(s-1);}
} seg;
struct Event2{
int x,y,q,v,id;
bool operator<(const Event2 &r) const{
if(x==r.x){
if(q==r.q) return id<r.id;
return q<r.q;
}
return x<r.x;
}
};
void dnc(int s, int e){
if(s==e) return;
int m = s+e>>1;
vector<Event2> events2;
forf(i,s,m){
if(events[i].q == 0) events2.push_back({events[i].s,events[i].e,0,events[i].v,i});
}
forf(i,m+1,e){
if(events[i].q == 1) events2.push_back({events[i].s,events[i].e,1,0,events[i].t});
}
sort(all(events2));
for(auto i : events2){
if(i.q == 0) seg.update(i.y,i.v);
else ans[i.id] += seg.query(i.y,Q+2);
}
for(auto i : events2) if(i.q == 0) seg.update(i.y,-i.v);
dnc(s,m);
dnc(m+1,e);
}
int main(){
scanf("%d %d" , &N,&Q);
scanf("%s" ,S); S[N] = '0';
{
int l = 1;
forf(i, 1, N) {
if (S[i - 1] == '0' && S[i] == '1') l = i+1;
if (S[i - 1] == '1' && S[i] == '0') {
ins(l,i,1);
}
}
}
forf(i,2,Q+1){ // t = i
char s[10]; int a,b;
scanf("%s" , s);
if(s[0] == 'q'){
isq[i] = 1;
scanf("%d %d" , &a,&b); b--;
events.push_back({i,1,a,b});
if(enkeyf.lower_bound(b) != enkeyf.end()) {
int ind = enkeyf.lower_bound(b)->second;
if (ints[ind].s <= a && b <= ints[ind].e) ans[i] += (i - ints[ind].ts);
}
}
else{
scanf("%d" , &a);
if(S[a-1] == '0'){
S[a-1] = '1';
int l = a, r = a;
if(enkeyb.lower_bound(-a) != enkeyb.end()) {
int indl = enkeyb.lower_bound(-a)->second;
if (ints[indl].e + 1 == a) {
ers(indl, i - 1);
l = ints[indl].s;
}
}
if(stkeyf.lower_bound(a) != stkeyf.end()) {
int indr = stkeyf.lower_bound(a)->second;
if (ints[indr].s - 1 == a) {
ers(indr, i - 1);
r = ints[indr].e;
}
}
ins(l,r,i);
}
else{
S[a-1] = '0';
int ind = enkeyf.lower_bound(a)->second;
ers(ind,i-1);
if(ints[ind].s < a) ins(ints[ind].s , a-1 , i);
if(ints[ind].e > a) ins(a+1 , ints[ind].e , i);
}
}
}
for(auto i : ints){
if(i.te != -1) events.push_back({i.te,0,i.s,i.e,i.te-i.ts+1});
}
sort(all(events));
dnc(0,events.size()-1);
forf(i,1,Q+1){
if(isq[i]) printf("%d\n" , ans[i]);
}
}
Compilation message
street_lamps.cpp: In function 'void dnc(int, int)':
street_lamps.cpp:72:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
72 | int m = s+e>>1;
| ~^~
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:92:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | scanf("%d %d" , &N,&Q);
| ~~~~~^~~~~~~~~~~~~~~~~
street_lamps.cpp:93:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
93 | scanf("%s" ,S); S[N] = '0';
| ~~~~~^~~~~~~~~
street_lamps.cpp:107:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
107 | scanf("%s" , s);
| ~~~~~^~~~~~~~~~
street_lamps.cpp:110:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
110 | scanf("%d %d" , &a,&b); b--;
| ~~~~~^~~~~~~~~~~~~~~~~
street_lamps.cpp:118:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
118 | scanf("%d" , &a);
| ~~~~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2500 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
458 ms |
20888 KB |
Output is correct |
2 |
Correct |
485 ms |
28988 KB |
Output is correct |
3 |
Correct |
522 ms |
28092 KB |
Output is correct |
4 |
Correct |
823 ms |
46264 KB |
Output is correct |
5 |
Correct |
912 ms |
38148 KB |
Output is correct |
6 |
Correct |
888 ms |
48552 KB |
Output is correct |
7 |
Correct |
293 ms |
26036 KB |
Output is correct |
8 |
Correct |
294 ms |
29124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2396 KB |
Output is correct |
2 |
Correct |
3 ms |
2396 KB |
Output is correct |
3 |
Correct |
2 ms |
2396 KB |
Output is correct |
4 |
Correct |
2 ms |
2396 KB |
Output is correct |
5 |
Correct |
1076 ms |
32868 KB |
Output is correct |
6 |
Correct |
1022 ms |
40044 KB |
Output is correct |
7 |
Correct |
800 ms |
34452 KB |
Output is correct |
8 |
Correct |
313 ms |
26828 KB |
Output is correct |
9 |
Correct |
167 ms |
16052 KB |
Output is correct |
10 |
Correct |
187 ms |
21216 KB |
Output is correct |
11 |
Correct |
190 ms |
21840 KB |
Output is correct |
12 |
Correct |
295 ms |
24496 KB |
Output is correct |
13 |
Correct |
301 ms |
28676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2392 KB |
Output is correct |
2 |
Correct |
2 ms |
2396 KB |
Output is correct |
3 |
Correct |
2 ms |
2396 KB |
Output is correct |
4 |
Correct |
2 ms |
2396 KB |
Output is correct |
5 |
Correct |
572 ms |
37624 KB |
Output is correct |
6 |
Correct |
717 ms |
47520 KB |
Output is correct |
7 |
Correct |
862 ms |
47880 KB |
Output is correct |
8 |
Correct |
1034 ms |
42428 KB |
Output is correct |
9 |
Correct |
510 ms |
32580 KB |
Output is correct |
10 |
Correct |
546 ms |
27600 KB |
Output is correct |
11 |
Correct |
505 ms |
31564 KB |
Output is correct |
12 |
Correct |
529 ms |
27068 KB |
Output is correct |
13 |
Correct |
509 ms |
29976 KB |
Output is correct |
14 |
Correct |
532 ms |
28696 KB |
Output is correct |
15 |
Correct |
292 ms |
25776 KB |
Output is correct |
16 |
Correct |
308 ms |
26800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2500 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
458 ms |
20888 KB |
Output is correct |
9 |
Correct |
485 ms |
28988 KB |
Output is correct |
10 |
Correct |
522 ms |
28092 KB |
Output is correct |
11 |
Correct |
823 ms |
46264 KB |
Output is correct |
12 |
Correct |
912 ms |
38148 KB |
Output is correct |
13 |
Correct |
888 ms |
48552 KB |
Output is correct |
14 |
Correct |
293 ms |
26036 KB |
Output is correct |
15 |
Correct |
294 ms |
29124 KB |
Output is correct |
16 |
Correct |
2 ms |
2396 KB |
Output is correct |
17 |
Correct |
3 ms |
2396 KB |
Output is correct |
18 |
Correct |
2 ms |
2396 KB |
Output is correct |
19 |
Correct |
2 ms |
2396 KB |
Output is correct |
20 |
Correct |
1076 ms |
32868 KB |
Output is correct |
21 |
Correct |
1022 ms |
40044 KB |
Output is correct |
22 |
Correct |
800 ms |
34452 KB |
Output is correct |
23 |
Correct |
313 ms |
26828 KB |
Output is correct |
24 |
Correct |
167 ms |
16052 KB |
Output is correct |
25 |
Correct |
187 ms |
21216 KB |
Output is correct |
26 |
Correct |
190 ms |
21840 KB |
Output is correct |
27 |
Correct |
295 ms |
24496 KB |
Output is correct |
28 |
Correct |
301 ms |
28676 KB |
Output is correct |
29 |
Correct |
2 ms |
2392 KB |
Output is correct |
30 |
Correct |
2 ms |
2396 KB |
Output is correct |
31 |
Correct |
2 ms |
2396 KB |
Output is correct |
32 |
Correct |
2 ms |
2396 KB |
Output is correct |
33 |
Correct |
572 ms |
37624 KB |
Output is correct |
34 |
Correct |
717 ms |
47520 KB |
Output is correct |
35 |
Correct |
862 ms |
47880 KB |
Output is correct |
36 |
Correct |
1034 ms |
42428 KB |
Output is correct |
37 |
Correct |
510 ms |
32580 KB |
Output is correct |
38 |
Correct |
546 ms |
27600 KB |
Output is correct |
39 |
Correct |
505 ms |
31564 KB |
Output is correct |
40 |
Correct |
529 ms |
27068 KB |
Output is correct |
41 |
Correct |
509 ms |
29976 KB |
Output is correct |
42 |
Correct |
532 ms |
28696 KB |
Output is correct |
43 |
Correct |
292 ms |
25776 KB |
Output is correct |
44 |
Correct |
308 ms |
26800 KB |
Output is correct |
45 |
Correct |
253 ms |
16420 KB |
Output is correct |
46 |
Correct |
289 ms |
15472 KB |
Output is correct |
47 |
Correct |
394 ms |
22316 KB |
Output is correct |
48 |
Correct |
847 ms |
43976 KB |
Output is correct |
49 |
Correct |
231 ms |
23036 KB |
Output is correct |
50 |
Correct |
216 ms |
25852 KB |
Output is correct |
51 |
Correct |
213 ms |
22444 KB |
Output is correct |
52 |
Correct |
219 ms |
22700 KB |
Output is correct |
53 |
Correct |
230 ms |
23460 KB |
Output is correct |
54 |
Correct |
249 ms |
23872 KB |
Output is correct |