#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;
map <int,int> fen[N];
void addd(ll idx,ll idy,ll val){
for (ll x=idx;x<N;x+=x & (-x)){
for (ll y=idy;y<N;y+=y & (-y)){
fen[x][y]+=val;
}
}
}
void add(ll l,ll r,ll val){
addd(l,l,val);
addd(l,r+1,-val);
addd(r+1,l,-val);
addd(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=idy;y;y-=y & (-y)){
s+=fen[x][y];
}
}
return s;
}
ll a[N];
int32_t main(){
sync;
set <pair <pii,int> > s;
ll n,q;
cin >> n >> q;
for (int i=1;i<=n;i++){
char c;
cin >> c;
a[i]=c-'0';
}
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;
}
for (int i=1;i<=q;i++){
string type;
cin >> type;
if (type=="toggle"){
ll id;
cin >> 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;
add(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){
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,r;
cin >> l >> r;
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;
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14464 KB |
Output is correct |
2 |
Correct |
9 ms |
14464 KB |
Output is correct |
3 |
Correct |
10 ms |
14464 KB |
Output is correct |
4 |
Correct |
10 ms |
14592 KB |
Output is correct |
5 |
Correct |
12 ms |
14616 KB |
Output is correct |
6 |
Correct |
9 ms |
14464 KB |
Output is correct |
7 |
Correct |
10 ms |
14464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2304 ms |
18824 KB |
Output is correct |
2 |
Correct |
3302 ms |
19680 KB |
Output is correct |
3 |
Execution timed out |
5091 ms |
28512 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
15992 KB |
Output is correct |
2 |
Correct |
40 ms |
16140 KB |
Output is correct |
3 |
Correct |
43 ms |
15992 KB |
Output is correct |
4 |
Correct |
12 ms |
15104 KB |
Output is correct |
5 |
Execution timed out |
5079 ms |
227168 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
15232 KB |
Output is correct |
2 |
Correct |
25 ms |
15744 KB |
Output is correct |
3 |
Correct |
28 ms |
15872 KB |
Output is correct |
4 |
Correct |
35 ms |
15736 KB |
Output is correct |
5 |
Execution timed out |
5106 ms |
403360 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14464 KB |
Output is correct |
2 |
Correct |
9 ms |
14464 KB |
Output is correct |
3 |
Correct |
10 ms |
14464 KB |
Output is correct |
4 |
Correct |
10 ms |
14592 KB |
Output is correct |
5 |
Correct |
12 ms |
14616 KB |
Output is correct |
6 |
Correct |
9 ms |
14464 KB |
Output is correct |
7 |
Correct |
10 ms |
14464 KB |
Output is correct |
8 |
Correct |
2304 ms |
18824 KB |
Output is correct |
9 |
Correct |
3302 ms |
19680 KB |
Output is correct |
10 |
Execution timed out |
5091 ms |
28512 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |