#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
#if 0
const int MOD = ;
inline int lgput(int a, int b) {
int ans = 1;
while(b > 0) {
if(b & 1) ans = (1LL * ans * a) % MOD;
b >>= 1;
a = (1LL * a * a) % MOD;
}
return ans;
}
inline void mod(int &x) {
if(x >= MOD)
x -= MOD;
}
inline void add(int &x, int y) {
x += y;
mod(x);
}
inline void sub(int &x, int y) {
x += MOD - y;
mod(x);
}
inline void mul(int &x, int y) {
x = (1LL * x * y) % MOD;
}
inline int inv(int x) {
return lgput(x, MOD - 2);
}
#endif
#if 0
int fact[], invfact[];
inline void prec(int n) {
fact[0] = 1;
for(int i = 1; i <= n; i++) {
fact[i] = (1LL * fact[i - 1] * i) % MOD;
}
invfact[n] = lgput(fact[n], MOD - 2);
for(int i = n - 1; i >= 0; i--) {
invfact[i] = (1LL * invfact[i + 1] * (i + 1)) % MOD;
}
}
inline int comb(int n, int k) {
if(n < k) return 0;
return (1LL * fact[n] * (1LL * invfact[k] * invfact[n - k] % MOD)) % MOD;
}
#endif
using namespace std;
const int INF = 2e9;
const int MAXN = (int) 5e5 + 5;
vector < pair <int, int> > offers[MAXN + 1];
struct Fenwick {
vector <int> aib;
int n;
inline void init(int _n) {
n = _n;
aib.resize(n + 1, -INF);
}
inline void update(int pos, int val) {
for(int i = pos; i <= n; i += lsb(i)) {
aib[i] = max(aib[i], val);
}
}
inline int query(int pos) {
int ans = -INF;
for(int i = pos; i > 0; i -= lsb(i)) {
ans = max(ans, aib[i]);
}
return ans;
}
};
int dp[MAXN + 1], aux[MAXN + 1];
int main() {
#if 0
ifstream cin("A.in");
ofstream cout("A.out");
#endif
int i, n, u, d, s;
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> u >> d >> s;
for(i = 1; i <= n; i++) {
int t, x, c;
cin >> t >> x >> c;
offers[t].push_back({x, c});
}
fill(dp, dp + MAXN + 1, -INF);
dp[s] = 0;
Fenwick fen1, fen2;
fen1.init(MAXN), fen2.init(MAXN);
fen1.update(MAXN - s, -u * s);
fen2.update(s, d * s);
for(int t = 1; t <= MAXN; t++) {
sort(offers[t].begin(), offers[t].end());
int sz = offers[t].size();
for(i = 1; i < sz; i++) {
offers[t][i].second += offers[t][i - 1].second;
}
for(auto it : offers[t]) {
aux[it.first] = -INF;
}
for(auto it : offers[t]) {
aux[it.first] = max(aux[it.first], max(-it.first * d + fen2.query(it.first), it.first * u + fen1.query(MAXN - it.first)));
}
int mx = -INF;
for(i = sz - 1; i >= 0; i--) {
int x = offers[t][i].first;
mx = max(mx, aux[x] - u * x + offers[t][i].second);
dp[x] = max(dp[x], mx - (i > 0 ? offers[t][i - 1].second : 0) + u * x);
}
mx = -INF;
for(i = 0; i < sz; i++) {
int x = offers[t][i].first;
mx = max(mx, aux[x] + d * x - (i > 0 ? offers[t][i - 1].second : 0));
dp[x] = max(dp[x], mx - d * x + offers[t][i].second);
}
for(auto it : offers[t]) {
int x = it.first;
fen1.update(MAXN - x, dp[x] - u * x);
fen2.update(x, dp[x] + d * x);
}
}
auto get = [&](int a, int b) {
if(a <= b) return (b - a) * d;
return (a - b) * u;
};
int ans = -INF;
for(i = 1; i <= MAXN; i++) {
ans = max(ans, dp[i] - get(i, s));
}
cout << ans;
return 0;
}
/*
dp[t][x]
x1 <= x2
dp[t + 1][x2] = dp[t][x] + sp[t + 1][x2] - sp[t + 1][x1 - 1] - U * (x - x1) - D * (x2 - x1)
dp[t + 1][x1] = dp[t][x] + sp[t + 1][x2] - sp[t + 1][x1 - 1] - D * (x2 - x) - U * (x2 - x1)
(x1 <= x)
dp[t + 1][x2] =
sp[t + 1][x2] - D * x2
dp[t][x] - U * x
-sp[t + 1][x1 - 1] + x1 * (D + U)
(x2 >= x)
dp[t + 1][x1] =
sp[t + 1][x2] - x2 * (D + U)
dp[t][x] + D * x
-sp[t + 1][x1 - 1] + U * x1
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
18044 KB |
Output is correct |
2 |
Correct |
20 ms |
17912 KB |
Output is correct |
3 |
Correct |
20 ms |
18040 KB |
Output is correct |
4 |
Correct |
21 ms |
18040 KB |
Output is correct |
5 |
Correct |
23 ms |
18168 KB |
Output is correct |
6 |
Correct |
41 ms |
20856 KB |
Output is correct |
7 |
Correct |
72 ms |
22468 KB |
Output is correct |
8 |
Correct |
122 ms |
25016 KB |
Output is correct |
9 |
Correct |
159 ms |
27000 KB |
Output is correct |
10 |
Correct |
272 ms |
34100 KB |
Output is correct |
11 |
Correct |
318 ms |
34684 KB |
Output is correct |
12 |
Correct |
389 ms |
38520 KB |
Output is correct |
13 |
Correct |
411 ms |
38776 KB |
Output is correct |
14 |
Correct |
488 ms |
43596 KB |
Output is correct |
15 |
Correct |
449 ms |
43640 KB |
Output is correct |
16 |
Correct |
496 ms |
43672 KB |
Output is correct |
17 |
Correct |
21 ms |
17912 KB |
Output is correct |
18 |
Correct |
20 ms |
17912 KB |
Output is correct |
19 |
Correct |
21 ms |
18040 KB |
Output is correct |
20 |
Correct |
21 ms |
18168 KB |
Output is correct |
21 |
Correct |
21 ms |
18040 KB |
Output is correct |
22 |
Correct |
25 ms |
18168 KB |
Output is correct |
23 |
Correct |
23 ms |
18040 KB |
Output is correct |
24 |
Correct |
23 ms |
18296 KB |
Output is correct |
25 |
Correct |
73 ms |
22264 KB |
Output is correct |
26 |
Correct |
128 ms |
24772 KB |
Output is correct |
27 |
Correct |
201 ms |
27844 KB |
Output is correct |
28 |
Correct |
234 ms |
29036 KB |
Output is correct |
29 |
Correct |
287 ms |
31352 KB |
Output is correct |
30 |
Correct |
291 ms |
32396 KB |
Output is correct |