#include <bits/stdc++.h>
typedef int ll;
typedef long double ld;
#define pb push_back
#define pii pair < int , int >
#define F first
#define S second
#define endl '\n'
//#define int long long
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define kill(x) return cout<<x<<'\n', 0;
using namespace std;
const int N=10,inf=1e9;
vector <int> query[N];
vector <int> fen[N];
void addd(ll idx,ll idy,ll val){
for (ll x=idx;x<N;x+=x & (-x)){
for (ll y=lower_bound(query[x].begin(),query[x].end(),idy)-query[x].begin();y<fen[x].size();y+=y & (-y)){
fen[x][y]+=val;
// cout << idx << " " << idy << " " << x << " add " << y << " " << fen[x][y] << " " << val << endl;
}
}
}
void add(ll l,ll r,ll val){
addd(l,l,val);
// cout << "kir" << endl;
addd(l,r+1,-val);
// cout << "TO basanet" << endl;
addd(r+1,l,-val);
addd(r+1,r+1,val);
}
void addd1(ll idx,ll idy,ll val){
for (ll x=idx;x<N;x+=x & (-x)){
query[x].pb(idy);
}
}
void add1(ll l,ll r,ll val){
addd1(l,l,val);
addd1(l,r+1,-val);
addd1(r+1,l,-val);
addd1(r+1,r+1,val);
}
ll get(ll idx,ll idy){
ll s=0;
for (ll x=idx;x;x-=x & (-x)){
for (ll y=upper_bound(query[x].begin(),query[x].end(),idy)-query[x].begin()-1;y;y-=y & (-y)){
s+=fen[x][y];
// cout << x << " get " << y << endl;
}
}
return s;
}
ll a[N];
ll b[N];
int32_t main(){
sync;
set <pair <pii,int> > s,w;
ll n,q;
cin >> n >> q;
for (int i=1;i<=n;i++){
char c;
cin >> c;
a[i]=c-'0';
b[i]=a[i];
}
for (int i=1;i<=n;i++){
if (!a[i]) continue;
ll k=i;
while(k<=n && a[k]) k++;
s.insert({{i,k-1},-1});
i=k-1;
}
w=s;
vector <pair <string,pii> > qq;
for (int i=1;i<=q;i++){
string type;
cin >> type;
qq.pb({type,{0,0}});
if (type=="toggle"){
ll id;
cin >> id;
qq.back().S.F=id;
if (a[id]){
auto t=s.upper_bound({{id,inf},0});
t--;
pair <pii,int> p=*t;
ll l=p.F.F,r=p.F.S,x=p.S;
add1(l,r,i-1-x);
s.erase(t);
if (id!=l) s.insert({{l,id-1},i-1});
if (id!=r) s.insert({{id+1,r},i-1});
}
else{
ll l=id,r=id;
auto t=s.upper_bound({{id,inf},0});
if (t!=s.end()){
pair <pii,int> p1=*t;
if (p1.F.F==id+1){
add1(p1.F.F,p1.F.S,i-1-p1.S);
r=p1.F.S;
s.erase(t);
}
}
t=s.upper_bound({{id,inf},0});
if (t!=s.begin()){
t--;
pair <pii,int> p1=*t;
if (p1.F.S==id-1){
add1(p1.F.F,p1.F.S,i-1-p1.S);
l=p1.F.F;
s.erase(t);
}
}
s.insert({{l,r},i-1});
}
a[id] ^= 1;
}
else{
ll l,r;
cin >> l >> r;
qq.back().S={l,r};
continue;
}
}
for (int i=0;i<=n+1;i++){
query[i].pb(0);
sort(query[i].begin(),query[i].end());
query[i].resize(unique(query[i].begin(),query[i].end())-query[i].begin());
// cout << query[i].size() << " sd " << endl;//
for (int j=0;j<query[i].size()+5;j++) fen[i].pb(0);
}
s=w;
for (int i=1;i<=n;i++) a[i]=b[i];
for (int i=1;i<=q;i++){
string type;
type=qq[i-1].F;
if (type=="toggle"){
ll id;
id=qq[i-1].S.F;
if (a[id]){
auto t=s.upper_bound({{id,inf},0});
t--;
pair <pii,int> p=*t;
ll l=p.F.F,r=p.F.S,x=p.S;
add(l,r,i-1-x);
// cout << l << " " << r << endl;
s.erase(t);
if (id!=l) s.insert({{l,id-1},i-1});
if (id!=r) s.insert({{id+1,r},i-1});
}
else{
ll l=id,r=id;
auto t=s.upper_bound({{id,inf},0});
if (t!=s.end()){
pair <pii,int> p1=*t;
if (p1.F.F==id+1){
add(p1.F.F,p1.F.S,i-1-p1.S);
r=p1.F.S;
s.erase(t);
}
}
/*
while(s.size()){
cout << s.begin()->F.F << " " << a.begin()->F.S << endl;
s.erase()
}
*/
t=s.upper_bound({{id,inf},0});
if (t!=s.begin()){
t--;
pair <pii,int> p1=*t;
if (p1.F.S==id-1){
add(p1.F.F,p1.F.S,i-1-p1.S);
l=p1.F.F;
s.erase(t);
}
}
// cout << l << " " << r << get(3,3) << endl;
s.insert({{l,r},i-1});
}
a[id] ^= 1;
}
else{
ll l=qq[i-1].S.F,r=qq[i-1].S.S;
r--;
ll ans=get(l,r);
// cout << " " << ans << endl;
auto t=s.upper_bound({{l,inf},0});
if (t!=s.begin()){
t--;
pair <pii,int> p=*t;
if (p.F.S>=r){
ans+=i-1-p.S;
}
}
cout << ans << endl;
}
}
}
/*
5 11
11011
query 1 2
query 1 2
query 1 6
query 3 4
toggle 3
query 3 4
query 4 6
query 1 6
query 4 6
toggle 4
query 4 6
*/
Compilation message
street_lamps.cpp: In function 'void addd(ll, ll, ll)':
street_lamps.cpp:19:86: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (ll y=lower_bound(query[x].begin(),query[x].end(),idy)-query[x].begin();y<fen[x].size();y+=y & (-y)){
~^~~~~~~~~~~~~~
street_lamps.cpp: In function 'int32_t main()':
street_lamps.cpp:132:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j=0;j<query[i].size()+5;j++) fen[i].pb(0);
~^~~~~~~~~~~~~~~~~~
street_lamps.cpp:70:26: warning: array subscript is above array bounds [-Warray-bounds]
while(k<=n && a[k]) k++;
~~~^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Runtime error |
1 ms |
512 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
512 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
512 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
512 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Runtime error |
1 ms |
512 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |