#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <class T> using ordered_set = tree<T,null_type,less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>;
#define int long long
#define pb push_back
#define pii pair<int,int>
#define fr first
#define sc second
#define OK puts("OK");
#define NO puts("NO");
#define YES puts("YES");
#define endi puts("");
#define ret return
#define all(s) s.begin(),s.end()
#define allr(s) s.rbegin(),s.rend()
const int N = 3e5+5,INF = 1e18+7;
int q[N],z[N],xo;
char s[N];
vector <pair<pii,int>> der[4*N];
void update(int v,int l,int r,int ql,int qr,int lol,int ror,int cost){
if (l > qr || r < ql)ret ;
if (ql <= l && r <= qr){
der[v].pb({{lol,ror},cost});
ret ;
}
int m = l+r>>1;
update(v<<1,l,m,ql,qr,lol,ror,cost);
update(v<<1|1,m+1,r,ql,qr,lol,ror,cost);
}
int get_ans(int v,int l,int r,int pos,int x){
int ans=0;
for (pair<pii,int> z: der[v])
if (z.fr.fr <= x && x <= z.fr.sc)
ans += z.sc,xo^=1;
if (l == r)ret ans;
int m = l+r>>1;
if (pos > m)
ret get_ans(v<<1|1,m+1,r,pos,x)+ans;
ret get_ans(v<<1,l,m,pos,x)+ans;
}
main(){
int n,t,i;
scanf("%lld%lld",&n,&t);
scanf("%s",s+1);
s[0] = s[n+1] = '0';
set <int> as;
for (i=0;i<=n+1;++i)
if (s[i] == '0')as.insert(i);
for (i=0;i<=n+1;++i){
if(s[i] == '0') continue;
int j=i;
while(j <= n+1 && s[j] == s[i]) j++;
update(1, 1, n, i, j-1, i, j-1, 0);
i = j - 1;
}
for (i=1;i<=t;++i){
string ss;
cin>>ss;
if (ss == "query"){
int l,r;
cin>>l>>r, r--;
xo = 0;
int ans = get_ans(1,1,n,l,r);
if (xo)ans += i;
cout <<ans<<"\n";
}
else {
int pos;
cin>>pos;
if (s[pos] == '0'){
as.erase(pos);
int l = *--as.upper_bound(pos);
int r = *as.lower_bound(pos);
update(1,1,n,l+1,pos,pos,r-1,-i);
s[pos] = '1';
}
else {
int l = *--as.upper_bound(pos);
int r = *as.lower_bound(pos);
update(1,1,n,l+1,pos,pos,r-1,i);
s[pos] = '0';
as.insert(pos);
}
}
}
}
Compilation message
street_lamps.cpp: In function 'void update(long long int, long long int, long long int, long long int, long long int, long long int, long long int, long long int)':
street_lamps.cpp:33:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
33 | int m = l+r>>1;
| ~^~
street_lamps.cpp: In function 'long long int get_ans(long long int, long long int, long long int, long long int, long long int)':
street_lamps.cpp:46:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
46 | int m = l+r>>1;
| ~^~
street_lamps.cpp: At global scope:
street_lamps.cpp:56:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
56 | main(){
| ^~~~
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:58:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | scanf("%lld%lld",&n,&t);
| ~~~~~^~~~~~~~~~~~~~~~~~
street_lamps.cpp:59:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | scanf("%s",s+1);
| ~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
28492 KB |
Output is correct |
2 |
Correct |
21 ms |
28400 KB |
Output is correct |
3 |
Correct |
18 ms |
28456 KB |
Output is correct |
4 |
Correct |
19 ms |
28472 KB |
Output is correct |
5 |
Correct |
18 ms |
28492 KB |
Output is correct |
6 |
Correct |
18 ms |
28492 KB |
Output is correct |
7 |
Correct |
19 ms |
28428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1251 ms |
38276 KB |
Output is correct |
2 |
Correct |
826 ms |
40028 KB |
Output is correct |
3 |
Correct |
676 ms |
42436 KB |
Output is correct |
4 |
Correct |
957 ms |
52184 KB |
Output is correct |
5 |
Correct |
994 ms |
49896 KB |
Output is correct |
6 |
Correct |
883 ms |
53476 KB |
Output is correct |
7 |
Correct |
965 ms |
49424 KB |
Output is correct |
8 |
Correct |
899 ms |
36676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
28492 KB |
Output is correct |
2 |
Correct |
19 ms |
28492 KB |
Output is correct |
3 |
Correct |
19 ms |
28492 KB |
Output is correct |
4 |
Correct |
20 ms |
28492 KB |
Output is correct |
5 |
Correct |
969 ms |
54036 KB |
Output is correct |
6 |
Correct |
959 ms |
52808 KB |
Output is correct |
7 |
Correct |
976 ms |
49292 KB |
Output is correct |
8 |
Correct |
911 ms |
36644 KB |
Output is correct |
9 |
Correct |
613 ms |
32124 KB |
Output is correct |
10 |
Correct |
681 ms |
32452 KB |
Output is correct |
11 |
Correct |
673 ms |
32652 KB |
Output is correct |
12 |
Correct |
992 ms |
49268 KB |
Output is correct |
13 |
Correct |
911 ms |
36612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
28492 KB |
Output is correct |
2 |
Correct |
17 ms |
28424 KB |
Output is correct |
3 |
Correct |
17 ms |
28496 KB |
Output is correct |
4 |
Correct |
17 ms |
28492 KB |
Output is correct |
5 |
Correct |
960 ms |
45980 KB |
Output is correct |
6 |
Correct |
898 ms |
49316 KB |
Output is correct |
7 |
Correct |
850 ms |
53060 KB |
Output is correct |
8 |
Correct |
745 ms |
57792 KB |
Output is correct |
9 |
Correct |
583 ms |
44008 KB |
Output is correct |
10 |
Correct |
321 ms |
45752 KB |
Output is correct |
11 |
Correct |
574 ms |
44148 KB |
Output is correct |
12 |
Correct |
320 ms |
45632 KB |
Output is correct |
13 |
Correct |
585 ms |
43828 KB |
Output is correct |
14 |
Correct |
320 ms |
45496 KB |
Output is correct |
15 |
Correct |
978 ms |
49268 KB |
Output is correct |
16 |
Correct |
921 ms |
36676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
28492 KB |
Output is correct |
2 |
Correct |
21 ms |
28400 KB |
Output is correct |
3 |
Correct |
18 ms |
28456 KB |
Output is correct |
4 |
Correct |
19 ms |
28472 KB |
Output is correct |
5 |
Correct |
18 ms |
28492 KB |
Output is correct |
6 |
Correct |
18 ms |
28492 KB |
Output is correct |
7 |
Correct |
19 ms |
28428 KB |
Output is correct |
8 |
Correct |
1251 ms |
38276 KB |
Output is correct |
9 |
Correct |
826 ms |
40028 KB |
Output is correct |
10 |
Correct |
676 ms |
42436 KB |
Output is correct |
11 |
Correct |
957 ms |
52184 KB |
Output is correct |
12 |
Correct |
994 ms |
49896 KB |
Output is correct |
13 |
Correct |
883 ms |
53476 KB |
Output is correct |
14 |
Correct |
965 ms |
49424 KB |
Output is correct |
15 |
Correct |
899 ms |
36676 KB |
Output is correct |
16 |
Correct |
18 ms |
28492 KB |
Output is correct |
17 |
Correct |
19 ms |
28492 KB |
Output is correct |
18 |
Correct |
19 ms |
28492 KB |
Output is correct |
19 |
Correct |
20 ms |
28492 KB |
Output is correct |
20 |
Correct |
969 ms |
54036 KB |
Output is correct |
21 |
Correct |
959 ms |
52808 KB |
Output is correct |
22 |
Correct |
976 ms |
49292 KB |
Output is correct |
23 |
Correct |
911 ms |
36644 KB |
Output is correct |
24 |
Correct |
613 ms |
32124 KB |
Output is correct |
25 |
Correct |
681 ms |
32452 KB |
Output is correct |
26 |
Correct |
673 ms |
32652 KB |
Output is correct |
27 |
Correct |
992 ms |
49268 KB |
Output is correct |
28 |
Correct |
911 ms |
36612 KB |
Output is correct |
29 |
Correct |
18 ms |
28492 KB |
Output is correct |
30 |
Correct |
17 ms |
28424 KB |
Output is correct |
31 |
Correct |
17 ms |
28496 KB |
Output is correct |
32 |
Correct |
17 ms |
28492 KB |
Output is correct |
33 |
Correct |
960 ms |
45980 KB |
Output is correct |
34 |
Correct |
898 ms |
49316 KB |
Output is correct |
35 |
Correct |
850 ms |
53060 KB |
Output is correct |
36 |
Correct |
745 ms |
57792 KB |
Output is correct |
37 |
Correct |
583 ms |
44008 KB |
Output is correct |
38 |
Correct |
321 ms |
45752 KB |
Output is correct |
39 |
Correct |
574 ms |
44148 KB |
Output is correct |
40 |
Correct |
320 ms |
45632 KB |
Output is correct |
41 |
Correct |
585 ms |
43828 KB |
Output is correct |
42 |
Correct |
320 ms |
45496 KB |
Output is correct |
43 |
Correct |
978 ms |
49268 KB |
Output is correct |
44 |
Correct |
921 ms |
36676 KB |
Output is correct |
45 |
Correct |
3349 ms |
35012 KB |
Output is correct |
46 |
Correct |
522 ms |
35328 KB |
Output is correct |
47 |
Correct |
537 ms |
40176 KB |
Output is correct |
48 |
Correct |
913 ms |
51788 KB |
Output is correct |
49 |
Correct |
737 ms |
32580 KB |
Output is correct |
50 |
Correct |
736 ms |
32652 KB |
Output is correct |
51 |
Correct |
749 ms |
32820 KB |
Output is correct |
52 |
Correct |
791 ms |
32996 KB |
Output is correct |
53 |
Correct |
774 ms |
32972 KB |
Output is correct |
54 |
Correct |
770 ms |
32952 KB |
Output is correct |