#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=3e5+100,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);
~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14464 KB |
Output is correct |
2 |
Correct |
9 ms |
14464 KB |
Output is correct |
3 |
Correct |
9 ms |
14464 KB |
Output is correct |
4 |
Correct |
9 ms |
14464 KB |
Output is correct |
5 |
Correct |
9 ms |
14464 KB |
Output is correct |
6 |
Correct |
12 ms |
14464 KB |
Output is correct |
7 |
Correct |
10 ms |
14464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1078 ms |
86484 KB |
Output is correct |
2 |
Correct |
1046 ms |
78596 KB |
Output is correct |
3 |
Correct |
1185 ms |
76652 KB |
Output is correct |
4 |
Correct |
2421 ms |
106908 KB |
Output is correct |
5 |
Correct |
2546 ms |
119796 KB |
Output is correct |
6 |
Correct |
2593 ms |
116352 KB |
Output is correct |
7 |
Correct |
345 ms |
58548 KB |
Output is correct |
8 |
Correct |
352 ms |
60048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
14848 KB |
Output is correct |
2 |
Correct |
16 ms |
14848 KB |
Output is correct |
3 |
Correct |
13 ms |
14848 KB |
Output is correct |
4 |
Correct |
10 ms |
14592 KB |
Output is correct |
5 |
Correct |
3796 ms |
139876 KB |
Output is correct |
6 |
Correct |
3569 ms |
131656 KB |
Output is correct |
7 |
Correct |
2863 ms |
118928 KB |
Output is correct |
8 |
Correct |
328 ms |
59956 KB |
Output is correct |
9 |
Correct |
124 ms |
27724 KB |
Output is correct |
10 |
Correct |
139 ms |
38224 KB |
Output is correct |
11 |
Correct |
137 ms |
38348 KB |
Output is correct |
12 |
Correct |
355 ms |
58676 KB |
Output is correct |
13 |
Correct |
376 ms |
59956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
14592 KB |
Output is correct |
2 |
Correct |
11 ms |
14720 KB |
Output is correct |
3 |
Correct |
12 ms |
14848 KB |
Output is correct |
4 |
Correct |
16 ms |
14848 KB |
Output is correct |
5 |
Correct |
602 ms |
67900 KB |
Output is correct |
6 |
Correct |
1639 ms |
92380 KB |
Output is correct |
7 |
Correct |
2521 ms |
115688 KB |
Output is correct |
8 |
Correct |
3747 ms |
137436 KB |
Output is correct |
9 |
Correct |
1353 ms |
101732 KB |
Output is correct |
10 |
Correct |
2035 ms |
134976 KB |
Output is correct |
11 |
Correct |
1406 ms |
101612 KB |
Output is correct |
12 |
Correct |
2065 ms |
140120 KB |
Output is correct |
13 |
Correct |
1354 ms |
101664 KB |
Output is correct |
14 |
Correct |
2150 ms |
139004 KB |
Output is correct |
15 |
Correct |
339 ms |
58676 KB |
Output is correct |
16 |
Correct |
356 ms |
60084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14464 KB |
Output is correct |
2 |
Correct |
9 ms |
14464 KB |
Output is correct |
3 |
Correct |
9 ms |
14464 KB |
Output is correct |
4 |
Correct |
9 ms |
14464 KB |
Output is correct |
5 |
Correct |
9 ms |
14464 KB |
Output is correct |
6 |
Correct |
12 ms |
14464 KB |
Output is correct |
7 |
Correct |
10 ms |
14464 KB |
Output is correct |
8 |
Correct |
1078 ms |
86484 KB |
Output is correct |
9 |
Correct |
1046 ms |
78596 KB |
Output is correct |
10 |
Correct |
1185 ms |
76652 KB |
Output is correct |
11 |
Correct |
2421 ms |
106908 KB |
Output is correct |
12 |
Correct |
2546 ms |
119796 KB |
Output is correct |
13 |
Correct |
2593 ms |
116352 KB |
Output is correct |
14 |
Correct |
345 ms |
58548 KB |
Output is correct |
15 |
Correct |
352 ms |
60048 KB |
Output is correct |
16 |
Correct |
15 ms |
14848 KB |
Output is correct |
17 |
Correct |
16 ms |
14848 KB |
Output is correct |
18 |
Correct |
13 ms |
14848 KB |
Output is correct |
19 |
Correct |
10 ms |
14592 KB |
Output is correct |
20 |
Correct |
3796 ms |
139876 KB |
Output is correct |
21 |
Correct |
3569 ms |
131656 KB |
Output is correct |
22 |
Correct |
2863 ms |
118928 KB |
Output is correct |
23 |
Correct |
328 ms |
59956 KB |
Output is correct |
24 |
Correct |
124 ms |
27724 KB |
Output is correct |
25 |
Correct |
139 ms |
38224 KB |
Output is correct |
26 |
Correct |
137 ms |
38348 KB |
Output is correct |
27 |
Correct |
355 ms |
58676 KB |
Output is correct |
28 |
Correct |
376 ms |
59956 KB |
Output is correct |
29 |
Correct |
11 ms |
14592 KB |
Output is correct |
30 |
Correct |
11 ms |
14720 KB |
Output is correct |
31 |
Correct |
12 ms |
14848 KB |
Output is correct |
32 |
Correct |
16 ms |
14848 KB |
Output is correct |
33 |
Correct |
602 ms |
67900 KB |
Output is correct |
34 |
Correct |
1639 ms |
92380 KB |
Output is correct |
35 |
Correct |
2521 ms |
115688 KB |
Output is correct |
36 |
Correct |
3747 ms |
137436 KB |
Output is correct |
37 |
Correct |
1353 ms |
101732 KB |
Output is correct |
38 |
Correct |
2035 ms |
134976 KB |
Output is correct |
39 |
Correct |
1406 ms |
101612 KB |
Output is correct |
40 |
Correct |
2065 ms |
140120 KB |
Output is correct |
41 |
Correct |
1354 ms |
101664 KB |
Output is correct |
42 |
Correct |
2150 ms |
139004 KB |
Output is correct |
43 |
Correct |
339 ms |
58676 KB |
Output is correct |
44 |
Correct |
356 ms |
60084 KB |
Output is correct |
45 |
Correct |
425 ms |
57420 KB |
Output is correct |
46 |
Correct |
524 ms |
56044 KB |
Output is correct |
47 |
Correct |
1251 ms |
63212 KB |
Output is correct |
48 |
Correct |
2225 ms |
106532 KB |
Output is correct |
49 |
Correct |
135 ms |
38348 KB |
Output is correct |
50 |
Correct |
136 ms |
38348 KB |
Output is correct |
51 |
Correct |
145 ms |
38476 KB |
Output is correct |
52 |
Correct |
147 ms |
38860 KB |
Output is correct |
53 |
Correct |
144 ms |
38860 KB |
Output is correct |
54 |
Correct |
151 ms |
38860 KB |
Output is correct |