#include <bits/stdc++.h>
using namespace std;
using lint = __int128_t;
const lint INF = 8e18;
const int MAXN = 2e5 + 5;
template<typename A, typename B>
void chmin(A &a, const B &b) { a = min(a, b); }
template<typename A, typename B>
void chmax(A &a, const B &b) { a = max(a, b); }
__int128_t ReadInt() {
long long x;
cin >> x;
return (__int128_t) x;
}
void Write(__int128_t x) {
cout << (long long) x << "\n";
}
struct Line {
lint a, b;
Line() : a(0), b(INF) {}
Line(lint a, lint b) : a(a), b(b) {}
lint get(lint x) {
return a * x + b;
}
};
class LiChao {
private:
struct Node {
Line line = Line();
Node *lc = nullptr;
Node *rc = nullptr;
};
lint sz;
Node *root = nullptr;
void InsertLineKnowingly(Node* &n, lint tl, lint tr, Line x) {
if (n == nullptr) n = new Node();
if (n->line.get(tl) > x.get(tl)) swap(n->line, x);
if (n->line.get(tr) <= x.get(tr)) return;
if (tl == tr) return;
lint mid = (tl + tr) / 2;
if (n->line.get(mid) < x.get(mid)) {
InsertLineKnowingly(n->rc, mid + 1, tr, x);
} else {
swap(n->line, x);
InsertLineKnowingly(n->lc, tl, mid, x);
}
}
void InsertLine(Node* &n, lint tl, lint tr, lint l, lint r, Line x) {
if (tr < l || r < tl || tl > tr || l > r) return;
if (n == nullptr) n = new Node();
if (l <= tl && tr <= r) return InsertLineKnowingly(n, tl, tr, x);
lint mid = (tl + tr) / 2;
InsertLine(n->lc, tl, mid, l, r, x);
InsertLine(n->rc, mid + 1, tr, l, r, x);
}
lint Query(Node* &n, lint tl, lint tr, lint x) {
if (n == nullptr) return INF;
if (tl == tr) return n->line.get(x);
lint res = n->line.get(x);
lint mid = (tl + tr) / 2;
if (x <= mid) {
res = min(res, Query(n->lc, tl, mid, x));
} else {
res = min(res, Query(n->rc, mid + 1, tr, x));
}
return res;
}
public:
LiChao() {}
LiChao(lint sz) : sz(sz) {}
void InsertLine(lint l, lint r, Line x) {
return InsertLine(root, 0, sz - 1, l, r, x);
}
lint Query(lint x) {
return min(INF, Query(root, 0, sz - 1, x));
}
};
int N, M;
lint X, W, T;
LiChao li_chao(INF);
lint S[MAXN], P[MAXN];
pair<lint, lint> A[MAXN];
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
X = ReadInt();
N = ReadInt();
M = ReadInt();
W = ReadInt();
T = ReadInt();
for (int i = 0; i < N; i++) {
S[i] = ReadInt();
}
S[N] = X;
vector<int> order(N + 1);
iota(begin(order), end(order), 0);
sort(begin(order), end(order), [&](int i, int j) {
return (S[i] % T) < (S[j] % T);
});
for (int i = 0; i < M; i++) {
A[i].first = ReadInt();
A[i].second = ReadInt();
}
sort(A, A + M);
for (int i = 0; i < M; i++) {
P[i + 1] = P[i] + A[i].second;
}
// At time t1 delete [a, b].
// At time t2 delete [c, d].
// Then [a, b] and [c, d] will never overlap in optimal answer.
// If they overlap, we can delete the overlapping region at min(t1, t2) to get more benefit.
// This is possible since we can keep extending both a and b to the left until 0.
vector<lint> dp(M + 1, INF);
dp[0] = (X + T - 1) / T * W;
for (int i = 0, ptr = 0; i < M; i++) {
chmin(dp[i + 1], dp[i] + (X + T - 1 - A[i].first) / T * W);
li_chao.InsertLine(0, INF - 1, Line(-i, dp[i] - P[i]));
while (ptr <= N) {
int p = upper_bound(A, A + M, make_pair(S[order[ptr]] % T, (__int128_t) -1)) - A;
if (p < i + 1) {
ptr += 1;
continue;
}
if (p == i + 1) {
lint x = S[order[ptr]] / T * W;
chmin(dp[i + 1], (i + 1) * x + P[i + 1] + li_chao.Query(x));
ptr += 1;
} else {
break;
}
}
}
Write(dp[M]);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
404 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
400 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
416 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
384 KB |
Output is correct |
22 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
404 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
400 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
416 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
384 KB |
Output is correct |
22 |
Correct |
0 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
1 ms |
384 KB |
Output is correct |
31 |
Correct |
1 ms |
384 KB |
Output is correct |
32 |
Correct |
1 ms |
384 KB |
Output is correct |
33 |
Correct |
1 ms |
384 KB |
Output is correct |
34 |
Correct |
1 ms |
384 KB |
Output is correct |
35 |
Correct |
1 ms |
384 KB |
Output is correct |
36 |
Correct |
1 ms |
384 KB |
Output is correct |
37 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
404 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
400 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
416 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
384 KB |
Output is correct |
22 |
Correct |
0 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
1 ms |
384 KB |
Output is correct |
31 |
Correct |
1 ms |
384 KB |
Output is correct |
32 |
Correct |
1 ms |
384 KB |
Output is correct |
33 |
Correct |
1 ms |
384 KB |
Output is correct |
34 |
Correct |
1 ms |
384 KB |
Output is correct |
35 |
Correct |
1 ms |
384 KB |
Output is correct |
36 |
Correct |
1 ms |
384 KB |
Output is correct |
37 |
Correct |
0 ms |
384 KB |
Output is correct |
38 |
Correct |
3 ms |
512 KB |
Output is correct |
39 |
Correct |
3 ms |
512 KB |
Output is correct |
40 |
Correct |
3 ms |
640 KB |
Output is correct |
41 |
Correct |
4 ms |
512 KB |
Output is correct |
42 |
Correct |
4 ms |
512 KB |
Output is correct |
43 |
Correct |
3 ms |
512 KB |
Output is correct |
44 |
Correct |
4 ms |
640 KB |
Output is correct |
45 |
Correct |
4 ms |
512 KB |
Output is correct |
46 |
Correct |
4 ms |
640 KB |
Output is correct |
47 |
Correct |
2 ms |
512 KB |
Output is correct |
48 |
Correct |
3 ms |
512 KB |
Output is correct |
49 |
Correct |
4 ms |
640 KB |
Output is correct |
50 |
Correct |
3 ms |
512 KB |
Output is correct |
51 |
Correct |
3 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
404 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
400 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
416 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
384 KB |
Output is correct |
22 |
Correct |
0 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
1 ms |
384 KB |
Output is correct |
31 |
Correct |
1 ms |
384 KB |
Output is correct |
32 |
Correct |
1 ms |
384 KB |
Output is correct |
33 |
Correct |
1 ms |
384 KB |
Output is correct |
34 |
Correct |
1 ms |
384 KB |
Output is correct |
35 |
Correct |
1 ms |
384 KB |
Output is correct |
36 |
Correct |
1 ms |
384 KB |
Output is correct |
37 |
Correct |
0 ms |
384 KB |
Output is correct |
38 |
Correct |
3 ms |
512 KB |
Output is correct |
39 |
Correct |
3 ms |
512 KB |
Output is correct |
40 |
Correct |
3 ms |
640 KB |
Output is correct |
41 |
Correct |
4 ms |
512 KB |
Output is correct |
42 |
Correct |
4 ms |
512 KB |
Output is correct |
43 |
Correct |
3 ms |
512 KB |
Output is correct |
44 |
Correct |
4 ms |
640 KB |
Output is correct |
45 |
Correct |
4 ms |
512 KB |
Output is correct |
46 |
Correct |
4 ms |
640 KB |
Output is correct |
47 |
Correct |
2 ms |
512 KB |
Output is correct |
48 |
Correct |
3 ms |
512 KB |
Output is correct |
49 |
Correct |
4 ms |
640 KB |
Output is correct |
50 |
Correct |
3 ms |
512 KB |
Output is correct |
51 |
Correct |
3 ms |
512 KB |
Output is correct |
52 |
Correct |
311 ms |
17232 KB |
Output is correct |
53 |
Correct |
306 ms |
22648 KB |
Output is correct |
54 |
Correct |
401 ms |
21624 KB |
Output is correct |
55 |
Correct |
410 ms |
22136 KB |
Output is correct |
56 |
Correct |
358 ms |
22264 KB |
Output is correct |
57 |
Correct |
353 ms |
21980 KB |
Output is correct |
58 |
Correct |
312 ms |
22520 KB |
Output is correct |
59 |
Correct |
423 ms |
21880 KB |
Output is correct |
60 |
Correct |
401 ms |
21752 KB |
Output is correct |
61 |
Correct |
394 ms |
22008 KB |
Output is correct |
62 |
Correct |
380 ms |
21880 KB |
Output is correct |
63 |
Correct |
441 ms |
33528 KB |
Output is correct |
64 |
Correct |
298 ms |
22648 KB |
Output is correct |
65 |
Correct |
349 ms |
22648 KB |
Output is correct |
66 |
Correct |
276 ms |
22520 KB |
Output is correct |
67 |
Correct |
391 ms |
22652 KB |
Output is correct |
68 |
Correct |
373 ms |
22520 KB |
Output is correct |
69 |
Correct |
321 ms |
22392 KB |
Output is correct |
70 |
Correct |
324 ms |
22392 KB |
Output is correct |
71 |
Correct |
322 ms |
22340 KB |
Output is correct |
72 |
Correct |
338 ms |
22392 KB |
Output is correct |
73 |
Correct |
314 ms |
22264 KB |
Output is correct |
74 |
Correct |
305 ms |
22476 KB |
Output is correct |
75 |
Correct |
246 ms |
22392 KB |
Output is correct |
76 |
Correct |
236 ms |
22520 KB |
Output is correct |
77 |
Correct |
488 ms |
22476 KB |
Output is correct |
78 |
Correct |
384 ms |
22720 KB |
Output is correct |
79 |
Correct |
304 ms |
21880 KB |
Output is correct |
80 |
Correct |
301 ms |
21880 KB |
Output is correct |