This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
#define ar array
namespace sub1{
void solve(int n, int q){
vector<string> s(q + 1);
cin>>s[0];
for(int i=1;i<=q;i++){
s[i] = s[i-1];
string t; cin>>t;
if(t == "query"){
int l, r; cin>>l>>r;
--l, --r;
int res = 0;
for(int j=0;j<i;j++){ bool ok = 1;
for(int k=l;k<r;k++){
if(s[j][k] == '0') ok = 0;
} res += ok;
}
cout<<res<<"\n";
} else {
int p; cin>>p; p--;
s[i][p] = '1' - s[i][p] + '0';
}
}
}
};
namespace sub2{
/*
5 5
11011
query 1 2
query 1 2
query 3 4
toggle 3
query 3 4
*/
void solve(int n, int q, string& s, vector<ar<int, 3>>& qq){
vector<int> sum(n);
for(int i=0;i<n;i++){
if(s[i] == '1') sum[i] = 1;
}
for(int i=0;i<q;i++){
if(qq[i][0]){
int p = --qq[i][1];
if(s[p] == '0') sum[p] -= i, s[p] = '1';
else sum[p] += i, s[p] = '0';
} else {
int p = --qq[i][1];
cout<<sum[p] + (s[p] == '1') * i<<"\n";
}
}
}
};
namespace sub3{
const int N = 3e5 + 5;
struct ST{
int tree[N << 2];
ST(){
memset(tree, 63, sizeof tree);
}
void sett(int i, int v, int lx = 0, int rx = N, int x = 1){
if(lx == rx) { tree[x] = v; return; }
int m = (lx + rx) >> 1;
if(i <= m) sett(i, v, lx, m, x<<1);
else sett(i, v, m+1, rx, x<<1|1);
tree[x] = max(tree[x<<1], tree[x<<1|1]);
}
int get(int l, int r, int lx = 0, int rx = N, int x = 1){
if(lx > r || rx < l) return -1;
if(lx >= l && rx <= r) return tree[x];
int m = (lx + rx) >> 1;
return max(get(l, r, lx, m, x<<1), get(l, r, m+1, rx, x<<1|1));
}
}tree;
void solve(int n, int q, string& s, vector<ar<int, 3>>& qq){
for(int i=0;i<n;i++){
if(s[i] == '1') tree.sett(i, -1);
}
for(int i=0;i<q;i++){
if(qq[i][0]){
tree.sett(--qq[i][1], i);
} else {
int l = --qq[i][1], r = --qq[i][2];
int p = tree.get(l, r - 1);
cout<<max(0, i - p)<<"\n";
}
}
}
};
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n, q; cin>>n>>q;
if(n <= 100 && q <= 100){
sub1::solve(n, q);
} else {
string s; cin>>s;
bool is = 1;
vector<ar<int, 3>> qq(q);
for(int i=0;i<q;i++){
string t; cin>>t;
if(t == "query"){
cin>>qq[i][1]>>qq[i][2];
if(qq[i][2] - qq[i][1] != 1) is = 0;
} else { qq[i][0] = 1;
cin>>qq[i][1];
}
}
if(is){
sub2::solve(n, q, s, qq);
} else {
sub3::solve(n, q, s, qq);
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |