#include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
#include <set>
#include <queue>
#include <cassert>
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>;
#define sz(x) int(x.size())
struct project
{
int T;
int L;
int R;
int C;
int I;
};
vector<project> P;
const int maxM = 200'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];
vector<int> v[1 << 20];
segtree()
{
;
}
segtree(int L, int R)
{
;
}
void add(int i, int l, int r, int I, ll Pr)
{
v[i].push_back(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, int 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].back()].L + P[v[i].back()].T > mxv) break;
if(!visited[v[i].back()])
{
query_ans.push_back(v[i].back());
visited[v[i].back()] = 1;
}
v[i].pop_back();
}
}
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, int 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].back()].L - P[v[i].back()].T > mxv) break;
if(!visited[v[i].back()])
{
query_ans.push_back(v[i].back());
visited[v[i].back()] = 1;
}
v[i].pop_back();
}
}
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;
set<pos> tbv;
int ct = 0;
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;
// cerr << "A\n";
// int ct = 0;
// cerr << "size of P = " << sz(P) << '\n';
sort(P.begin(), P.end(), [] (project x, project y)
{
// cerr << "callsort\n";
// if((++ct) % 10000 == 0) cerr << "sorting: " << ct << '\n';
return x.L + x.T < y.L + y.T;
});
// cerr << "pre add\n";
for(int i = 0; i < M; i++)
U.add(1, 0, M-1, P[i].I, P[i].I);
// cerr << "post add\n";
sort(P.begin(), P.end(), [] (project x, project y)
{
return x.L - x.T < y.L - y.T;
});
// cerr << "B\n";
for(int i = 0; i < M; i++)
D.add(1, 0, M-1, P[i].I, P[i].I);
for(int f = 0; f < (1 << 18); f++)
{
reverse(U.v[f].begin(), U.v[f].end());
reverse(D.v[f].begin(), D.v[f].end());
}
// while(1);
// cerr << "C\n";
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;
tbv.insert(pos{i});
}
}
// cerr << "D\n";
while(!tbv.empty())
{
int u = tbv.begin()->i;
tbv.erase(tbv.begin());
// cerr << "visiting " << u << ' ' << P[u].L << ' ' << P[u].R << '\n';
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';
assert(P[v].L <= P[u].R + 1 - abs(P[u].T - P[v].T));
assert(dp[v] == INF);
dp[v] = dp[u] + P[v].C;
tbv.insert(pos{v});
}
}
// cerr << "E\n";
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 |
370 ms |
80056 KB |
Output is correct |
2 |
Correct |
299 ms |
79932 KB |
Output is correct |
3 |
Correct |
388 ms |
80984 KB |
Output is correct |
4 |
Correct |
358 ms |
84268 KB |
Output is correct |
5 |
Correct |
365 ms |
87684 KB |
Output is correct |
6 |
Correct |
295 ms |
83356 KB |
Output is correct |
7 |
Correct |
247 ms |
83024 KB |
Output is correct |
8 |
Correct |
211 ms |
83192 KB |
Output is correct |
9 |
Correct |
193 ms |
83020 KB |
Output is correct |
10 |
Correct |
187 ms |
83164 KB |
Output is correct |
11 |
Correct |
387 ms |
88052 KB |
Output is correct |
12 |
Correct |
389 ms |
87896 KB |
Output is correct |
13 |
Correct |
403 ms |
86388 KB |
Output is correct |
14 |
Correct |
415 ms |
86448 KB |
Output is correct |
15 |
Correct |
396 ms |
83156 KB |
Output is correct |
16 |
Correct |
392 ms |
83032 KB |
Output is correct |
17 |
Correct |
381 ms |
82404 KB |
Output is correct |
18 |
Correct |
351 ms |
87268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
51804 KB |
Output is correct |
2 |
Correct |
28 ms |
51816 KB |
Output is correct |
3 |
Correct |
30 ms |
51824 KB |
Output is correct |
4 |
Correct |
28 ms |
51916 KB |
Output is correct |
5 |
Correct |
28 ms |
51916 KB |
Output is correct |
6 |
Correct |
28 ms |
51924 KB |
Output is correct |
7 |
Correct |
29 ms |
51888 KB |
Output is correct |
8 |
Correct |
27 ms |
51916 KB |
Output is correct |
9 |
Correct |
27 ms |
51916 KB |
Output is correct |
10 |
Correct |
32 ms |
51892 KB |
Output is correct |
11 |
Correct |
28 ms |
51832 KB |
Output is correct |
12 |
Correct |
28 ms |
51832 KB |
Output is correct |
13 |
Correct |
34 ms |
51896 KB |
Output is correct |
14 |
Correct |
30 ms |
51924 KB |
Output is correct |
15 |
Correct |
28 ms |
51916 KB |
Output is correct |
16 |
Correct |
28 ms |
51912 KB |
Output is correct |
17 |
Correct |
28 ms |
51928 KB |
Output is correct |
18 |
Correct |
27 ms |
51872 KB |
Output is correct |
19 |
Correct |
31 ms |
51920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
51804 KB |
Output is correct |
2 |
Correct |
28 ms |
51816 KB |
Output is correct |
3 |
Correct |
30 ms |
51824 KB |
Output is correct |
4 |
Correct |
28 ms |
51916 KB |
Output is correct |
5 |
Correct |
28 ms |
51916 KB |
Output is correct |
6 |
Correct |
28 ms |
51924 KB |
Output is correct |
7 |
Correct |
29 ms |
51888 KB |
Output is correct |
8 |
Correct |
27 ms |
51916 KB |
Output is correct |
9 |
Correct |
27 ms |
51916 KB |
Output is correct |
10 |
Correct |
32 ms |
51892 KB |
Output is correct |
11 |
Correct |
28 ms |
51832 KB |
Output is correct |
12 |
Correct |
28 ms |
51832 KB |
Output is correct |
13 |
Correct |
34 ms |
51896 KB |
Output is correct |
14 |
Correct |
30 ms |
51924 KB |
Output is correct |
15 |
Correct |
28 ms |
51916 KB |
Output is correct |
16 |
Correct |
28 ms |
51912 KB |
Output is correct |
17 |
Correct |
28 ms |
51928 KB |
Output is correct |
18 |
Correct |
27 ms |
51872 KB |
Output is correct |
19 |
Correct |
31 ms |
51920 KB |
Output is correct |
20 |
Correct |
35 ms |
53316 KB |
Output is correct |
21 |
Correct |
36 ms |
53452 KB |
Output is correct |
22 |
Correct |
37 ms |
53228 KB |
Output is correct |
23 |
Correct |
35 ms |
53232 KB |
Output is correct |
24 |
Correct |
39 ms |
53452 KB |
Output is correct |
25 |
Correct |
38 ms |
53208 KB |
Output is correct |
26 |
Correct |
37 ms |
53260 KB |
Output is correct |
27 |
Correct |
36 ms |
53200 KB |
Output is correct |
28 |
Correct |
43 ms |
53528 KB |
Output is correct |
29 |
Correct |
38 ms |
53216 KB |
Output is correct |
30 |
Correct |
34 ms |
53224 KB |
Output is correct |
31 |
Correct |
35 ms |
53328 KB |
Output is correct |
32 |
Correct |
38 ms |
53532 KB |
Output is correct |
33 |
Correct |
39 ms |
53456 KB |
Output is correct |
34 |
Correct |
40 ms |
53328 KB |
Output is correct |
35 |
Correct |
52 ms |
53548 KB |
Output is correct |
36 |
Correct |
44 ms |
53584 KB |
Output is correct |
37 |
Correct |
45 ms |
53256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
370 ms |
80056 KB |
Output is correct |
2 |
Correct |
299 ms |
79932 KB |
Output is correct |
3 |
Correct |
388 ms |
80984 KB |
Output is correct |
4 |
Correct |
358 ms |
84268 KB |
Output is correct |
5 |
Correct |
365 ms |
87684 KB |
Output is correct |
6 |
Correct |
295 ms |
83356 KB |
Output is correct |
7 |
Correct |
247 ms |
83024 KB |
Output is correct |
8 |
Correct |
211 ms |
83192 KB |
Output is correct |
9 |
Correct |
193 ms |
83020 KB |
Output is correct |
10 |
Correct |
187 ms |
83164 KB |
Output is correct |
11 |
Correct |
387 ms |
88052 KB |
Output is correct |
12 |
Correct |
389 ms |
87896 KB |
Output is correct |
13 |
Correct |
403 ms |
86388 KB |
Output is correct |
14 |
Correct |
415 ms |
86448 KB |
Output is correct |
15 |
Correct |
396 ms |
83156 KB |
Output is correct |
16 |
Correct |
392 ms |
83032 KB |
Output is correct |
17 |
Correct |
381 ms |
82404 KB |
Output is correct |
18 |
Correct |
351 ms |
87268 KB |
Output is correct |
19 |
Correct |
28 ms |
51804 KB |
Output is correct |
20 |
Correct |
28 ms |
51816 KB |
Output is correct |
21 |
Correct |
30 ms |
51824 KB |
Output is correct |
22 |
Correct |
28 ms |
51916 KB |
Output is correct |
23 |
Correct |
28 ms |
51916 KB |
Output is correct |
24 |
Correct |
28 ms |
51924 KB |
Output is correct |
25 |
Correct |
29 ms |
51888 KB |
Output is correct |
26 |
Correct |
27 ms |
51916 KB |
Output is correct |
27 |
Correct |
27 ms |
51916 KB |
Output is correct |
28 |
Correct |
32 ms |
51892 KB |
Output is correct |
29 |
Correct |
28 ms |
51832 KB |
Output is correct |
30 |
Correct |
28 ms |
51832 KB |
Output is correct |
31 |
Correct |
34 ms |
51896 KB |
Output is correct |
32 |
Correct |
30 ms |
51924 KB |
Output is correct |
33 |
Correct |
28 ms |
51916 KB |
Output is correct |
34 |
Correct |
28 ms |
51912 KB |
Output is correct |
35 |
Correct |
28 ms |
51928 KB |
Output is correct |
36 |
Correct |
27 ms |
51872 KB |
Output is correct |
37 |
Correct |
31 ms |
51920 KB |
Output is correct |
38 |
Correct |
35 ms |
53316 KB |
Output is correct |
39 |
Correct |
36 ms |
53452 KB |
Output is correct |
40 |
Correct |
37 ms |
53228 KB |
Output is correct |
41 |
Correct |
35 ms |
53232 KB |
Output is correct |
42 |
Correct |
39 ms |
53452 KB |
Output is correct |
43 |
Correct |
38 ms |
53208 KB |
Output is correct |
44 |
Correct |
37 ms |
53260 KB |
Output is correct |
45 |
Correct |
36 ms |
53200 KB |
Output is correct |
46 |
Correct |
43 ms |
53528 KB |
Output is correct |
47 |
Correct |
38 ms |
53216 KB |
Output is correct |
48 |
Correct |
34 ms |
53224 KB |
Output is correct |
49 |
Correct |
35 ms |
53328 KB |
Output is correct |
50 |
Correct |
38 ms |
53532 KB |
Output is correct |
51 |
Correct |
39 ms |
53456 KB |
Output is correct |
52 |
Correct |
40 ms |
53328 KB |
Output is correct |
53 |
Correct |
52 ms |
53548 KB |
Output is correct |
54 |
Correct |
44 ms |
53584 KB |
Output is correct |
55 |
Correct |
45 ms |
53256 KB |
Output is correct |
56 |
Correct |
231 ms |
83660 KB |
Output is correct |
57 |
Correct |
249 ms |
85300 KB |
Output is correct |
58 |
Correct |
213 ms |
82204 KB |
Output is correct |
59 |
Correct |
223 ms |
82596 KB |
Output is correct |
60 |
Correct |
244 ms |
83284 KB |
Output is correct |
61 |
Correct |
217 ms |
82176 KB |
Output is correct |
62 |
Correct |
261 ms |
83668 KB |
Output is correct |
63 |
Correct |
182 ms |
83020 KB |
Output is correct |
64 |
Correct |
182 ms |
83004 KB |
Output is correct |
65 |
Correct |
370 ms |
83276 KB |
Output is correct |
66 |
Correct |
238 ms |
83316 KB |
Output is correct |
67 |
Correct |
375 ms |
83772 KB |
Output is correct |
68 |
Correct |
364 ms |
83664 KB |
Output is correct |
69 |
Correct |
308 ms |
83672 KB |
Output is correct |
70 |
Correct |
382 ms |
83996 KB |
Output is correct |
71 |
Correct |
360 ms |
83912 KB |
Output is correct |
72 |
Correct |
310 ms |
83672 KB |
Output is correct |
73 |
Correct |
398 ms |
83884 KB |
Output is correct |
74 |
Correct |
217 ms |
83672 KB |
Output is correct |
75 |
Correct |
204 ms |
83792 KB |
Output is correct |
76 |
Correct |
380 ms |
88384 KB |
Output is correct |
77 |
Correct |
380 ms |
88488 KB |
Output is correct |
78 |
Correct |
422 ms |
86856 KB |
Output is correct |
79 |
Correct |
387 ms |
83808 KB |
Output is correct |
80 |
Correct |
389 ms |
83544 KB |
Output is correct |
81 |
Correct |
251 ms |
83744 KB |
Output is correct |
82 |
Correct |
377 ms |
82948 KB |
Output is correct |
83 |
Correct |
384 ms |
83132 KB |
Output is correct |
84 |
Correct |
380 ms |
82896 KB |
Output is correct |
85 |
Correct |
291 ms |
83624 KB |
Output is correct |
86 |
Correct |
293 ms |
83644 KB |
Output is correct |
87 |
Correct |
311 ms |
83800 KB |
Output is correct |
88 |
Correct |
280 ms |
83664 KB |
Output is correct |
89 |
Correct |
298 ms |
83536 KB |
Output is correct |
90 |
Correct |
424 ms |
88556 KB |
Output is correct |
91 |
Correct |
346 ms |
86212 KB |
Output is correct |
92 |
Correct |
338 ms |
83804 KB |
Output is correct |
93 |
Correct |
377 ms |
83668 KB |
Output is correct |
94 |
Correct |
340 ms |
83920 KB |
Output is correct |
95 |
Correct |
366 ms |
83644 KB |
Output is correct |
96 |
Correct |
390 ms |
88276 KB |
Output is correct |
97 |
Correct |
380 ms |
88516 KB |
Output is correct |
98 |
Correct |
378 ms |
88296 KB |
Output is correct |
99 |
Correct |
369 ms |
88512 KB |
Output is correct |
100 |
Correct |
341 ms |
88448 KB |
Output is correct |
101 |
Correct |
372 ms |
88404 KB |
Output is correct |
102 |
Correct |
372 ms |
88776 KB |
Output is correct |
103 |
Correct |
316 ms |
84436 KB |
Output is correct |