QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#263447 | #186. Street Lamps | Camillus | 0 | 0ms | 3408kb | C++20 | 6.1kb | 2023-11-24 20:54:19 | 2023-11-24 20:54:20 |
answer
#include "bits/stdc++.h"
using ll = long long;
using namespace std;
pair<int, ll> operator+(const pair<int, ll> &A, const pair<int, ll> &B) {
return {A.first + B.first, A.second + B.second};
}
namespace implicit_st {
struct node {
int cnt = 0;
ll sum = 0;
int left = 0, right = 0;
};
vector<node> tree = { node() };
int new_node() {
tree.emplace_back();
return (int)tree.size() - 1;
}
void add(int l, int r, int v, int x, int lx, int rx) {
assert(x != 0);
if (l <= lx && rx <= r) {
tree[x].cnt += 1;
tree[x].sum += v;
return;
}
int mx = (lx + rx) / 2;
if (r <= mx) {
if (!tree[x].left) {
tree[x].left = new_node();
}
add(l, r, v, tree[x].left, lx, mx);
}
if (l >= mx) {
if (!tree[x].right) {
tree[x].right = new_node();
}
add(l, r, v, tree[x].right, mx, rx);
}
}
pair<int, ll> get(int i, int x, int lx, int rx) {
if (x == 0) {
return {0, 0ll};
}
pair<int, ll> cur(tree[x].cnt, tree[x].sum);
if (rx - lx == 1) {
return cur;
}
int mx = (lx + rx) / 2;
if (i < mx) {
return get(i, tree[x].left, lx, mx) + cur;
} else {
return get(i, tree[x].right, mx, rx) + cur;
}
}
struct st {
int root;
int n;
st(int n) : n(n) {
root = new_node();
}
void add(int l, int r, int v) const {
implicit_st::add(l, r, v, root, 0, n);
}
pair<int, ll> get(int i) const {
return implicit_st::get(i, root, 0, n);
}
};
} // namespace implicit_st
struct main_st {
vector<implicit_st::st> tree;
int n;
main_st(int n) : n(n) {
tree.assign(4 * n, implicit_st::st(0));
for (auto &item : tree) {
item = implicit_st::st(n + 1);
}
}
void add(pair<int, int> lefts, pair<int, int> rights, int time) {
add(lefts.first, lefts.second + 1, rights.first, rights.second + 1, time, 0, 0, n);
}
pair<int, ll> get(int a, int b) {
return get(a, b, 0, 0, n);
}
private:
void add(int lq, int rq, int l, int r, int v, int x, int lx, int rx) {
if (lq <= lx && rx <= rq) {
tree[x].add(l, r, v);
return;
}
if (lq >= rx || lx >= rq) {
return;
}
add(lq, rq, l, r, v, x * 2 + 1, lx, (lx + rx) / 2);
add(lq, rq, l, r, v, x * 2 + 2, (lx + rx) / 2, rx);
}
pair<int, ll> get(int i, int v, int x, int lx, int rx) {
if (rx - lx == 1) {
return tree[x].get(v);
}
int mx = (lx + rx) / 2;
if (i < mx) {
return tree[x].get(v) + get(i, v, x * 2 + 1, lx, mx);
} else {
return tree[x].get(v) + get(i, v, x * 2 + 2, mx, rx);
}
}
};
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
main_st B(n), E(n);
set<pair<int, int>> S;
string s;
cin >> s;
for (int i = 0; i < n; ) {
if (s[i] == '1') {
int l = i, r = i;
while (s[i] == '1') {
r = i + 1;
i++;
}
for (int j = l; j < r; j++) {
B.add(make_pair(j, j), make_pair(j + 1, r), 0);
}
S.emplace(l, r);
} else {
i++;
}
}
for (int t = 1; t <= q; t++) {
string type;
cin >> type;
if (type == "query") {
int a, b;
cin >> a >> b;
a--, b--;
auto x = B.get(a, b);
auto y = E.get(a, b);
ll ans = y.second - x.second;
if (x.first != y.first) {
ans += t;
}
cout << ans << '\n';
} else {
int i;
cin >> i;
i--;
if (s[i] == '1') {
s[i] = '0';
auto it = prev(S.upper_bound(make_pair(i, i)));
int l = it->first;
int r = it->second;
E.add(make_pair(l, i), make_pair(i + 1, r), t);
S.erase(it);
if (l < i) {
S.emplace(l, i);
}
if (i + 1 < r) {
S.emplace(i + 1, r);
}
} else {
s[i] = '1';
if ((i - 1 >= 0 && s[i - 1] == '0') && (i + 1 < n && s[i + 1] == '0')) {
B.add(make_pair(i, i), make_pair(i + 1, i + 1), t);
} else if (!(i - 1 >= 0 && s[i - 1] == '0') && (i + 1 < n && s[i + 1] == '0')) {
auto it = S.upper_bound(make_pair(i, i));
int r = it->second;
S.erase(it);
S.emplace(i, r);
B.add(make_pair(i, i), make_pair(i + 1, r), t);
} else if ((i - 1 >= 0 && s[i - 1] == '0') && !(i + 1 < n && s[i + 1] == '0')) {
auto it = prev(S.upper_bound(make_pair(i, i)));
int l = it->first;
S.erase(it);
S.emplace(l, i + 1);
B.add(make_pair(l, i), make_pair(i + 1, i + 1), t);
} else {
auto it = S.upper_bound(make_pair(i, i));
int r = it->second;
S.erase(it);
it = prev(S.upper_bound(make_pair(i, i)));
int l = it->first;
S.erase(it);
S.emplace(l, r);
B.add(make_pair(l, i), make_pair(i + 1, r), t);
}
}
}
}
return 0;
}
詳細信息
Subtask #1:
score: 0
Time Limit Exceeded
Test #1:
score: 20
Accepted
time: 0ms
memory: 3408kb
input:
5 7 11011 query 1 2 query 1 2 query 1 6 query 3 4 toggle 3 query 3 4 query 1 6
output:
1 2 0 0 1 2
result:
ok 6 lines
Test #2:
score: -20
Time Limit Exceeded
input:
5 50 01001 query 1 6 toggle 3 toggle 3 toggle 2 toggle 3 toggle 2 toggle 4 query 2 6 query 2 3 query 1 3 query 3 5 toggle 3 query 2 6 query 1 5 query 2 3 query 3 6 toggle 5 toggle 1 toggle 2 toggle 4 query 1 6 query 4 5 toggle 3 query 5 6 toggle 2 query 4 6 toggle 5 toggle 5 toggle 2 query 4 5 query...
output:
result:
Subtask #2:
score: 0
Time Limit Exceeded
Test #9:
score: 0
Time Limit Exceeded
input:
100 300000 1100100000000101010010100111010001100010001100111101000010111110001101101110100100100110101010110010 query 13 14 query 42 43 toggle 64 query 78 79 toggle 85 query 35 36 toggle 35 query 4 5 toggle 5 query 4 5 query 42 43 query 35 36 query 13 14 query 14 15 toggle 15 toggle 31 query 20 21 q...
output:
result:
Subtask #3:
score: 0
Time Limit Exceeded
Test #17:
score: 0
Time Limit Exceeded
input:
1000 1003 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
result:
Subtask #4:
score: 0
Time Limit Exceeded
Test #30:
score: 0
Time Limit Exceeded
input:
1000 1003 10111011001010101101100010101100100010100110001000000001001100111110101100110100010001111101101100110111110100011000111101100100000110110010101011101001101110111100010100100000110001010001111101001010100101011111010000001110111110001011010111101100000001001110101110011111000101101100011010...
output:
result:
Subtask #5:
score: 0
Skipped
Dependency #1:
0%