#include<bits/stdc++.h>
/*
#pragma GCC optimize("-Ofast")
//#pragma GCC optimize("trapv")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx2,tune=native")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-funroll-loops")*/
#define I inline void
#define S struct
#define vi vector<int>
#define vii vector<pair<int,int>>
#define pii pair<int,int>
#define pll pair<ll,ll>
using namespace std ;
using ll = long long ;
using ld = long double ;
const int N = 1e5 + 7 , mod = 1e9 + 7 ;
const int inf = N ;
// How interesting!
int n , k ;
string s;
int a[N] ;
string tmp[N] ;
int ts ;
int solve(int l , int r){
int ret = 0 ;
int x = r - l + 1;
for(int i = 0 ;i <= ts ; i ++){
// cout<< tmp[i] <<" " ;
ret+= (tmp[i].substr(l , x) == string(x , '1') ) ;
}
// cout<<"-----\n" ;
//if(ts)
// exit(0) ;
return ret ;
}
int solve2(int l ,int r){
int ret = 0 ;
// l++ ; r -- ;
for(int i = 0 ;i <= ts ;i++){
vector<int> cont(n+1 , 0) , cont2(n + 1, 0) ;
vector<pair<int,int>>v ;
int cnt = 0 ;
if(l == r){
ret += 2 * (tmp[i][l] -'0') ;
continue;
}
for(int j = 0 ;j < n ;j ++){
if(tmp[i][j] == '1'){
if(cnt){
} else{
cont[j]++ ;
}
cnt++ ;
}else{
if(cnt){
v.push_back({ j - cnt , j -1 }) ;
cont2[j-1]-- ;
}else{
}
cnt = 0 ;
}
}
if(cnt){
v.push_back({n - cnt , n - 1 }) ;
cont2[n-1] -- ;
}
// ret += cont[l] ;
for(int j = 0 ;j <= l ;j ++){
ret += cont[j] + cont2[j];
}
// ret-= cont2[r] ;
for(int j = r ; j < n ;j ++)
ret -= cont2[j] + cont[j];
// cout<< ret <<"*\n" ;
for(auto u : v){
if(u.first> l && u.second < r)
ret += 2;
if(u.first > l && u.first < r)
ret -- ;
if(u.second > l && u.second < r)
ret -- ;
}
/* for(int j = 0 ;j < n ;j ++)
cout<< cont [j] <<" " ;
cout<<"\n" ;
for(int j = 0 ;j < n ;j ++)
cout<< cont2[j] <<" ";
cout<<"\n" ;*/
}
return ret / 2;
}
int main(){
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
/* tmp[0] = "11011" ;
n = tmp[0].size() ;
cout<< solve2( 0 , 4 ) ;
return 0 ;*/
//freopen("in.in" , "r" , stdin) ;
cin >> n >> k ;
cin >> s ;
tmp[0] = s;
for(int i = 0 ;i < n;i ++)
a[i] = s[i] - '0' ;
for(int i = 0 ;i < k ;i ++){
string t ;
int l , r;
cin >> t ;
if(t == "toggle"){
cin >> l ;
tmp[ts+1] = tmp[ts] ;ts++;
tmp[ts][l-1] = '1' - tmp[ts][l-1] + '0';
a[l] ^=1 ;
} else{
cin >> l >> r;
l-- ; r-=2;
if(r - l + 1 <= 2){
cout<< solve(l ,r) <<"\n" ;
}else
cout<< solve2(l , r) <<"\n";
tmp[ts + 1] = tmp[ts] ; ts ++ ;
}
}
return 0 ;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3456 KB |
Output is correct |
2 |
Correct |
3 ms |
3456 KB |
Output is correct |
3 |
Correct |
3 ms |
3456 KB |
Output is correct |
4 |
Correct |
5 ms |
3456 KB |
Output is correct |
5 |
Correct |
2 ms |
3456 KB |
Output is correct |
6 |
Correct |
5 ms |
3456 KB |
Output is correct |
7 |
Correct |
5 ms |
3456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5080 ms |
7808 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
4480 KB |
Output is correct |
2 |
Correct |
1049 ms |
4856 KB |
Output is correct |
3 |
Correct |
1939 ms |
4680 KB |
Output is correct |
4 |
Correct |
1719 ms |
4864 KB |
Output is correct |
5 |
Runtime error |
10 ms |
9108 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3169 ms |
4656 KB |
Output is correct |
2 |
Correct |
3263 ms |
4800 KB |
Output is correct |
3 |
Correct |
2289 ms |
4752 KB |
Output is correct |
4 |
Correct |
135 ms |
4600 KB |
Output is correct |
5 |
Runtime error |
10 ms |
9108 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3456 KB |
Output is correct |
2 |
Correct |
3 ms |
3456 KB |
Output is correct |
3 |
Correct |
3 ms |
3456 KB |
Output is correct |
4 |
Correct |
5 ms |
3456 KB |
Output is correct |
5 |
Correct |
2 ms |
3456 KB |
Output is correct |
6 |
Correct |
5 ms |
3456 KB |
Output is correct |
7 |
Correct |
5 ms |
3456 KB |
Output is correct |
8 |
Execution timed out |
5080 ms |
7808 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |