제출 #541725

#제출 시각아이디문제언어결과실행 시간메모리
541725sidonLong Distance Coach (JOI17_coach)C++17
100 / 100
429 ms73188 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = __int128;
using i64 = int64_t;

const ll INF = 1e18;
const int SZ = 2e5+1;

struct Line {
	ll m {}, c {};
	ll operator()(const ll &x) { return m * x + c; }
};

struct LiChaoTree {
	Line v;
	LiChaoTree *L = NULL, *R = NULL;
	void insert(Line x, ll l, ll r) {
		ll m = (l + r) >> 1;
		if(v(m) < x(m)) swap(v, x);
		if(r - l < 2) return;
		x.m > v.m ? (R ? : R = new LiChaoTree)->insert(x, m, r):
					(L ? : L = new LiChaoTree)->insert(x, l, m);
	}
	ll operator()(const ll &x, ll l, ll r) {
		if(r - l < 2) return v(x);
		ll m = (l + r) >> 1;
		return x < m ? max(v(x), L ? (*L)(x, l, m) : 0):
					   max(v(x), R ? (*R)(x, m, r) : 0);
	}
};

i64 N, M, X, W, T, S[SZ], D[SZ], C[SZ];

int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin >> X >> N >> M >> W >> T;

	vector<pair<ll, int>> a(N+M+1);

	for(int i = 0; i < N; ++i) {
		cin >> S[i];
		a[i+0] = {S[i] % T, i};
	}
	a[N] = {X % T, N};
	S[N++] = X;

	for(int i = 0; i < M; ++i) {
		cin >> D[i] >> C[i];
		a[i+N] = {D[i] % T, i + N};
	}

	sort(begin(a), end(a), [](const auto &i, const auto &j) {
		return i.first < j.first;
	});

	LiChaoTree lct;

	ll cost {}, dp {}, cnt {}, ext = ll((X - ll(1)) / T + 1) * ll(W);

	for(int i = 1; i <= N + M; ++i) {
		int c = a[i-1].second;
		if(c < N) {
			ll f = (S[c] - ll(1)) / T;
			dp = max(dp, cost - f * cnt * ll(W) + lct(f, 0, X + 1));
		} else {
			c -= N; ++cnt;
			ll total = ((X - ll(1)) / T) + (D[c] < (X % T));
			ext += total * ll(W);
			cost += total * ll(W) - ll(C[c]);
		}
		lct.insert(Line{cnt * ll(W), dp - cost}, 0, X + 1);
	}
	cout << int64_t(ext - dp);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...