이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#ifdef __LOCAL__
#include <debug_local.h>
#endif
using namespace std;
const int mxN = 3e5 + 2;
vector<int> t[4 * mxN], kk[mxN];
int sz[4 * mxN];
pair<int, int> bit[mxN * 30];
int ii;
vector<tuple<int, int, int, int>> g;
struct BIT {
int n;
int s;
BIT() { }
BIT(int _n) {
n = _n;
s = ii;
ii += n;
}
void add(int i, int a, int b) {
for (; i <= n; i = i | (i + 1)) bit[i + s].first += a, bit[i + s].second += b;
}
pair<int, int> sum(int r) {
pair<int, int> ret = {0, 0};
for (; r >= 1; r = (r & (r + 1)) - 1) {
ret.first += bit[r + s].first;
ret.second += bit[r + s].second;
}
return ret;
}
pair<int, int> sum(int l, int r) {
auto a = sum(r);
auto b = sum(l - 1);
return {a.first - b.first, a.second - b.second};
}
} bb[4 * mxN];
void build(int x, int lx, int rx) {
if (lx == rx) {
t[x] = kk[lx];
t[x].push_back(1e9);
sort(t[x].begin(), t[x].end());
t[x].erase(unique(t[x].begin(), t[x].end()), t[x].end());
sz[x] = t[x].size();
bb[x] = BIT(sz[x]);
return;
}
int mx = (lx + rx) >> 1;
build(x << 1, lx, mx);
build(x << 1 | 1, mx + 1, rx);
merge(t[x << 1].begin(), t[x << 1].end(), t[x << 1 | 1].begin(), t[x << 1 | 1].end(), back_inserter(t[x]));
t[x].erase(unique(t[x].begin(), t[x].end()), t[x].end());
sz[x] = t[x].size();
bb[x] = BIT(sz[x]);
}
void update(int x, int lx, int rx, int i, int y, int timer, int tt) {
if (lx == rx) {
int yy = lower_bound(t[x].begin(), t[x].end(), y) - t[x].begin() + 1;
bb[x].add(yy, timer, tt);
return;
}
int mx = (lx + rx) >> 1;
if (i <= mx) update(x << 1, lx, mx, i, y, timer, tt);
else update(x << 1 | 1, mx + 1, rx, i, y, timer, tt);
int yy = lower_bound(t[x].begin(), t[x].end(), y) - t[x].begin() + 1;
bb[x].add(yy, timer, tt);
}
pair<int, int> query(int x, int lx, int rx, int l, int r, int y) {
if (lx > r || l > rx) return {0, 0};
if (l <= lx && r >= rx) {
int yy = lower_bound(t[x].begin(), t[x].end(), y) - t[x].begin();
return bb[x].sum(yy + 1, sz[x]);
}
int mx = (lx + rx) >> 1;
auto a = query(x << 1, lx, mx, l, r, y);
auto b = query(x << 1 | 1, mx + 1, rx, l, r, y);
return {a.first + b.first, a.second + b.second};
}
void testCase() {
int n, q;
cin >> n >> q;
{
string s;
cin >> s;
s = '0' + s;
s += '0';
int l = -1;
set<pair<int, int>> st;
for (int i = 1; i <= n; i++) {
if (s[i] == '1' && s[i - 1] != '1') l = i;
if (s[i] == '1' && s[i + 1] != '1') {
g.push_back({0, 1, l, i + 1});
st.insert({l, i + 1});
}
}
for (int time = 1; time <= q; time++) {
string t;
cin >> t;
if (t[0] == 'q') {
int a, b;
cin >> a >> b;
g.push_back({time, 3, a, b});
} else {
int i;
cin >> i;
if (s[i] == '1') {
auto p = *prev(st.upper_bound({i, 1e9}));
st.erase(p);
g.push_back({time, 2, p.first, p.second});
if (s[i - 1] == '1') {
g.push_back({time, 1, p.first, i});
st.insert({p.first, i});
}
if (s[i + 1] == '1') {
g.push_back({time, 1, i + 1, p.second});
st.insert({i + 1, p.second});
}
s[i] = '0';
} else {
if (s[i + 1] == '1' && s[i - 1] == '1') {
auto p = *st.upper_bound({i, 0});
g.push_back({time, 2, p.first, p.second});
st.erase(p);
auto pp = *prev(st.upper_bound({i, 0}));
st.erase(pp);
g.push_back({time, 2, pp.first, pp.second});
st.insert({pp.first, p.second});
g.push_back({time, 1, pp.first, p.second});
} else if (s[i + 1] == '1') {
auto p = *st.upper_bound({i, 0});
st.erase(p);
g.push_back({time, 2, p.first, p.second});
st.insert({i, p.second});
g.push_back({time, 1, i, p.second});
} else if (s[i - 1] == '1') {
auto p = *prev(st.lower_bound({i, 0}));
st.erase(p);
g.push_back({time, 2, p.first, p.second});
st.insert({p.first, i + 1});
g.push_back({time, 1, p.first, i + 1});
} else {
g.push_back({time, 1, i, i + 1});
st.insert({i, i + 1});
}
s[i] = '1';
}
}
}
}
sort(g.begin(), g.end());
for (auto [a, b, c, d] : g) {
kk[c].push_back(d);
}
build(1, 1, n);
for (auto [a, b, c, d] : g) {
if (b == 1) {
update(1, 1, n, c, d, -a, 1);
} else if (b == 2) {
update(1, 1, n, c, d, a, -1);
} else {
auto x = query(1, 1, n, 1, c, d);
cout << 1LL * a * x.second + x.first << "\n";
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
testCase();
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... |