QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#536#91153#21650. 【PR #4】赌徒DaiRuiChen007HasretFailed.2024-02-16 12:22:252024-02-16 12:22:26

Details

Extra Test:

Accepted
time: 0ms
memory: 5856kb

input:

1
1000000000 1000000000 1

output:

-8

result:

ok 1 number(s): "-8"

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#91153#21650. 【PR #4】赌徒Hasret100 ✓156ms30880kbC++201.5kb2023-03-27 15:05:182023-03-27 15:05:19

answer

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
#define LL long long
const int N = 5e5;
using namespace std;

#define L(i, s, t) for(int i = s; i <= t; ++i)
#define R(i, t, s) for(int i = t; i >= s; --i)

template<typename Tp>
void read(Tp &x) {
    x = 0; int w = 1; char c = getchar(); for(; c < '0' || c > '9'; c = getchar()) if (c == '-') w = -1;
    for(; c <= '9' && c >= '0'; c = getchar()) x = x * 10 + c - '0'; x *= w;
}

int n; LL sum;
struct Node{
	LL x, v;
}a[2 * N + 5];
LL s[2 * N + 5];

int q[2 * N + 5], tot;

LL calc(int x, int y) {return s[x] + s[y] - 4 * a[x].x * 1ll * a[y].x;}
LL ans = -1e18;
int main() {
	// freopen("t.in", "r", stdin);
	read(n);
	L(i, 1, n) {
		LL x, y, v; read(x);read(y);read(v);
		a[2 * i - 1] = Node{x, 2 * v};
		a[2 * i] = Node{y, 2 * v};
		sum -= 4 * v;
	}

	sort(a + 1, a + 2 * n + 1, [](Node x, Node y) {return x.x < y.x;});
	for(int i = 1; i <= 2 * n; ++i) s[i] = s[i - 1] + a[i].v;
    // cout << a[2].v << endl;
	for(int i = 1; i <= 2 * n; ++i) {
		while(tot >= 2 && (__int128)(s[i] - s[q[tot]]) * (a[q[tot]].x - a[q[tot - 1]].x) > (__int128)(s[q[tot]] - s[q[tot - 1]]) * (a[i].x - a[q[tot]].x))
			--tot;
		q[++tot] = i;
	}

	int now = tot;
	ans = max(ans, -4ll);
	for(int i = 1; i <= 2 * n; ++i) {
		ans = max(ans, s[i] - 4 * 1ll * a[i].x);
		while(now >= 2 && calc(i, q[now]) < calc(i, q[now - 1]))
			--now;
		ans = max(ans, calc(i, q[now]));
	}
	printf("%lld\n", ans + sum);
    return 0;
}