#include <bits/stdc++.h>
using namespace std;
struct node {
int l, r, val;
};
int n, q;
vector<node> seg;
vector<int> root;
void _update(int &idx, int i, int j, int pos, int val) {
if (idx == -1) {
idx = (int) seg.size();
seg.push_back({-1, -1, 0});
}
seg[idx].val += val;
if (i == j) {
return;
}
int mid = (i + j) / 2;
if (mid >= pos) {
_update(seg[idx].l, i, mid, pos, val);
} else {
_update(seg[idx].r, mid + 1, j, pos, val);
}
}
int _query(int idx, int i, int j, int l, int r) {
if (idx == -1 || i > r || j < l) {
return 0;
}
if (i >= l && j <= r) {
return seg[idx].val;
}
return _query(seg[idx].l, i, (i + j) / 2, l, r) + _query(seg[idx].r, (i + j) / 2 + 1, j, l, r);
}
void update(int i, int j, int val) {
for (; i <= n + 2; i += i & -i) {
_update(root[i], 1, n + 2, j, val);
}
}
int query(int i, int j) {
int ans = 0;
for (; i > 0; i -= i & -i) {
ans += _query(root[i], 1, n + 2, 1, j);
}
return ans;
}
void add(int i_1, int j_1, int i_2, int j_2, int val) {
update(i_1, j_1, val);
update(i_1, j_2 + 1, -val);
update(i_2 + 1, j_1, -val);
update(i_2 + 1, j_2 + 1, val);
}
struct comp {
inline bool operator()(const pair<int, int> &a, const pair<int, int> &b) {
return a.second < b.second;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> q;
set< pair<int, int>, comp> st;
for (int i = 1; i <= n + 1; i++) {
st.insert({i, i});
}
string s;
cin >> s;
for (int i = 1; i <= n; i++) {
if (s[i - 1] == '1') {
auto it = st.upper_bound({0, i});
pair<int, int> tmp = {prev(it)->first, it->second};
st.erase(prev(it)); st.erase(it);
st.insert(tmp);
}
}
root.assign(n + 2, -1);
seg.push_back({-1, -1, 0});
for (auto& p : st) {
add(p.first, p.first, p.second, p.second, -1);
}
for (int cur = 1; cur <= q; cur++) {
cin >> s;
if (s == "query") {
int l, r;
cin >> l >> r;
int ans = query(l, r);
if (st.lower_bound({0, l}) == st.lower_bound({0, r})) {
ans += cur + 1;
}
cout << ans << '\n';
} else {
int x;
cin >> x;
if (s[x - 1] == '1') {
auto it = st.lower_bound({0, x});
pair<int, int> tmp_1 = {it->first, x}, tmp_2 = {x + 1, it->second};
add(it->first, it->first, it->second, it->second, cur);
st.erase(it);
st.insert(tmp_1); st.insert(tmp_2);
s[x - 1] = '0';
} else {
auto it = st.upper_bound({0, x});
pair<int, int> tmp = {prev(it)->first, it->second};
/*for (int i = 1; i <= n + 2; i++) {
for (int j = 1; j <= n + 2; j++) {
cout << query(i, j) << ' ';
}
cout << endl;
}*/
add(prev(it)->first, prev(it)->first, it->second, it->second, -cur - 1);
/*for (int i = 1; i <= n + 2; i++) {
for (int j = 1; j <= n + 2; j++) {
cout << query(i, j) << ' ';
}
cout << endl;
}*/
st.erase(prev(it)); st.erase(it);
st.insert(tmp);
s[x - 1] = '1';
}
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
3 ms |
640 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
640 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
1088 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
1088 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
3 ms |
640 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |