QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#186959 | #3857. Single-track railway | ballance# | WA | 0ms | 3560kb | C++17 | 1.2kb | 2023-09-24 13:32:57 | 2023-09-24 13:32:57 |
Judging History
answer
#include<iostream>
using namespace std;
typedef long long ll;
const int N = 200010;
int n;
ll a[N];
#define lowbit(x) (x&-x)
void add(int x, int k)
{
while (x <= n)
a[x] += k, x += lowbit(x);
}
ll sum(int x)
{
ll ans = 0;
while (x)
ans += a[x], x -= lowbit(x);
return ans;
}
ll f()
{
ll total = sum(n);
int step = 1;
int cnt = 1;
ll ans = 1e15;
while (step)
{
ll cntlen = sum(cnt + step);
//ll cntdel = abs(total - 2 * cntlen);
if (cntlen <= total / 2)
cnt += step, step *= 2;
else
step /= 2;
}
ll ans1 = abs(total - 2 * sum(cnt));
ll ans2 = abs(total - 2 * sum(cnt + 1));
return min(ans1, ans2);
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
--n;
if (n == 1)
{
int k; cin >> k;
cout << k << '\n';
int t; cin >> t; while (t--)
{
int k; cin >> k;
cout << k << '\n';
}
exit(0);
}
for (int i = 1; i <= n; i++)
{
int k; cin >> k;
add(i, k);
}
cout << f() << '\n';
int t; cin >> t; while (t--)
{
int o; cin >> o;
int v; cin >> v;
int base = sum(o) - sum(o - 1);
add(o, v - base);
cout << f() << '\n';
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3560kb
input:
2 39 7 1 20 1 70 1 56 1 37 1 37 1 37 1 91
output:
39 1 20 1 70 1 56 1
result:
wrong answer 2nd lines differ - expected: '20', found: '1'