#include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
#include <set>
#include <queue>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vi = vector<int>;
using vll = vector<ll>;
using pii = pair<int, int>;
struct project
{
int T;
int L;
int R;
int C;
int I;
};
vector<project> P;
const int maxM = 100'000;
vi query_ans;
vi visited(maxM, 0);
const ll INF = 1'000'000'000'000'000'000LL;
vll dp(maxM, INF);
struct pos
{
int i;
};
bool operator < (pos A, pos B)
{
if(dp[A.i] == dp[B.i]) return A.i < B.i;
else return dp[A.i] < dp[B.i];
}
struct segtree
{
// deque<int> v[220'000];
queue<int> v[1 << 18];
segtree()
{
;
}
segtree(int L, int R)
{
;
}
void add(int i, int l, int r, int I, ll Pr)
{
v[i].push(Pr);
if(l != r)
{
if(I <= (l+r)/2) add(2*i, l, (l+r)/2, I, Pr);
else add(2*i + 1, (l+r)/2+1, r, I, Pr);
}
}
void getU(int i, int l, int r, int L, int R, ll mxv)
{
if(R < l || r < L || R < L) return;
else if(L <= l && r <= R)
{
while(1)
{
if(v[i].empty()) break;
if(P[v[i].front()].L + P[v[i].front()].T > mxv) break;
if(!visited[v[i].front()])
{
query_ans.push_back(v[i].front());
visited[v[i].front()] = 1;
}
v[i].pop();
}
}
else
{
getU(2*i, l, (l+r)/2, L, R, mxv);
getU(2*i+1, (l+r)/2+1, r, L, R, mxv);
}
}
void getD(int i, int l, int r, int L, int R, ll mxv)
{
if(R < l || r < L || R < L) return;
else if(L <= l && r <= R)
{
while(1)
{
if(v[i].empty()) break;
if(P[v[i].front()].L - P[v[i].front()].T > mxv) break;
if(!visited[v[i].front()])
{
query_ans.push_back(v[i].front());
visited[v[i].front()] = 1;
}
v[i].pop();
}
}
else
{
getD(2*i, l, (l+r)/2, L, R, mxv);
getD(2*i+1, (l+r)/2+1, r, L, R, mxv);
}
}
};
segtree U, D;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N, M;
cin >> N >> M;
P = vector<project>(M);
for(int i = 0; i < M; i++) cin >> P[i].T >> P[i].L >> P[i].R >> P[i].C;
sort(P.begin(), P.end(), [] (project x, project y)
{
return x.T < y.T;
});
for(int i = 0; i < M; i++) P[i].I = i;
sort(P.begin(), P.end(), [] (project x, project y)
{
return x.L + x.T < y.L + y.T;
});
for(int i = 0; i < M; i++)
U.add(1, 0, M-1, P[i].I, P[i].I);
sort(P.begin(), P.end(), [] (project x, project y)
{
return x.L - x.T < y.L - y.T;
});
for(int i = 0; i < M; i++)
D.add(1, 0, M-1, P[i].I, P[i].I);
sort(P.begin(), P.end(), [] (project x, project y)
{
return x.I < y.I;
});
for(int i = 0; i < M; i++)
{
if(P[i].L == 1)
{
dp[i] = P[i].C;
visited[i] = 1;
}
}
set<pos> tbv;
for(int i = 0; i < M; i++) tbv.insert(pos{i});
while(!tbv.empty())
{
int u = tbv.begin()->i;
tbv.erase(tbv.begin());
// cerr << "visiting " << u << ' ' << P[u].L << ' ' << P[u].R << '\n';
if(!visited[u]) break;
query_ans.clear();
U.getU(1, 0, M-1, u+1, M-1, P[u].R + P[u].T + 1);
D.getD(1, 0, M-1, 0, u-1, P[u].R - P[u].T + 1);
for(int v: query_ans)
{
// cerr << u << " -> " << v << '\n';
if(P[v].L <= P[u].R + 1 - abs(P[u].T - P[v].T))
{
if(dp[v] > dp[u] + P[v].C)
{
tbv.erase(pos{v});
dp[v] = dp[u] + P[v].C;
tbv.insert(pos{v});
}
}
}
}
ll final_ans = INF;
for(int i = 0; i < M; i++)
if(P[i].R == N)
final_ans = min(final_ans, dp[i]);
if(final_ans >= INF) final_ans = -1;
cout << final_ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
746 ms |
372292 KB |
Output is correct |
2 |
Correct |
616 ms |
372200 KB |
Output is correct |
3 |
Correct |
680 ms |
371372 KB |
Output is correct |
4 |
Correct |
678 ms |
371416 KB |
Output is correct |
5 |
Correct |
656 ms |
372076 KB |
Output is correct |
6 |
Correct |
621 ms |
372152 KB |
Output is correct |
7 |
Correct |
520 ms |
371964 KB |
Output is correct |
8 |
Correct |
468 ms |
372032 KB |
Output is correct |
9 |
Correct |
464 ms |
372084 KB |
Output is correct |
10 |
Correct |
430 ms |
372068 KB |
Output is correct |
11 |
Correct |
678 ms |
372752 KB |
Output is correct |
12 |
Correct |
658 ms |
372544 KB |
Output is correct |
13 |
Correct |
758 ms |
372040 KB |
Output is correct |
14 |
Correct |
753 ms |
372128 KB |
Output is correct |
15 |
Correct |
755 ms |
372088 KB |
Output is correct |
16 |
Correct |
751 ms |
372128 KB |
Output is correct |
17 |
Correct |
772 ms |
371224 KB |
Output is correct |
18 |
Correct |
619 ms |
372008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
235 ms |
354468 KB |
Output is correct |
2 |
Correct |
214 ms |
354472 KB |
Output is correct |
3 |
Correct |
210 ms |
354420 KB |
Output is correct |
4 |
Correct |
217 ms |
354428 KB |
Output is correct |
5 |
Correct |
216 ms |
354300 KB |
Output is correct |
6 |
Correct |
209 ms |
354316 KB |
Output is correct |
7 |
Correct |
217 ms |
354420 KB |
Output is correct |
8 |
Correct |
213 ms |
354384 KB |
Output is correct |
9 |
Correct |
217 ms |
354428 KB |
Output is correct |
10 |
Correct |
221 ms |
354596 KB |
Output is correct |
11 |
Correct |
211 ms |
354332 KB |
Output is correct |
12 |
Correct |
222 ms |
354328 KB |
Output is correct |
13 |
Correct |
218 ms |
354388 KB |
Output is correct |
14 |
Correct |
225 ms |
354524 KB |
Output is correct |
15 |
Correct |
227 ms |
354368 KB |
Output is correct |
16 |
Correct |
236 ms |
354344 KB |
Output is correct |
17 |
Correct |
209 ms |
354372 KB |
Output is correct |
18 |
Correct |
214 ms |
354368 KB |
Output is correct |
19 |
Correct |
212 ms |
354424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
235 ms |
354468 KB |
Output is correct |
2 |
Correct |
214 ms |
354472 KB |
Output is correct |
3 |
Correct |
210 ms |
354420 KB |
Output is correct |
4 |
Correct |
217 ms |
354428 KB |
Output is correct |
5 |
Correct |
216 ms |
354300 KB |
Output is correct |
6 |
Correct |
209 ms |
354316 KB |
Output is correct |
7 |
Correct |
217 ms |
354420 KB |
Output is correct |
8 |
Correct |
213 ms |
354384 KB |
Output is correct |
9 |
Correct |
217 ms |
354428 KB |
Output is correct |
10 |
Correct |
221 ms |
354596 KB |
Output is correct |
11 |
Correct |
211 ms |
354332 KB |
Output is correct |
12 |
Correct |
222 ms |
354328 KB |
Output is correct |
13 |
Correct |
218 ms |
354388 KB |
Output is correct |
14 |
Correct |
225 ms |
354524 KB |
Output is correct |
15 |
Correct |
227 ms |
354368 KB |
Output is correct |
16 |
Correct |
236 ms |
354344 KB |
Output is correct |
17 |
Correct |
209 ms |
354372 KB |
Output is correct |
18 |
Correct |
214 ms |
354368 KB |
Output is correct |
19 |
Correct |
212 ms |
354424 KB |
Output is correct |
20 |
Correct |
248 ms |
355068 KB |
Output is correct |
21 |
Correct |
266 ms |
355004 KB |
Output is correct |
22 |
Correct |
252 ms |
355108 KB |
Output is correct |
23 |
Correct |
245 ms |
355136 KB |
Output is correct |
24 |
Correct |
249 ms |
355172 KB |
Output is correct |
25 |
Correct |
250 ms |
355268 KB |
Output is correct |
26 |
Correct |
252 ms |
355176 KB |
Output is correct |
27 |
Correct |
246 ms |
355104 KB |
Output is correct |
28 |
Correct |
259 ms |
355232 KB |
Output is correct |
29 |
Correct |
270 ms |
355140 KB |
Output is correct |
30 |
Correct |
242 ms |
355184 KB |
Output is correct |
31 |
Correct |
245 ms |
355140 KB |
Output is correct |
32 |
Correct |
254 ms |
355240 KB |
Output is correct |
33 |
Correct |
246 ms |
355140 KB |
Output is correct |
34 |
Correct |
266 ms |
355024 KB |
Output is correct |
35 |
Correct |
277 ms |
355092 KB |
Output is correct |
36 |
Correct |
252 ms |
355140 KB |
Output is correct |
37 |
Correct |
263 ms |
355008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
746 ms |
372292 KB |
Output is correct |
2 |
Correct |
616 ms |
372200 KB |
Output is correct |
3 |
Correct |
680 ms |
371372 KB |
Output is correct |
4 |
Correct |
678 ms |
371416 KB |
Output is correct |
5 |
Correct |
656 ms |
372076 KB |
Output is correct |
6 |
Correct |
621 ms |
372152 KB |
Output is correct |
7 |
Correct |
520 ms |
371964 KB |
Output is correct |
8 |
Correct |
468 ms |
372032 KB |
Output is correct |
9 |
Correct |
464 ms |
372084 KB |
Output is correct |
10 |
Correct |
430 ms |
372068 KB |
Output is correct |
11 |
Correct |
678 ms |
372752 KB |
Output is correct |
12 |
Correct |
658 ms |
372544 KB |
Output is correct |
13 |
Correct |
758 ms |
372040 KB |
Output is correct |
14 |
Correct |
753 ms |
372128 KB |
Output is correct |
15 |
Correct |
755 ms |
372088 KB |
Output is correct |
16 |
Correct |
751 ms |
372128 KB |
Output is correct |
17 |
Correct |
772 ms |
371224 KB |
Output is correct |
18 |
Correct |
619 ms |
372008 KB |
Output is correct |
19 |
Correct |
235 ms |
354468 KB |
Output is correct |
20 |
Correct |
214 ms |
354472 KB |
Output is correct |
21 |
Correct |
210 ms |
354420 KB |
Output is correct |
22 |
Correct |
217 ms |
354428 KB |
Output is correct |
23 |
Correct |
216 ms |
354300 KB |
Output is correct |
24 |
Correct |
209 ms |
354316 KB |
Output is correct |
25 |
Correct |
217 ms |
354420 KB |
Output is correct |
26 |
Correct |
213 ms |
354384 KB |
Output is correct |
27 |
Correct |
217 ms |
354428 KB |
Output is correct |
28 |
Correct |
221 ms |
354596 KB |
Output is correct |
29 |
Correct |
211 ms |
354332 KB |
Output is correct |
30 |
Correct |
222 ms |
354328 KB |
Output is correct |
31 |
Correct |
218 ms |
354388 KB |
Output is correct |
32 |
Correct |
225 ms |
354524 KB |
Output is correct |
33 |
Correct |
227 ms |
354368 KB |
Output is correct |
34 |
Correct |
236 ms |
354344 KB |
Output is correct |
35 |
Correct |
209 ms |
354372 KB |
Output is correct |
36 |
Correct |
214 ms |
354368 KB |
Output is correct |
37 |
Correct |
212 ms |
354424 KB |
Output is correct |
38 |
Correct |
248 ms |
355068 KB |
Output is correct |
39 |
Correct |
266 ms |
355004 KB |
Output is correct |
40 |
Correct |
252 ms |
355108 KB |
Output is correct |
41 |
Correct |
245 ms |
355136 KB |
Output is correct |
42 |
Correct |
249 ms |
355172 KB |
Output is correct |
43 |
Correct |
250 ms |
355268 KB |
Output is correct |
44 |
Correct |
252 ms |
355176 KB |
Output is correct |
45 |
Correct |
246 ms |
355104 KB |
Output is correct |
46 |
Correct |
259 ms |
355232 KB |
Output is correct |
47 |
Correct |
270 ms |
355140 KB |
Output is correct |
48 |
Correct |
242 ms |
355184 KB |
Output is correct |
49 |
Correct |
245 ms |
355140 KB |
Output is correct |
50 |
Correct |
254 ms |
355240 KB |
Output is correct |
51 |
Correct |
246 ms |
355140 KB |
Output is correct |
52 |
Correct |
266 ms |
355024 KB |
Output is correct |
53 |
Correct |
277 ms |
355092 KB |
Output is correct |
54 |
Correct |
252 ms |
355140 KB |
Output is correct |
55 |
Correct |
263 ms |
355008 KB |
Output is correct |
56 |
Correct |
498 ms |
371744 KB |
Output is correct |
57 |
Correct |
540 ms |
372016 KB |
Output is correct |
58 |
Correct |
488 ms |
371268 KB |
Output is correct |
59 |
Correct |
505 ms |
371600 KB |
Output is correct |
60 |
Correct |
525 ms |
372376 KB |
Output is correct |
61 |
Correct |
511 ms |
371388 KB |
Output is correct |
62 |
Correct |
510 ms |
371736 KB |
Output is correct |
63 |
Correct |
434 ms |
372276 KB |
Output is correct |
64 |
Correct |
463 ms |
372268 KB |
Output is correct |
65 |
Correct |
765 ms |
372340 KB |
Output is correct |
66 |
Correct |
508 ms |
372420 KB |
Output is correct |
67 |
Correct |
749 ms |
372920 KB |
Output is correct |
68 |
Correct |
731 ms |
372848 KB |
Output is correct |
69 |
Correct |
646 ms |
372944 KB |
Output is correct |
70 |
Correct |
800 ms |
372852 KB |
Output is correct |
71 |
Correct |
733 ms |
372880 KB |
Output is correct |
72 |
Correct |
622 ms |
372932 KB |
Output is correct |
73 |
Correct |
807 ms |
372844 KB |
Output is correct |
74 |
Correct |
479 ms |
372756 KB |
Output is correct |
75 |
Correct |
457 ms |
372760 KB |
Output is correct |
76 |
Correct |
701 ms |
373140 KB |
Output is correct |
77 |
Correct |
696 ms |
373044 KB |
Output is correct |
78 |
Correct |
770 ms |
372452 KB |
Output is correct |
79 |
Correct |
764 ms |
372856 KB |
Output is correct |
80 |
Correct |
812 ms |
372412 KB |
Output is correct |
81 |
Correct |
510 ms |
372760 KB |
Output is correct |
82 |
Correct |
738 ms |
371700 KB |
Output is correct |
83 |
Correct |
786 ms |
371904 KB |
Output is correct |
84 |
Correct |
761 ms |
372036 KB |
Output is correct |
85 |
Correct |
669 ms |
372808 KB |
Output is correct |
86 |
Correct |
606 ms |
372808 KB |
Output is correct |
87 |
Correct |
638 ms |
372936 KB |
Output is correct |
88 |
Correct |
568 ms |
372860 KB |
Output is correct |
89 |
Correct |
687 ms |
372820 KB |
Output is correct |
90 |
Correct |
710 ms |
373156 KB |
Output is correct |
91 |
Correct |
714 ms |
373184 KB |
Output is correct |
92 |
Correct |
665 ms |
372736 KB |
Output is correct |
93 |
Correct |
734 ms |
372856 KB |
Output is correct |
94 |
Correct |
682 ms |
372932 KB |
Output is correct |
95 |
Correct |
721 ms |
372812 KB |
Output is correct |
96 |
Correct |
777 ms |
373208 KB |
Output is correct |
97 |
Correct |
730 ms |
372856 KB |
Output is correct |
98 |
Correct |
696 ms |
372892 KB |
Output is correct |
99 |
Correct |
704 ms |
373028 KB |
Output is correct |
100 |
Correct |
671 ms |
373140 KB |
Output is correct |
101 |
Correct |
699 ms |
373028 KB |
Output is correct |
102 |
Correct |
674 ms |
373524 KB |
Output is correct |
103 |
Correct |
601 ms |
372932 KB |
Output is correct |