#include "bits/stdc++.h"
using namespace std;
#define int int64_t
#define pb push_back
using lint=__int128_t;
const int lim=200100;
const int mod=1e9+7;
using pii=pair<int,int>;
struct query{
int t,x,y;
};
struct{
int tree[4*lim];
int n;
void init(int N){
n=N;
for(int i=0;i<=4*n;i++)tree[i]=LLONG_MAX;
}
int P,VAL;
void update(int l,int r,int node){
if(l==r){
tree[node]=VAL;
return;
}
int mid=(l+r)>>1,child=node<<1;
if(P<=mid)update(l,mid,child);
else update(mid+1,r,child|1);
tree[node]=max(tree[child],tree[child|1]);
}
void update(int p,int val){
P=p,VAL=val;
update(0,n-1,1);
}
int L,R;
int query(int l,int r,int node){
if(L<=l&&r<=R){
return tree[node];
}
if(r<L||R<l){
return -5;
}
int mid=(l+r)>>1,child=node<<1;
return max(query(l,mid,child),query(mid+1,r,child|1));
}
int query(int l,int r){
L=l,R=r;
return query(0,n-1,1);
}
}st;
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#ifdef Local
freopen(".in","r",stdin);freopen(".out1","w",stdout);
#endif
int n,m;
cin>>n>>m;
string s;
cin>>s;
query q[m];
for(int i=0;i<m;i++){
string t;
cin>>t;
if(t=="toggle"){
q[i].t=0;
}else{
q[i].t=1;
}
if(q[i].t){
cin>>q[i].x>>q[i].y;
}else{
cin>>q[i].x;
}
q[i].x--,q[i].y--;
}
if(n<=100&&m<=100){
int arr[m+1][n];
for(int i=0;i<n;i++){
arr[0][i]=s[i]-'0';
}
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
arr[i+1][j]=arr[i][j];
if(i&&!q[i-1].t&&q[i-1].x==j){
arr[i+1][j]=!arr[i+1][j];
}
//cerr<<arr[i+1][j]<<" ";
}//cerr<<"\n";
if(q[i].t){
int cnt=0;
//cerr<<q[i].x<<" "<<q[i].y<<"\n";
for(int j=0;j<=i;j++){
bool ok=1;
for(int k=q[i].x;k<q[i].y;k++){
if(!arr[j+1][k]){
ok=0;
break;
}
}
cnt+=ok;
}
cout<<cnt<<"\n";
}
}
}else{
bool ok=1;
for(int i=0;i<m;i++){
if(q[i].t&&q[i].x+1!=q[i].y){
ok=0;
break;
}
}
if(ok){
vector<int>toggles[n];
for(int i=0;i<n;i++){
toggles[i].pb(-1);
}
vector<int>qr[n];
for(int i=0;i<m;i++){
if(!q[i].t)toggles[q[i].x].push_back(i);
else qr[q[i].x].pb(i);
}
int ans[m];
for(int i=0;i<n;i++){
int state=s[i]-'0';
int tp=0,cur=0;
for(int j=0;j+1<toggles[i].size();j++){
while(tp<qr[i].size()&&qr[i][tp]<=toggles[i][j+1]){
ans[qr[i][tp]]=cur+state*(qr[i][tp]-toggles[i][j]);
assert(0<=qr[i][tp]&&qr[i][tp]<m);
tp++;
}
cur+=state*(toggles[i][j+1]-toggles[i][j]);
state=!state;
}
while(tp<qr[i].size()){
ans[qr[i][tp]]=cur+state*(qr[i][tp]-toggles[i][toggles[i].size()-1]);
tp++;
}
}
for(int i=0;i<m;i++){
if(q[i].t)cout<<ans[i]<<"\n";
}
}else{
st.init(n);
for(int i=0;i<n;i++){
if(s[i]=='1')st.update(i,-1);
}
for(int i=0;i<m;i++){
if(q[i].t){
int val=st.query(q[i].x,q[i].y);
if(val!=LLONG_MAX){
cout<<i-val<<"\n";
}else{
cout<<"0\n";
}
}else{
st.update(q[i].x,i);
}
}
}
}
}
Compilation message
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:133:32: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
133 | for(int j=0;j+1<toggles[i].size();j++){
| ~~~^~~~~~~~~~~~~~~~~~
street_lamps.cpp:134:29: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
134 | while(tp<qr[i].size()&&qr[i][tp]<=toggles[i][j+1]){
| ~~^~~~~~~~~~~~~
street_lamps.cpp:142:25: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
142 | while(tp<qr[i].size()){
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
14160 KB |
Output is correct |
2 |
Correct |
45 ms |
14420 KB |
Output is correct |
3 |
Correct |
48 ms |
14928 KB |
Output is correct |
4 |
Correct |
134 ms |
38372 KB |
Output is correct |
5 |
Correct |
102 ms |
38884 KB |
Output is correct |
6 |
Correct |
97 ms |
37856 KB |
Output is correct |
7 |
Correct |
87 ms |
40432 KB |
Output is correct |
8 |
Correct |
109 ms |
41960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Runtime error |
46 ms |
28404 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
41 ms |
14160 KB |
Output is correct |
9 |
Correct |
45 ms |
14420 KB |
Output is correct |
10 |
Correct |
48 ms |
14928 KB |
Output is correct |
11 |
Correct |
134 ms |
38372 KB |
Output is correct |
12 |
Correct |
102 ms |
38884 KB |
Output is correct |
13 |
Correct |
97 ms |
37856 KB |
Output is correct |
14 |
Correct |
87 ms |
40432 KB |
Output is correct |
15 |
Correct |
109 ms |
41960 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Runtime error |
46 ms |
28404 KB |
Execution killed with signal 11 |
21 |
Halted |
0 ms |
0 KB |
- |