#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
struct segtree{
int k, n;
vector<int> mn;
segtree(int _n){
n = _n;
k = 1;
while(k < n) k*=2;
k*=2;
mn.resize(k, 1);
}
void toggle(int in){
in+=k/2;
mn[in] = !mn[in];
in/=2;
while(in > 0){
mn[in] = min(mn[2*in], mn[2*in+1]);
in/=2;
}
}
int get(int in){
return mn[in+k/2];
}
int find(int in, int nd, int a, int b){
if(mn[nd] == 1) return -1;
if(in > b) return -1;
if(a == b) return a;
int md = (a+b)/2;
int x;
return (x = find(in, 2*nd, a, md)) == -1 ? find(in, 2*nd+1, md+1, b) : x;
}
int find(int in){
int x = find(in, 1, 0, k/2-1);
if(x == -1) x = n;
x--;
return x;
}
int rfind(int in, int nd, int a, int b){
if(mn[nd] == 1) return -1;
if(in < a) return -1;
if(a == b) return a;
int md = (a+b)/2;
int x;
return (x = rfind(in, 2*nd+1, md+1, b)) == -1 ? rfind(in, 2*nd, a, md) : x;
}
int rfind(int in){
int x = rfind(in, 1, 0, k/2-1);
x++;
return x;
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, q; cin >> n >> q;
segtree seg(n);
for(int i = 0; i < n; i++){
char x; cin >> x;
if(x == '0'){
seg.toggle(i);
}
}
vector<pair<int, int>> ranges;
vector<int> value;
vector<bool> bl;
for(int i = 0; i < n;){
if(seg.get(i) == 0){
i++;
continue;
}
int s = i; i = seg.find(i);
ranges.pb({s, i});
value.pb(0);
bl.pb(0);
i++;
}
vector<int> tg;
vector<tuple<int, int, int>> query;
for(int i = 0; i < q; i++){
str tt; cin >> tt;
if(tt == "toggle"){
int in; cin >> in; in--;
tg.pb(in);
}
else{
int a, b; cin >> a >> b; a--, b-=2;
query.pb({a, b, query.size()});
}
}
map<pair<int, int>, int> mp;
for(int i = 0; i < tg.size(); i++){
int in = tg[i];
if(seg.get(in) == 1){
int l = seg.rfind(in), r = seg.find(in);
seg.toggle(in);
value[mp[{l, r}]] = i-value[mp[{l, r}]];
bl[mp[{l, r}]] = 1;
mp[{l, r}] = -1;
if(in-1 >= 0 && seg.get(in-1) == 1){
l = seg.rfind(in-1);
mp[{l, in-1}] = ranges.size();
ranges.pb({l, in-1});
value.pb(i);
bl.pb(0);
}
if(in+1 < n && seg.get(in+1) == 1){
r = seg.rfind(in+1);
mp[{in+1, r}] = ranges.size();
value.pb(i);
bl.pb(0);
}
}
else{
int l, r;
if(in-1 >= 0 && seg.get(in-1) == 1){
l = seg.rfind(in-1);
int ii = mp[{l, in-1}];
value[ii] = i-value[ii];
bl[ii] = 1;
mp[{l, in-1}] = -1;
}
if(in+1 < n && seg.get(in+1) == 1){
r = seg.rfind(in+1);
int ii = mp[{in+1, r}];
value[ii] = i-value[ii];
bl[ii] = 1;
mp[{in+1, r}] = -1;
}
seg.toggle(in);
l = seg.rfind(in), r = seg.find(in);
mp[{l, r}] = ranges.size();
ranges.pb({l, r});
value.pb(i);
bl.pb(0);
}
}
for(int i = 0; i < ranges.size(); i++){
cerr << ranges[i].f << " " << ranges[i].sc << " " << value[i] << " " << bl[i] << " d\n";
}
vector<int> ans(query.size());
for(int qq = 0; qq < query.size(); qq++){
auto [a, b, in] = query[qq];
ans[in] = 0;
for(int i = 0; i < ranges.size(); i++){
if(!(a >= ranges[i].f && b <= ranges[i].sc)) continue;
if(bl[i]) ans[in]+=value[i];
else ans[in]+=in+tg.size()-value[i]+1;
}
}
for(int x: ans) cout << x << "\n";
}
Compilation message
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:97:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
97 | for(int i = 0; i < tg.size(); i++){
| ~~^~~~~~~~~~~
street_lamps.cpp:144:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
144 | for(int i = 0; i < ranges.size(); i++){
| ~~^~~~~~~~~~~~~~~
street_lamps.cpp:148:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::tuple<int, int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
148 | for(int qq = 0; qq < query.size(); qq++){
| ~~~^~~~~~~~~~~~~~
street_lamps.cpp:151:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
151 | for(int i = 0; i < ranges.size(); i++){
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
460 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
76 ms |
8216 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
460 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
332 KB |
Output is correct |
2 |
Runtime error |
3 ms |
580 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
460 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |