#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <queue>
#include <set>
typedef long long llong;
const int MAXN = 500000 + 10;
const llong INF = 1e18;
int n;
llong d, u;
struct Event
{
int pos;
int time;
llong cost;
friend bool operator < (const Event &a, const Event &b)
{
return a.time < b.time || (a.time == b.time && a.pos < b.pos);
}
};
struct SegmentTree
{
llong tree[4*MAXN];
void build(int l, int r, int node)
{
if (l == r)
{
tree[node] = -INF;
return;
}
int mid = (l + r) / 2;
build(l, mid, 2*node);
build(mid + 1, r, 2*node + 1);
tree[node] = std::max(tree[2*node], tree[2*node + 1]);
}
void update(int l, int r, int node, int queryPos, llong queryVal)
{
if (l == r)
{
tree[node] = std::max(tree[node], queryVal);
return;
}
int mid = (l + r) / 2;
if (queryPos <= mid) update(l, mid, 2*node, queryPos, queryVal);
else update(mid + 1, r, 2*node + 1, queryPos, queryVal);
tree[node] = std::max(tree[2*node], tree[2*node + 1]);
}
llong query(int l, int r, int node, int queryL, int queryR)
{
if (queryL <= l && r <= queryR)
{
return tree[node];
}
llong res = -INF;
int mid = (l + r) / 2;
if (queryL <= mid) res = query(l, mid, 2*node, queryL, queryR);
if (mid + 1 <= queryR) res = std::max(res, query(mid + 1, r, 2*node + 1, queryL, queryR));
return res;
}
void build()
{
build(1, MAXN - 1, 1);
}
void update(int pos, int val)
{
update(1, MAXN - 1, 1, pos, val);
}
llong query(int l, int r)
{
return query(1, MAXN - 1, 1, l, r);
}
};
SegmentTree treeU;
SegmentTree treeD;
std::vector <std::pair <llong,int>> byTime[MAXN];
std::vector <llong> prefix[MAXN];
Event event[MAXN];
llong dp[MAXN];
llong sumToTime(int time, int pos)
{
int l = -1, r = byTime[time].size(), mid;
while (l < r - 1)
{
mid = (l + r) / 2;
if (byTime[time][mid].first <= pos) l = mid;
else r = mid;
}
if (l == -1) return 0;
return prefix[time][l];
}
llong sumFromTime(int time, int pos)
{
return prefix[time].back() - sumToTime(time, pos - 1);
}
void solve()
{
treeU.build();
treeD.build();
for (int i = 1 ; i < MAXN ; ++i)
{
if (byTime[i].empty()) continue;
std::sort(byTime[i].begin(), byTime[i].end());
prefix[i].resize(byTime[i].size());
prefix[i][0] = byTime[i][0].second;
for (int j = 1 ; j < prefix[i].size() ; ++j)
{
prefix[i][j] = prefix[i][j - 1] + byTime[i][j].second;
}
}
std::sort(event + 1, event + 1 + n);
int last = 1;
std::vector <std::pair <int,int>> ranges;
ranges.push_back({0, 0});
for (int i = 2 ; i <= n ; ++i)
{
if (event[i].time != event[i - 1].time)
{
ranges.push_back({last, i - 1});
last = i;
}
}
ranges.push_back({last, n});
std::reverse(ranges.begin(), ranges.end());
for (const auto &[l, r] : ranges)
{
for (int i = r ; i >= l ; --i)
{
if (event[i].pos <= event[0].pos) dp[i] = -(event[0].pos - event[i].pos) * d;
else dp[i] = -(event[i].pos - event[0].pos) * u;
dp[i] = std::max(dp[i], treeU.query(1, event[i].pos) - event[i].pos * u);
dp[i] = std::max(dp[i], treeD.query(event[i].pos, MAXN - 1) + event[i].pos * d);
treeU.update(event[i].pos, dp[i] + event[i].cost + event[i].pos * u);
treeD.update(event[i].pos, dp[i] + event[i].cost - event[i].pos * d);
}
}
std::cout << dp[0] << '\n';
}
void input()
{
std::cin >> n >> u >> d >> event[0].pos;
for (int i = 1 ; i <= n ; ++i)
{
std::cin >> event[i].time >> event[i].pos >> event[i].cost;
byTime[event[i].time].push_back({event[i].pos, event[i].cost});
}
}
void fastIOI()
{
std::ios_base :: sync_with_stdio(0);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
}
int main()
{
fastIOI();
input();
solve();
return 0;
}
Compilation message
salesman.cpp: In function 'void solve()':
salesman.cpp:126:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
126 | for (int j = 1 ; j < prefix[i].size() ; ++j)
| ~~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
47452 KB |
Output is correct |
2 |
Correct |
12 ms |
47452 KB |
Output is correct |
3 |
Correct |
12 ms |
47448 KB |
Output is correct |
4 |
Correct |
13 ms |
47708 KB |
Output is correct |
5 |
Correct |
16 ms |
48124 KB |
Output is correct |
6 |
Correct |
36 ms |
49328 KB |
Output is correct |
7 |
Correct |
75 ms |
55488 KB |
Output is correct |
8 |
Correct |
123 ms |
58848 KB |
Output is correct |
9 |
Correct |
191 ms |
62476 KB |
Output is correct |
10 |
Runtime error |
90 ms |
65536 KB |
Execution killed with signal 9 |
11 |
Runtime error |
104 ms |
65536 KB |
Execution killed with signal 9 |
12 |
Runtime error |
118 ms |
65536 KB |
Execution killed with signal 9 |
13 |
Runtime error |
118 ms |
65536 KB |
Execution killed with signal 9 |
14 |
Runtime error |
138 ms |
65536 KB |
Execution killed with signal 9 |
15 |
Runtime error |
165 ms |
65536 KB |
Execution killed with signal 9 |
16 |
Runtime error |
156 ms |
65536 KB |
Execution killed with signal 9 |
17 |
Correct |
11 ms |
47668 KB |
Output is correct |
18 |
Incorrect |
12 ms |
47452 KB |
Output isn't correct |
19 |
Incorrect |
12 ms |
47452 KB |
Output isn't correct |
20 |
Incorrect |
13 ms |
47708 KB |
Output isn't correct |
21 |
Incorrect |
14 ms |
47744 KB |
Output isn't correct |
22 |
Incorrect |
15 ms |
47836 KB |
Output isn't correct |
23 |
Incorrect |
18 ms |
47808 KB |
Output isn't correct |
24 |
Incorrect |
15 ms |
47708 KB |
Output isn't correct |
25 |
Incorrect |
95 ms |
54444 KB |
Output isn't correct |
26 |
Incorrect |
183 ms |
59252 KB |
Output isn't correct |
27 |
Incorrect |
275 ms |
65328 KB |
Output isn't correct |
28 |
Runtime error |
275 ms |
65536 KB |
Execution killed with signal 9 |
29 |
Runtime error |
114 ms |
65536 KB |
Execution killed with signal 9 |
30 |
Runtime error |
117 ms |
65536 KB |
Execution killed with signal 9 |