#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
struct Endpoint
{
int x, y, segment, color;
bool operator <(const Endpoint &other) const
{
if (x != other.x)
return (x < other.x);
return (color < other.color);
}
};
struct Edge
{
Edge(int d, long long w) : dest(d), weight(w) {}
int dest;
long long weight;
bool operator <(const Edge &other) const
{
return (other.weight < weight);
}
};
const int MAXN = 1e5;
const int MAXG = 2e6;
const long long INFTY = 1e18;
int day[MAXN];
int left[MAXN], right[MAXN];
long long cost[MAXN];
Endpoint extr[2 * MAXN];
Endpoint bckp[2 * MAXN];
int nodes;
std::vector<Edge> graph[MAXG];
std::priority_queue<Edge> dijkstra;
long long minDist[MAXG];
void buildgraph(int first, int last)
{
if (last - first == 1)
return;
int mid = (first + last) / 2;
buildgraph(first, mid);
buildgraph(mid, last);
int previous = -1;
int iLeft = first, iSort = first;
for (int iRight = mid; iRight < last; iRight++)
{
while (iLeft < mid && extr[iLeft].y >= extr[iRight].y)
{
if (extr[iLeft].color == 0)
{
graph[nodes].emplace_back(extr[iLeft].segment, cost[extr[iLeft].segment]);
if (previous != -1)
graph[nodes].emplace_back(previous, 0);
previous = nodes;
nodes++;
}
bckp[iSort] = extr[iLeft];
iLeft++; iSort++;
}
if (extr[iRight].color == 1 && previous != -1)
graph[extr[iRight].segment].emplace_back(previous, 0);
bckp[iSort] = extr[iRight];
iSort++;
}
for (; iLeft < mid; iLeft++)
{
bckp[iSort] = extr[iLeft];
iSort++;
}
for (int iCpy = first; iCpy < last; iCpy++)
extr[iCpy] = bckp[iCpy];
}
void push(int node, long long dist)
{
for (Edge &edge : graph[node])
if (minDist[edge.dest] == INFTY)
{
minDist[edge.dest] = dist + edge.weight;
if (edge.weight == 0)
push(edge.dest, dist);
else
dijkstra.emplace(edge.dest, dist + edge.weight);
}
}
long long findcost(int people, int treatments)
{
std::fill_n(minDist, MAXG, INFTY);
for (int iTreat = 0; iTreat < treatments; iTreat++)
if (left[iTreat] == 1)
{
minDist[iTreat] = cost[iTreat];
dijkstra.emplace(iTreat, cost[iTreat]);
}
while (!dijkstra.empty())
{
int node = dijkstra.top().dest;
long long dist = dijkstra.top().weight;
dijkstra.pop();
push(node, dist);
}
long long minCost = INFTY;
for (int iTreat = 0; iTreat < treatments; iTreat++)
if (right[iTreat] == people)
minCost = std::min(minCost, minDist[iTreat]);
return minCost;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
int people, treatments;
std::cin >> people >> treatments;
for (int iTreat = 0; iTreat < treatments; iTreat++)
{
std::cin >> day[iTreat] >> left[iTreat] >> right[iTreat] >> cost[iTreat];
extr[iTreat * 2].x = day[iTreat] + left[iTreat];
extr[iTreat * 2].y = day[iTreat] - left[iTreat];
extr[iTreat * 2].segment = iTreat;
extr[iTreat * 2].color = 0;
extr[iTreat * 2 + 1].x = day[iTreat] + right[iTreat] + 1;
extr[iTreat * 2 + 1].y = day[iTreat] - right[iTreat] - 1;
extr[iTreat * 2 + 1].segment = iTreat;
extr[iTreat * 2 + 1].color = 1;
}
std::sort(extr, extr + 2 * treatments);
nodes = 2 * treatments;
buildgraph(0, 2 * treatments);
long long minpay = findcost(people, treatments);
if (minpay == INFTY)
std::cout << "-1\n";
else
std::cout << minpay << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
353 ms |
138872 KB |
Output is correct |
2 |
Correct |
362 ms |
136568 KB |
Output is correct |
3 |
Correct |
320 ms |
134680 KB |
Output is correct |
4 |
Correct |
307 ms |
134672 KB |
Output is correct |
5 |
Correct |
299 ms |
131548 KB |
Output is correct |
6 |
Correct |
315 ms |
132668 KB |
Output is correct |
7 |
Correct |
314 ms |
136440 KB |
Output is correct |
8 |
Correct |
285 ms |
124256 KB |
Output is correct |
9 |
Correct |
265 ms |
127712 KB |
Output is correct |
10 |
Correct |
362 ms |
131748 KB |
Output is correct |
11 |
Correct |
376 ms |
138380 KB |
Output is correct |
12 |
Correct |
379 ms |
136924 KB |
Output is correct |
13 |
Correct |
423 ms |
140544 KB |
Output is correct |
14 |
Correct |
429 ms |
140528 KB |
Output is correct |
15 |
Correct |
525 ms |
138440 KB |
Output is correct |
16 |
Correct |
401 ms |
138324 KB |
Output is correct |
17 |
Correct |
393 ms |
137536 KB |
Output is correct |
18 |
Correct |
444 ms |
136188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
62968 KB |
Output is correct |
2 |
Correct |
40 ms |
62976 KB |
Output is correct |
3 |
Correct |
40 ms |
62976 KB |
Output is correct |
4 |
Correct |
39 ms |
62972 KB |
Output is correct |
5 |
Correct |
42 ms |
62944 KB |
Output is correct |
6 |
Correct |
49 ms |
62972 KB |
Output is correct |
7 |
Correct |
42 ms |
62968 KB |
Output is correct |
8 |
Correct |
39 ms |
62968 KB |
Output is correct |
9 |
Correct |
38 ms |
62976 KB |
Output is correct |
10 |
Correct |
38 ms |
62976 KB |
Output is correct |
11 |
Correct |
57 ms |
62944 KB |
Output is correct |
12 |
Correct |
39 ms |
62944 KB |
Output is correct |
13 |
Correct |
39 ms |
62968 KB |
Output is correct |
14 |
Correct |
38 ms |
62968 KB |
Output is correct |
15 |
Correct |
40 ms |
62968 KB |
Output is correct |
16 |
Correct |
49 ms |
62968 KB |
Output is correct |
17 |
Correct |
42 ms |
62968 KB |
Output is correct |
18 |
Correct |
42 ms |
62976 KB |
Output is correct |
19 |
Correct |
41 ms |
62944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
62968 KB |
Output is correct |
2 |
Correct |
40 ms |
62976 KB |
Output is correct |
3 |
Correct |
40 ms |
62976 KB |
Output is correct |
4 |
Correct |
39 ms |
62972 KB |
Output is correct |
5 |
Correct |
42 ms |
62944 KB |
Output is correct |
6 |
Correct |
49 ms |
62972 KB |
Output is correct |
7 |
Correct |
42 ms |
62968 KB |
Output is correct |
8 |
Correct |
39 ms |
62968 KB |
Output is correct |
9 |
Correct |
38 ms |
62976 KB |
Output is correct |
10 |
Correct |
38 ms |
62976 KB |
Output is correct |
11 |
Correct |
57 ms |
62944 KB |
Output is correct |
12 |
Correct |
39 ms |
62944 KB |
Output is correct |
13 |
Correct |
39 ms |
62968 KB |
Output is correct |
14 |
Correct |
38 ms |
62968 KB |
Output is correct |
15 |
Correct |
40 ms |
62968 KB |
Output is correct |
16 |
Correct |
49 ms |
62968 KB |
Output is correct |
17 |
Correct |
42 ms |
62968 KB |
Output is correct |
18 |
Correct |
42 ms |
62976 KB |
Output is correct |
19 |
Correct |
41 ms |
62944 KB |
Output is correct |
20 |
Correct |
47 ms |
65540 KB |
Output is correct |
21 |
Correct |
70 ms |
65528 KB |
Output is correct |
22 |
Correct |
48 ms |
65528 KB |
Output is correct |
23 |
Correct |
48 ms |
65272 KB |
Output is correct |
24 |
Correct |
51 ms |
65784 KB |
Output is correct |
25 |
Correct |
52 ms |
65656 KB |
Output is correct |
26 |
Correct |
51 ms |
65656 KB |
Output is correct |
27 |
Correct |
52 ms |
65888 KB |
Output is correct |
28 |
Correct |
53 ms |
65656 KB |
Output is correct |
29 |
Correct |
52 ms |
65528 KB |
Output is correct |
30 |
Correct |
59 ms |
65532 KB |
Output is correct |
31 |
Correct |
48 ms |
65632 KB |
Output is correct |
32 |
Correct |
52 ms |
66296 KB |
Output is correct |
33 |
Correct |
52 ms |
66168 KB |
Output is correct |
34 |
Correct |
57 ms |
66040 KB |
Output is correct |
35 |
Correct |
52 ms |
66300 KB |
Output is correct |
36 |
Correct |
51 ms |
66168 KB |
Output is correct |
37 |
Correct |
51 ms |
66040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
353 ms |
138872 KB |
Output is correct |
2 |
Correct |
362 ms |
136568 KB |
Output is correct |
3 |
Correct |
320 ms |
134680 KB |
Output is correct |
4 |
Correct |
307 ms |
134672 KB |
Output is correct |
5 |
Correct |
299 ms |
131548 KB |
Output is correct |
6 |
Correct |
315 ms |
132668 KB |
Output is correct |
7 |
Correct |
314 ms |
136440 KB |
Output is correct |
8 |
Correct |
285 ms |
124256 KB |
Output is correct |
9 |
Correct |
265 ms |
127712 KB |
Output is correct |
10 |
Correct |
362 ms |
131748 KB |
Output is correct |
11 |
Correct |
376 ms |
138380 KB |
Output is correct |
12 |
Correct |
379 ms |
136924 KB |
Output is correct |
13 |
Correct |
423 ms |
140544 KB |
Output is correct |
14 |
Correct |
429 ms |
140528 KB |
Output is correct |
15 |
Correct |
525 ms |
138440 KB |
Output is correct |
16 |
Correct |
401 ms |
138324 KB |
Output is correct |
17 |
Correct |
393 ms |
137536 KB |
Output is correct |
18 |
Correct |
444 ms |
136188 KB |
Output is correct |
19 |
Correct |
57 ms |
62968 KB |
Output is correct |
20 |
Correct |
40 ms |
62976 KB |
Output is correct |
21 |
Correct |
40 ms |
62976 KB |
Output is correct |
22 |
Correct |
39 ms |
62972 KB |
Output is correct |
23 |
Correct |
42 ms |
62944 KB |
Output is correct |
24 |
Correct |
49 ms |
62972 KB |
Output is correct |
25 |
Correct |
42 ms |
62968 KB |
Output is correct |
26 |
Correct |
39 ms |
62968 KB |
Output is correct |
27 |
Correct |
38 ms |
62976 KB |
Output is correct |
28 |
Correct |
38 ms |
62976 KB |
Output is correct |
29 |
Correct |
57 ms |
62944 KB |
Output is correct |
30 |
Correct |
39 ms |
62944 KB |
Output is correct |
31 |
Correct |
39 ms |
62968 KB |
Output is correct |
32 |
Correct |
38 ms |
62968 KB |
Output is correct |
33 |
Correct |
40 ms |
62968 KB |
Output is correct |
34 |
Correct |
49 ms |
62968 KB |
Output is correct |
35 |
Correct |
42 ms |
62968 KB |
Output is correct |
36 |
Correct |
42 ms |
62976 KB |
Output is correct |
37 |
Correct |
41 ms |
62944 KB |
Output is correct |
38 |
Correct |
47 ms |
65540 KB |
Output is correct |
39 |
Correct |
70 ms |
65528 KB |
Output is correct |
40 |
Correct |
48 ms |
65528 KB |
Output is correct |
41 |
Correct |
48 ms |
65272 KB |
Output is correct |
42 |
Correct |
51 ms |
65784 KB |
Output is correct |
43 |
Correct |
52 ms |
65656 KB |
Output is correct |
44 |
Correct |
51 ms |
65656 KB |
Output is correct |
45 |
Correct |
52 ms |
65888 KB |
Output is correct |
46 |
Correct |
53 ms |
65656 KB |
Output is correct |
47 |
Correct |
52 ms |
65528 KB |
Output is correct |
48 |
Correct |
59 ms |
65532 KB |
Output is correct |
49 |
Correct |
48 ms |
65632 KB |
Output is correct |
50 |
Correct |
52 ms |
66296 KB |
Output is correct |
51 |
Correct |
52 ms |
66168 KB |
Output is correct |
52 |
Correct |
57 ms |
66040 KB |
Output is correct |
53 |
Correct |
52 ms |
66300 KB |
Output is correct |
54 |
Correct |
51 ms |
66168 KB |
Output is correct |
55 |
Correct |
51 ms |
66040 KB |
Output is correct |
56 |
Correct |
312 ms |
121440 KB |
Output is correct |
57 |
Correct |
329 ms |
133324 KB |
Output is correct |
58 |
Correct |
297 ms |
121464 KB |
Output is correct |
59 |
Correct |
311 ms |
122020 KB |
Output is correct |
60 |
Correct |
290 ms |
113144 KB |
Output is correct |
61 |
Correct |
292 ms |
121464 KB |
Output is correct |
62 |
Correct |
308 ms |
121452 KB |
Output is correct |
63 |
Correct |
199 ms |
96120 KB |
Output is correct |
64 |
Correct |
185 ms |
96168 KB |
Output is correct |
65 |
Correct |
350 ms |
139000 KB |
Output is correct |
66 |
Correct |
274 ms |
108664 KB |
Output is correct |
67 |
Correct |
436 ms |
131576 KB |
Output is correct |
68 |
Correct |
409 ms |
131836 KB |
Output is correct |
69 |
Correct |
351 ms |
133368 KB |
Output is correct |
70 |
Correct |
442 ms |
131448 KB |
Output is correct |
71 |
Correct |
425 ms |
131064 KB |
Output is correct |
72 |
Correct |
357 ms |
133752 KB |
Output is correct |
73 |
Correct |
433 ms |
130460 KB |
Output is correct |
74 |
Correct |
330 ms |
129400 KB |
Output is correct |
75 |
Correct |
298 ms |
130068 KB |
Output is correct |
76 |
Correct |
384 ms |
137056 KB |
Output is correct |
77 |
Correct |
465 ms |
140012 KB |
Output is correct |
78 |
Correct |
425 ms |
138352 KB |
Output is correct |
79 |
Correct |
493 ms |
130296 KB |
Output is correct |
80 |
Correct |
383 ms |
136056 KB |
Output is correct |
81 |
Correct |
418 ms |
129900 KB |
Output is correct |
82 |
Correct |
371 ms |
135672 KB |
Output is correct |
83 |
Correct |
440 ms |
134904 KB |
Output is correct |
84 |
Correct |
458 ms |
129400 KB |
Output is correct |
85 |
Correct |
394 ms |
134264 KB |
Output is correct |
86 |
Correct |
400 ms |
131180 KB |
Output is correct |
87 |
Correct |
416 ms |
134904 KB |
Output is correct |
88 |
Correct |
369 ms |
136440 KB |
Output is correct |
89 |
Correct |
392 ms |
131704 KB |
Output is correct |
90 |
Correct |
453 ms |
136172 KB |
Output is correct |
91 |
Correct |
443 ms |
135280 KB |
Output is correct |
92 |
Correct |
420 ms |
130552 KB |
Output is correct |
93 |
Correct |
462 ms |
130180 KB |
Output is correct |
94 |
Correct |
414 ms |
133080 KB |
Output is correct |
95 |
Correct |
444 ms |
130040 KB |
Output is correct |
96 |
Correct |
473 ms |
134896 KB |
Output is correct |
97 |
Correct |
477 ms |
137656 KB |
Output is correct |
98 |
Correct |
504 ms |
137084 KB |
Output is correct |
99 |
Correct |
531 ms |
138960 KB |
Output is correct |
100 |
Correct |
404 ms |
133872 KB |
Output is correct |
101 |
Correct |
492 ms |
138092 KB |
Output is correct |
102 |
Correct |
487 ms |
140420 KB |
Output is correct |
103 |
Correct |
434 ms |
135292 KB |
Output is correct |