#include <bits/stdc++.h>
using namespace std;
struct segtree{
vector<int> st;
int n;
segtree(int N){
n=N;
st.assign(4*N, 1e9);
}
void build(int node, int left, int right, vector<int>&a){
if(left==right){
st[node]=a[left];
return;
}
build(node*2, left, (left+right)/2, a);
build(node*2+1, (left+right)/2+1, right, a);
}
void set(int node, int left, int right, int pos, int val){
if(left==right && left==pos){
st[node]=val;
return;
}
if(right<pos || left>pos)return;
set(node*2, left, (left+right)/2, pos, val);
set(node*2+1, (left+right)/2+1, right, pos, val);
st[node]=max(st[node*2], st[node*2+1]);
}
int query(int node, int left, int right, int ql, int qr){
if(right<ql || left>qr){
return -1e9;
}
if(left>=ql && right<=qr)return st[node];
return max(query(node*2, left, (left+right)/2, ql, qr), query(node*2+1, (left+right)/2+1, right, ql, qr));
}
};
signed main(){
int n, q;cin>>n>>q;
string s;cin>>s;
vector<int> a;
for(int i = 0;i<n;i++){
if(s[i]=='1')a.push_back(1);
else a.push_back(0);
}
vector<vector<int>> events;
bool isss2=true;
bool isss3=true;
vector<int> statee=a;
for(int i = 0;i<q;i++){
string ev;cin>>ev;
if(ev=="toggle"){
int x;cin>>x;
events.push_back({0, x});
statee[x]^=1;
if(statee[x]==0)isss3=false;
}
else{
int x, y;cin>>x>>y;
if(y-x!=1)isss2=false;
events.push_back({1, x, y});
}
}
if(isss3){
segtree st(n);
for(int i = 0;i<q;i++){
if(events[i][0]==0){
st.set(1, 0, n-1, events[i][1]-1, i);
}
else{
cout << max(0, i-st.query(1, 0, n-1, events[i][1]-1, events[i][2]-2)) << '\n';
}
}
return 0;
}
//now codes the ss2
//segtree for max activation time, like it changes after each thing
//like at first its infinity for all of them, then I set someone to one, two three etc
//a query will just be the i-max, low bar 0
if(isss2){
vector<int> count=a;
vector<int> state=a;
vector<int> lastc(n, 0);
for(int i = 0;i<q;i++){
auto thing = events[i];
if(thing[0]==0){
if(state[thing[1]-1]==1)count[thing[1]-1]+=(i-lastc[thing[1]-1]);
state[thing[1]-1]^=1;
lastc[thing[1]-1]=i;
}
else{
int res = count[thing[1]-1];
if(state[thing[1]-1]==1)res+=(i-lastc[thing[1]-1]);
cout << res << '\n';
}
}
return 0;
}
if(n<=200 && q<=200){
vector<vector<int>> strings={a};
for(auto thing:events){
if (thing[0]==0){
a[thing[1]-1]^=1;
}
else{
int tres = 0;
for(auto strin:strings){
bool res = true;
for(int i =thing[1]-1;i<thing[2]-1;i++){
if(strin[i]==0)res=false;
}
if(res)tres++;
}
cout << tres << '\n';
}
strings.push_back(a);
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |