QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#186945#3857. Single-track railwayballance#RE 0ms0kbC++171018b2023-09-24 13:28:002023-09-24 13:28:01

Judging History

你现在查看的是最新测评结果

  • [2023-09-24 13:28:01]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2023-09-24 13:28:00]
  • 提交

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;
	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
Runtime Error

input:

2
39
7
1 20
1 70
1 56
1 37
1 37
1 37
1 91

output:


result: