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;
struct node {
int l, r, val;
};
int n, q, cnt = 0;
vector<node> seg;
vector<int> root;
void _update(int &idx, int i, int j, int pos, int val) {
if (idx == -1) {
idx = cnt++;
}
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 + 3, -1);
seg.assign(16000000, {-1, -1, 0});
for (auto& p : st) {
add(p.first, p.first, p.second, p.second, -1);
}
for (int cur = 1; cur <= q; cur++) {
string t;
cin >> t;
if (t == "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, x + 1, x, it->second, cur + 1);
st.erase({it->first, it->second});
st.insert(tmp_1); st.insert(tmp_2);
s[x - 1] = '0';
} else {
auto it = st.upper_bound({0, x});
int i_1 = prev(it)->first, j_1 = prev(it)->second, i_2 = it->first, j_2 = it->second;
pair<int, int> tmp = {i_1, j_2};
add(i_1, i_2, j_1, j_2, -cur - 1);
st.erase({i_1, j_1}); st.erase({i_2, j_2});
st.insert(tmp);
s[x - 1] = '1';
}
}
}
return 0;
}
# | 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... |