QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#346657 | #3857. Single-track railway | Kirill22# | RE | 1ms | 3816kb | C++23 | 1.3kb | 2024-03-08 20:46:13 | 2024-03-08 20:46:13 |
Judging History
answer
#include "bits/stdc++.h"
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<int> a(n - 1);
vector<long long> f(n - 1);
for (int i = 0; i + 1 < n; i++) {
cin >> a[i];
for (int j = i; j < n; j = j | (j + 1)) {
f[j] += a[i];
}
}
auto ask = [&] () {
long long sum = 0;
for (int i = n - 2; i >= 0; i = (i & (i + 1)) - 1) {
sum += f[i];
}
int pos = 0;
long long cur = 0;
// for (auto& x : f) cout << x << " "; cout << endl;
for (int j = 20; j >= 0; j--) {
if (pos + (1 << j) >= n) {
continue;
}
if (cur + f[pos + (1 << j) - 1] > sum / 2) {
continue;
}
cur += f[pos + (1 << j) - 1];
pos += (1 << j);
}
long long ans = abs(sum - cur - cur);
ans = min(ans, abs(sum - (cur + a[pos]) - (cur + a[pos])));
cout << ans << '\n';
};
ask();
int q;
cin >> q;
while (q--) {
int i, x;
cin >> i >> x;
i--;
for (int j = i; j < n; j = j | (j + 1)) {
f[j] += x - a[i];
}
a[i] = x;
ask();
}
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3760kb
input:
2 39 7 1 20 1 70 1 56 1 37 1 37 1 37 1 91
output:
39 20 70 56 37 37 37 91
result:
ok 8 lines
Test #2:
score: 0
Accepted
time: 1ms
memory: 3592kb
input:
3 8 41 0
output:
33
result:
ok single line: '33'
Test #3:
score: 0
Accepted
time: 1ms
memory: 3816kb
input:
30 86 100 80 30 23 17 90 98 20 3 43 31 49 14 14 47 13 44 7 30 50 29 67 68 56 69 28 61 81 10 21 23 7 99 16 70 3 50 4 17 26 42 7 37 10 43 26 83 13 96
output:
8 19 28 5 3 10 11 17 5 18 1
result:
ok 11 lines
Test #4:
score: -100
Runtime Error
input:
150 7 76 71 86 28 49 57 12 30 79 91 52 27 49 5 30 2 27 31 49 69 73 57 53 57 6 94 35 30 16 51 71 50 82 34 31 65 2 63 91 20 89 92 11 39 88 36 40 26 16 46 20 87 89 74 43 99 39 55 60 82 27 52 83 20 95 22 16 1 51 64 79 53 59 88 13 94 64 73 47 67 77 24 9 59 49 70 77 49 3 62 24 16 70 10 95 78 16 75 64 44 4...