#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
#define scan(x) do{while((x=getchar())<'0'); for(x-='0'; '0'<=(_=getchar()); x=(x<<3)+(x<<1)+_-'0');}while(0)
char _;
#define complete_unique(a) a.erase(unique(a.begin(),a.end()),a.end())
#define all(a) a.begin(),a.end()
#define println printf("\n");
#define readln(x) getline(cin,x);
#define pb push_back
#define endl "\n"
#define INT_INF 0x3f3f3f3f
#define LL_INF 0x3f3f3f3f3f3f3f3f
#define MOD 1000000007
#define mp make_pair
#define fastio cin.tie(0); cin.sync_with_stdio(0);
#define MAXN 300005
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef unordered_map<int,int> umii;
typedef pair<int,int> pii;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef pair<int,pii> triple;
typedef int8_t byte;
mt19937 g1(time(0));
int randint(int a, int b){return uniform_int_distribution<int>(a, b)(g1);}
ll randlong(ll a,ll b){return uniform_int_distribution<long long>(a, b)(g1);}
ll gcd(ll a, ll b){return b == 0 ? a : gcd(b, a % b);}
ll lcm(ll a, ll b){return a*b/gcd(a,b);}
ll fpow(ll b, ll exp, ll mod){if(exp == 0) return 1;ll t = fpow(b,exp/2,mod);if(exp&1) return t*t%mod*b%mod;return t*t%mod;}
ll divmod(ll i, ll j, ll mod){i%=mod,j%=mod;return i*fpow(j,mod-2,mod)%mod;}
struct node{
int l,r;
ll lazy0,lazy1,sum,cnt; //lazy0 for sum, lazy1 for cnt
node *lft,*rgt;
node(int l, int r){
this->l = l;
this->r = r;
this->lazy0 = this->lazy1 = this->sum = this->cnt = 0;
lft = rgt = 0;
}
};
struct segTree{
node *rt;
inline void push_down(node *n){
int mid = (n->l+n->r)/2;
if(!n->lft) n->lft = new node(n->l,mid);
if(!n->rgt) n->rgt = new node(mid+1,n->r);
n->lft->lazy0+=n->lazy0, n->rgt->lazy0+=n->lazy0;
n->lft->sum+=n->lazy0, n->rgt->sum+=n->lazy0;
n->lazy0 = 0;
n->lft->lazy1+=n->lazy1, n->rgt->lazy1+=n->lazy1;
n->lft->cnt+=n->lazy1, n->rgt->cnt+=n->lazy1;
n->lazy1 = 0;
}
void update(int l, int r, node *n, ll v, int s){
if(n->l == l && n->r == r){
if(s == 0) n->lazy0+=v, n->sum+=v;
else n->lazy1+=v, n->cnt+=v;
return;
}
if(n->lazy0 || n->lazy1) push_down(n);
int mid = (n->l+n->r)/2;
if(r <= mid){
if(!n->lft) n->lft = new node(n->l,mid);
update(l,r,n->lft,v,s);
}else if(l > mid){
if(!n->rgt) n->rgt = new node(mid+1,n->r);
update(l,r,n->rgt,v,s);
}else{
if(!n->lft) n->lft = new node(n->l,mid);
if(!n->rgt) n->rgt = new node(mid+1,n->r);
update(l,mid,n->lft,v,s);
update(mid+1,r,n->rgt,v,s);
}
}
ll query(int pos, node *n, int s){
if(n->l == n->r){
if(s == 0) return n->sum;
return n->cnt;
}
if(n->lazy0 || n->lazy1) push_down(n);
int mid = (n->l+n->r)/2;
if(pos <= mid){
if(!n->lft) n->lft = new node(n->l,mid);
return query(pos,n->lft,s);
}
if(!n->rgt) n->rgt = new node(mid+1,n->r);
return query(pos,n->rgt,s);
}
};
int len,num_q,arr[MAXN];
segTree bit[MAXN];
set<int> zeros;
inline void update(int xl, int xr, int l, int r, ll v, int s){
// for(int i=xl; i<=xr; i++)
// bit[i].update(l,r,bit[i].rt,v);
for(int i=xl; i<=len+1; i+=i&-i)
bit[i].update(l,r,bit[i].rt,v,s);
for(int i=xr+1; i<=len+1; i+=i&-i)
bit[i].update(l,r,bit[i].rt,-v,s);
}
inline ll query(int a, int b, int s){
ll res = 0;
// res = bit[a].query(b,bit[a].rt);
for(int i=a; i>0; i-=i&-i)
res+=bit[i].query(b,bit[i].rt,s);
return res;
}
//inline void cupdate(int xl, int xr, int l, int r, ll v){
//// for(int i=xl; i<=xr; i++)
//// cnt[i].update(l,r,cnt[i].rt,v);
// for(int i=xl; i<=len+1; i+=i&-i)
// cnt[i].update(l,r,cnt[i].rt,v);
// for(int i=xr+1; i<=len+1; i+=i&-i)
// cnt[i].update(l,r,cnt[i].rt,-v);
//}
//
//inline ll cquery(int a, int b){
// ll res = 0;
//// res = cnt[a].query(b,cnt[a].rt);
// for(int i=a; i>0; i-=i&-i)
// res+=cnt[i].query(b,cnt[i].rt);
// return res;
//}
int main(){
// freopen("data.in","r",stdin);
fastio; cin>>len>>num_q;
for(int i=1; i<=len; i++){
char c; cin>>c;
arr[i] = c-'0';
}
for(int i=1; i<=len+1; i++){
bit[i].rt = new node(1,len+1);
// cnt[i].rt = new node(1,len+1,0,0);
// bit[i].build(1,len+1,1);
// cnt[i].build(1,len+1,1);
}
zeros.insert(0); zeros.insert(len+1);
// cupdate(1,len+1,1,len+1,1);
update(1,len+1,1,len+1,1,1);
for(int a=1; a<=len; a++){
if(arr[a] == 0){
zeros.insert(a);
int l = *(--zeros.lower_bound(a));
int r = len+1;
// update(bit,l+1,a,a+1,r-1,i);
// cupdate(l+1,a,a+1,r,-1);
update(l+1,a,a+1,r,-1,1);
}
}
for(int i=1; i<=num_q; i++){
string op; int a; cin>>op>>a;
if(op == "toggle"){
int l = *(--zeros.lower_bound(a));
int r = *zeros.upper_bound(a);
if(arr[a] == 0){
update(l+1,a,a+1,r,-i,0);
update(l+1,a,a+1,r,1,1);
zeros.erase(a);
//(previous value)+(t-i)
//(previous value)+t-i
}else{
update(l+1,a,a+1,r,i,0);
update(l+1,a,a+1,r,-1,1);
zeros.insert(a);
//(previous value)-(t-i)
//(previous value)-t+i
}
arr[a] = !arr[a];
}else{
int b; cin>>b;
ll f = query(a,b,1)*i;
ll s = query(a,b,0);
cout<<f+s<<endl;
}
}
// cout<<"lol"<<endl;
}
/*
5 4
01001
toggle 3
toggle 2
toggle 4
query 2 6
ans=1
5 7
11011
query 1 2
query 1 2
query 1 6
query 3 4
toggle 3
query 3 4
query 1 6
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
500 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
476 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
524 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
359 ms |
4676 KB |
Output is correct |
2 |
Correct |
507 ms |
5608 KB |
Output is correct |
3 |
Correct |
1307 ms |
20700 KB |
Output is correct |
4 |
Runtime error |
1102 ms |
524288 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
2168 KB |
Output is correct |
2 |
Correct |
8 ms |
2424 KB |
Output is correct |
3 |
Correct |
7 ms |
2552 KB |
Output is correct |
4 |
Correct |
6 ms |
2172 KB |
Output is correct |
5 |
Runtime error |
1283 ms |
524288 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
3064 KB |
Output is correct |
2 |
Correct |
7 ms |
2808 KB |
Output is correct |
3 |
Correct |
7 ms |
2424 KB |
Output is correct |
4 |
Correct |
7 ms |
1784 KB |
Output is correct |
5 |
Runtime error |
1086 ms |
524288 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
500 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
476 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
524 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
359 ms |
4676 KB |
Output is correct |
9 |
Correct |
507 ms |
5608 KB |
Output is correct |
10 |
Correct |
1307 ms |
20700 KB |
Output is correct |
11 |
Runtime error |
1102 ms |
524288 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
12 |
Halted |
0 ms |
0 KB |
- |