#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){
if(in < 0 || in >= n) exit(1);
return mn[in+k/2];
}
int find(int in){
for(int i = in; i < n; i++) if(get(i) == 0) return i-1;
return n-1;
}
int rfind(int in){
for(int i = in; i >= 0; i--) if(get(i) == 0) return i+1;
return 0;
}
};
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);
}
}
map<pair<int, int>, int> mp;
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);
mp[{s, i}] = ranges.size();
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()});
}
}
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:80:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | for(int i = 0; i < tg.size(); i++){
| ~~^~~~~~~~~~~
street_lamps.cpp:127: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]
127 | for(int i = 0; i < ranges.size(); i++){
| ~~^~~~~~~~~~~~~~~
street_lamps.cpp:131: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]
131 | for(int qq = 0; qq < query.size(); qq++){
| ~~~^~~~~~~~~~~~~~
street_lamps.cpp:134: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]
134 | for(int i = 0; i < ranges.size(); i++){
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
332 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
82 ms |
7152 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 |
2 ms |
496 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
332 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |