#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << " " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << " " << j << " " << #i << " " << q << " " << #p << endl;
#define show4(x,y) for(auto it:y) cout << it << " "; cout << #x << endl;
typedef pair<int,int>pii;
int fw[300005];
void update(int x, int v) {
for (; x<300005; x+=x&(-x)) fw[x] += v;
}
int sum(int x) {
int res = 0;
for(; x; x-=x&(-x)) res += fw[x];
return res;
}
int query(int x, int y){
return sum(y)-sum(x-1);
}
int n,q;
string s;
int ans[300005];
typedef pair<pii,pii>pi2;
vector<pi2>storage[300005];
vector<pi2>que[300005];
vector<pii>undo;
void dnc(int l, int r){
if(r<=l){
sort(storage[l].begin(),storage[l].end());
sort(que[l].begin(),que[l].end());
return;
}
vector<pi2>lstorage;
vector<pi2>lque;
int mid=(l+r)/2;
for(auto it:storage[l]){
if(it.second.first<=mid){
lstorage.push_back(it);
}
else if(it.second.first>mid){
storage[mid+1].push_back(it);
}
}
for(auto it:que[l]){
if(it.second.first<=mid){
lque.push_back(it);
}
else que[mid+1].push_back(it);
}
storage[l]=lstorage;
que[l]=lque;
lstorage.resize(0);
lque.resize(0);
dnc(l,mid);
dnc(mid+1,r);
//show3(l,l,mid,mid,r,r);
//cout << "storage" << endl;
//for(auto it:storage[l]){
//cout << it.first.first << " " << it.first.second << " " << it.second.first << " " << it.second.second << endl;
//}
vector<pi2>hold;
vector<pi2>hold2;
int cur=0;
for(auto it:que[mid+1]){
int lft=it.first.first;
int rgt=it.first.second;
int index=it.second.second;
while(cur<(int)storage[l].size()){
if(storage[l][cur].first.first<=lft){
int val=(storage[l][cur].second.first)*storage[l][cur].second.second;
update(storage[l][cur].first.second,val);
undo.push_back({storage[l][cur].first.second,-val});
cur++;
}
else{
break;
}
}
int inc=query(rgt,n);
ans[index]+=inc;
//show2(index,index,add,inc);
}
for(auto it:undo){
update(it.first,it.second);
}
undo.clear();
merge(storage[l].begin(),storage[l].end(),storage[mid+1].begin(),storage[mid+1].end(),back_inserter(hold));
merge(que[l].begin(),que[l].end(),que[mid+1].begin(),que[mid+1].end(),back_inserter(hold2));
storage[l].resize(0);
storage[mid+1].resize(0);
que[l].resize(0);
que[mid+1].resize(0);
storage[l]=hold;
que[l]=hold2;
hold.resize(0);
hold2.resize(0);
}
void solve(){
cin >> n >> q;
cin >> s;
s="0"+s+"0";
string temp;
int l,r;
int ptr=0;
set<pair<pii,int>>se;
for(int x=1;x<=n;x++){
if(s[x]=='1'){
int cur=x;
for(int y=x;y<=n;y++){
if(s[y]=='0') break;
cur=y;
}
se.insert({{cur,x},1LL});
x=cur;
}
}
for(int x=2;x<=q+1;x++){
cin >> temp;
if(temp=="toggle"){
cin >> l;
if(s[l]=='1'){
//turn off
auto it=se.lower_bound({{l,0LL},0LL});
pair<pii,int>hold=*it;
storage[0].push_back({{hold.first.second,hold.first.first},{x-1,1}});
storage[0].push_back({{hold.first.second,hold.first.first},{hold.second-1,-1}});
if(hold.first.second<l){
se.insert({{l-1,hold.first.second},x});
}
if(hold.first.first>l){
se.insert({{hold.first.first,l+1},x});
}
se.erase(se.find(hold));
s[l]='0';
}
else{
//turn on
if(s[l-1]=='1'&&s[l+1]=='1'){
auto it=se.lower_bound({{l,0LL},0LL});
pair<pii,int>hold,hold2;
hold=*it;
it--;
hold2=*it;
storage[0].push_back({{hold.first.second,hold.first.first},{x-1,1}});
storage[0].push_back({{hold.first.second,hold.first.first},{hold.second-1,-1}});
storage[0].push_back({{hold2.first.second,hold2.first.first},{x-1,1}});
storage[0].push_back({{hold2.first.second,hold2.first.first},{hold2.second-1,-1}});
se.insert({{hold.first.first,hold2.first.second},x});
se.erase(se.find(hold));
se.erase(se.find(hold2));
}
else if(s[l-1]=='1'){
auto it=se.lower_bound({{l,0LL},0LL});
it--;
pair<pii,int>hold=*it;
storage[0].push_back({{hold.first.second,hold.first.first},{x-1,1}});
storage[0].push_back({{hold.first.second,hold.first.first},{hold.second-1,-1}});
se.insert({{l,hold.first.second},x});
se.erase(se.find(hold));
}
else if(s[l+1]=='1'){
auto it=se.lower_bound({{l,0LL},0LL});
pair<pii,int>hold=*it;
storage[0].push_back({{hold.first.second,hold.first.first},{x-1,1}});
storage[0].push_back({{hold.first.second,hold.first.first},{hold.second-1,-1}});
se.insert({{hold.first.first,l},x});
se.erase(se.find(hold));
}
else{
se.insert({{l,l},x});
}
s[l]='1';
}
}
else{
cin >> l >> r;
r--;
auto it=se.lower_bound({{l,0LL},0LL});
pair<pii,int>hold=*it;
if(hold.first.second<=l&&hold.first.first>=r){
ans[ptr]+=x-1;
}
que[0].push_back({{l,r},{x,ptr++}});
}
}
for(auto it:se){
storage[0].push_back({{it.first.second,it.first.first},{q+1,1}});
storage[0].push_back({{it.first.second,it.first.first},{it.second-1,-1}});
}
se.clear();
//for(auto it:storage[0]){
//cout << it.first.first << " " << it.first.second << " " << it.second.first << " " << it.second.second << endl;
//}
dnc(0,q+1);
for(int x=0;x<ptr;x++){
cout << ans[x] << "\n";
}
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);
int t=1;
//cin >> t;
while(t--){
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
14936 KB |
Output is correct |
2 |
Correct |
4 ms |
14936 KB |
Output is correct |
3 |
Correct |
4 ms |
14940 KB |
Output is correct |
4 |
Correct |
4 ms |
14940 KB |
Output is correct |
5 |
Correct |
4 ms |
14944 KB |
Output is correct |
6 |
Correct |
4 ms |
14940 KB |
Output is correct |
7 |
Correct |
4 ms |
14940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
524 ms |
148976 KB |
Output is correct |
2 |
Correct |
524 ms |
149660 KB |
Output is correct |
3 |
Correct |
542 ms |
152460 KB |
Output is correct |
4 |
Correct |
717 ms |
187460 KB |
Output is correct |
5 |
Correct |
708 ms |
163344 KB |
Output is correct |
6 |
Correct |
560 ms |
194884 KB |
Output is correct |
7 |
Correct |
318 ms |
115728 KB |
Output is correct |
8 |
Correct |
338 ms |
113280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
15196 KB |
Output is correct |
2 |
Correct |
5 ms |
15288 KB |
Output is correct |
3 |
Correct |
5 ms |
15196 KB |
Output is correct |
4 |
Correct |
5 ms |
15036 KB |
Output is correct |
5 |
Correct |
605 ms |
185164 KB |
Output is correct |
6 |
Correct |
762 ms |
199692 KB |
Output is correct |
7 |
Correct |
687 ms |
161928 KB |
Output is correct |
8 |
Correct |
335 ms |
119252 KB |
Output is correct |
9 |
Correct |
177 ms |
73144 KB |
Output is correct |
10 |
Correct |
219 ms |
106052 KB |
Output is correct |
11 |
Correct |
204 ms |
103236 KB |
Output is correct |
12 |
Correct |
317 ms |
118200 KB |
Output is correct |
13 |
Correct |
319 ms |
119156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
15196 KB |
Output is correct |
2 |
Correct |
6 ms |
15192 KB |
Output is correct |
3 |
Correct |
5 ms |
15196 KB |
Output is correct |
4 |
Correct |
5 ms |
15472 KB |
Output is correct |
5 |
Correct |
476 ms |
149788 KB |
Output is correct |
6 |
Correct |
519 ms |
168400 KB |
Output is correct |
7 |
Correct |
540 ms |
193852 KB |
Output is correct |
8 |
Correct |
597 ms |
235928 KB |
Output is correct |
9 |
Correct |
397 ms |
168108 KB |
Output is correct |
10 |
Correct |
416 ms |
201008 KB |
Output is correct |
11 |
Correct |
394 ms |
169772 KB |
Output is correct |
12 |
Correct |
410 ms |
209272 KB |
Output is correct |
13 |
Correct |
397 ms |
173744 KB |
Output is correct |
14 |
Correct |
409 ms |
203716 KB |
Output is correct |
15 |
Correct |
314 ms |
118524 KB |
Output is correct |
16 |
Correct |
328 ms |
116836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
14936 KB |
Output is correct |
2 |
Correct |
4 ms |
14936 KB |
Output is correct |
3 |
Correct |
4 ms |
14940 KB |
Output is correct |
4 |
Correct |
4 ms |
14940 KB |
Output is correct |
5 |
Correct |
4 ms |
14944 KB |
Output is correct |
6 |
Correct |
4 ms |
14940 KB |
Output is correct |
7 |
Correct |
4 ms |
14940 KB |
Output is correct |
8 |
Correct |
524 ms |
148976 KB |
Output is correct |
9 |
Correct |
524 ms |
149660 KB |
Output is correct |
10 |
Correct |
542 ms |
152460 KB |
Output is correct |
11 |
Correct |
717 ms |
187460 KB |
Output is correct |
12 |
Correct |
708 ms |
163344 KB |
Output is correct |
13 |
Correct |
560 ms |
194884 KB |
Output is correct |
14 |
Correct |
318 ms |
115728 KB |
Output is correct |
15 |
Correct |
338 ms |
113280 KB |
Output is correct |
16 |
Correct |
6 ms |
15196 KB |
Output is correct |
17 |
Correct |
5 ms |
15288 KB |
Output is correct |
18 |
Correct |
5 ms |
15196 KB |
Output is correct |
19 |
Correct |
5 ms |
15036 KB |
Output is correct |
20 |
Correct |
605 ms |
185164 KB |
Output is correct |
21 |
Correct |
762 ms |
199692 KB |
Output is correct |
22 |
Correct |
687 ms |
161928 KB |
Output is correct |
23 |
Correct |
335 ms |
119252 KB |
Output is correct |
24 |
Correct |
177 ms |
73144 KB |
Output is correct |
25 |
Correct |
219 ms |
106052 KB |
Output is correct |
26 |
Correct |
204 ms |
103236 KB |
Output is correct |
27 |
Correct |
317 ms |
118200 KB |
Output is correct |
28 |
Correct |
319 ms |
119156 KB |
Output is correct |
29 |
Correct |
5 ms |
15196 KB |
Output is correct |
30 |
Correct |
6 ms |
15192 KB |
Output is correct |
31 |
Correct |
5 ms |
15196 KB |
Output is correct |
32 |
Correct |
5 ms |
15472 KB |
Output is correct |
33 |
Correct |
476 ms |
149788 KB |
Output is correct |
34 |
Correct |
519 ms |
168400 KB |
Output is correct |
35 |
Correct |
540 ms |
193852 KB |
Output is correct |
36 |
Correct |
597 ms |
235928 KB |
Output is correct |
37 |
Correct |
397 ms |
168108 KB |
Output is correct |
38 |
Correct |
416 ms |
201008 KB |
Output is correct |
39 |
Correct |
394 ms |
169772 KB |
Output is correct |
40 |
Correct |
410 ms |
209272 KB |
Output is correct |
41 |
Correct |
397 ms |
173744 KB |
Output is correct |
42 |
Correct |
409 ms |
203716 KB |
Output is correct |
43 |
Correct |
314 ms |
118524 KB |
Output is correct |
44 |
Correct |
328 ms |
116836 KB |
Output is correct |
45 |
Correct |
342 ms |
87712 KB |
Output is correct |
46 |
Correct |
325 ms |
88952 KB |
Output is correct |
47 |
Correct |
391 ms |
100888 KB |
Output is correct |
48 |
Correct |
688 ms |
183428 KB |
Output is correct |
49 |
Correct |
240 ms |
114528 KB |
Output is correct |
50 |
Correct |
238 ms |
114360 KB |
Output is correct |
51 |
Correct |
233 ms |
114940 KB |
Output is correct |
52 |
Correct |
240 ms |
114608 KB |
Output is correct |
53 |
Correct |
238 ms |
116408 KB |
Output is correct |
54 |
Correct |
248 ms |
113848 KB |
Output is correct |