//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define pii pair<int, int>
#define pb push_back
#define st first
#define nd second
#define ar(n) array<ll,(n)>
using namespace std;
typedef long long ll;
const ll inf = 1e16 + 7;
const int M = 200005;
ar(4) a[M], b[M];
priority_queue<ar(2), vector<ar(2)>, greater<ar(2)>> pq;
ll n, m, t=1, dp[M];
vector<pii> seg[M];
int vis[M];
void setE(int idx, int i, int j) {
for (++i; i < M; i += i & -i)
seg[i].pb({j, idx});
}
void updE(int i, int j, ll v) {
for (++i; i > 0; i -= i & -i) {
while (!seg[i].empty()) {
int k = seg[i].back().nd;
if (vis[k]) {
seg[i].pop_back();
continue;
}
if (seg[i].back().st < j)
break;
pq.push({v + a[k][3], k});
vis[k] = 1;
seg[i].pop_back();
}
}
}
int main() {
scanf("%lld %lld", &n, &m);
for (int i = 1; i <= m; i++) {
for (int j = 0; j < 4; j++)
scanf("%lld", &a[i][j]);
t = max(t, a[i][0]);
}
vector<ll> cH, cV;
for (int i = 1; i <= m; i++) {
dp[i] = inf;
b[i][0] = a[i][0]+a[i][1]-1;
b[i][1] = a[i][0]-a[i][2]+n;
b[i][2] = a[i][0]+a[i][2]-1;
b[i][3] = a[i][0]-a[i][1]+n;
if (a[i][1] == 1) {
b[i][3] = n+t-1;
dp[i] = a[i][3];
pq.push({dp[i], i});
}
if (a[i][2] == n)
b[i][2] = n+t-1;
cH.pb(b[i][0]);
cH.pb(b[i][2]+1);
cV.pb(b[i][1]-1);
cV.pb(b[i][3]);
}
sort(cH.begin(), cH.end());
sort(cV.begin(), cV.end());
cH.erase(unique(cH.begin(), cH.end()), cH.end());
cV.erase(unique(cV.begin(), cV.end()), cV.end());
for (int i = 1; i <= m; i++) {
b[i][0] = lower_bound(cH.begin(), cH.end(), b[i][0]) - cH.begin() + 1;
b[i][2] = lower_bound(cH.begin(), cH.end(), b[i][2]+1) - cH.begin() + 1;
b[i][1] = lower_bound(cV.begin(), cV.end(), b[i][1]-1) - cV.begin() + 1;
b[i][3] = lower_bound(cV.begin(), cV.end(), b[i][3]) - cV.begin() + 1;
if (dp[i] == inf)
setE(i, b[i][0], b[i][3]);
}
for (int i = 0; i < M; i++)
sort(seg[i].begin(), seg[i].end());
ll ans = inf;
while (!pq.empty()) {
auto it = pq.top(); pq.pop();
dp[it[1]] = it[0];
if (a[it[1]][2] == n) {
ans = min(ans, it[0]);
continue;
}
updE(b[it[1]][2], b[it[1]][1], it[0]);
}
ans = ans == inf ? -1 : ans;
printf("%lld\n", ans);
return 0;
}
Compilation message
treatment.cpp: In function 'int main()':
treatment.cpp:41:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld %lld", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~~~~~
treatment.cpp:44:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld", &a[i][j]);
~~~~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
301 ms |
25296 KB |
Output is correct |
2 |
Correct |
276 ms |
25304 KB |
Output is correct |
3 |
Correct |
232 ms |
25072 KB |
Output is correct |
4 |
Correct |
238 ms |
24600 KB |
Output is correct |
5 |
Correct |
264 ms |
32344 KB |
Output is correct |
6 |
Correct |
245 ms |
28628 KB |
Output is correct |
7 |
Correct |
211 ms |
27856 KB |
Output is correct |
8 |
Correct |
234 ms |
28244 KB |
Output is correct |
9 |
Correct |
214 ms |
26704 KB |
Output is correct |
10 |
Correct |
197 ms |
26584 KB |
Output is correct |
11 |
Correct |
327 ms |
30460 KB |
Output is correct |
12 |
Correct |
333 ms |
30472 KB |
Output is correct |
13 |
Correct |
361 ms |
31184 KB |
Output is correct |
14 |
Correct |
385 ms |
31376 KB |
Output is correct |
15 |
Correct |
337 ms |
28500 KB |
Output is correct |
16 |
Correct |
337 ms |
27736 KB |
Output is correct |
17 |
Correct |
326 ms |
28760 KB |
Output is correct |
18 |
Correct |
315 ms |
30372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4992 KB |
Output is correct |
2 |
Correct |
7 ms |
4992 KB |
Output is correct |
3 |
Correct |
7 ms |
4992 KB |
Output is correct |
4 |
Correct |
8 ms |
4992 KB |
Output is correct |
5 |
Correct |
7 ms |
4992 KB |
Output is correct |
6 |
Correct |
7 ms |
4992 KB |
Output is correct |
7 |
Correct |
7 ms |
4992 KB |
Output is correct |
8 |
Correct |
7 ms |
4992 KB |
Output is correct |
9 |
Correct |
7 ms |
4992 KB |
Output is correct |
10 |
Correct |
7 ms |
4992 KB |
Output is correct |
11 |
Correct |
7 ms |
4992 KB |
Output is correct |
12 |
Correct |
8 ms |
4992 KB |
Output is correct |
13 |
Correct |
7 ms |
5120 KB |
Output is correct |
14 |
Correct |
7 ms |
4992 KB |
Output is correct |
15 |
Correct |
7 ms |
4992 KB |
Output is correct |
16 |
Correct |
8 ms |
4992 KB |
Output is correct |
17 |
Correct |
7 ms |
4992 KB |
Output is correct |
18 |
Correct |
9 ms |
4992 KB |
Output is correct |
19 |
Correct |
7 ms |
4992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4992 KB |
Output is correct |
2 |
Correct |
7 ms |
4992 KB |
Output is correct |
3 |
Correct |
7 ms |
4992 KB |
Output is correct |
4 |
Correct |
8 ms |
4992 KB |
Output is correct |
5 |
Correct |
7 ms |
4992 KB |
Output is correct |
6 |
Correct |
7 ms |
4992 KB |
Output is correct |
7 |
Correct |
7 ms |
4992 KB |
Output is correct |
8 |
Correct |
7 ms |
4992 KB |
Output is correct |
9 |
Correct |
7 ms |
4992 KB |
Output is correct |
10 |
Correct |
7 ms |
4992 KB |
Output is correct |
11 |
Correct |
7 ms |
4992 KB |
Output is correct |
12 |
Correct |
8 ms |
4992 KB |
Output is correct |
13 |
Correct |
7 ms |
5120 KB |
Output is correct |
14 |
Correct |
7 ms |
4992 KB |
Output is correct |
15 |
Correct |
7 ms |
4992 KB |
Output is correct |
16 |
Correct |
8 ms |
4992 KB |
Output is correct |
17 |
Correct |
7 ms |
4992 KB |
Output is correct |
18 |
Correct |
9 ms |
4992 KB |
Output is correct |
19 |
Correct |
7 ms |
4992 KB |
Output is correct |
20 |
Correct |
19 ms |
6144 KB |
Output is correct |
21 |
Correct |
17 ms |
6144 KB |
Output is correct |
22 |
Correct |
19 ms |
6348 KB |
Output is correct |
23 |
Correct |
23 ms |
6400 KB |
Output is correct |
24 |
Correct |
20 ms |
6400 KB |
Output is correct |
25 |
Correct |
21 ms |
6400 KB |
Output is correct |
26 |
Correct |
19 ms |
6400 KB |
Output is correct |
27 |
Correct |
17 ms |
6400 KB |
Output is correct |
28 |
Correct |
19 ms |
6272 KB |
Output is correct |
29 |
Correct |
19 ms |
6272 KB |
Output is correct |
30 |
Correct |
17 ms |
6272 KB |
Output is correct |
31 |
Correct |
16 ms |
6272 KB |
Output is correct |
32 |
Correct |
20 ms |
6528 KB |
Output is correct |
33 |
Correct |
21 ms |
6528 KB |
Output is correct |
34 |
Correct |
20 ms |
6400 KB |
Output is correct |
35 |
Correct |
20 ms |
6528 KB |
Output is correct |
36 |
Correct |
20 ms |
6476 KB |
Output is correct |
37 |
Correct |
20 ms |
6400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
301 ms |
25296 KB |
Output is correct |
2 |
Correct |
276 ms |
25304 KB |
Output is correct |
3 |
Correct |
232 ms |
25072 KB |
Output is correct |
4 |
Correct |
238 ms |
24600 KB |
Output is correct |
5 |
Correct |
264 ms |
32344 KB |
Output is correct |
6 |
Correct |
245 ms |
28628 KB |
Output is correct |
7 |
Correct |
211 ms |
27856 KB |
Output is correct |
8 |
Correct |
234 ms |
28244 KB |
Output is correct |
9 |
Correct |
214 ms |
26704 KB |
Output is correct |
10 |
Correct |
197 ms |
26584 KB |
Output is correct |
11 |
Correct |
327 ms |
30460 KB |
Output is correct |
12 |
Correct |
333 ms |
30472 KB |
Output is correct |
13 |
Correct |
361 ms |
31184 KB |
Output is correct |
14 |
Correct |
385 ms |
31376 KB |
Output is correct |
15 |
Correct |
337 ms |
28500 KB |
Output is correct |
16 |
Correct |
337 ms |
27736 KB |
Output is correct |
17 |
Correct |
326 ms |
28760 KB |
Output is correct |
18 |
Correct |
315 ms |
30372 KB |
Output is correct |
19 |
Correct |
7 ms |
4992 KB |
Output is correct |
20 |
Correct |
7 ms |
4992 KB |
Output is correct |
21 |
Correct |
7 ms |
4992 KB |
Output is correct |
22 |
Correct |
8 ms |
4992 KB |
Output is correct |
23 |
Correct |
7 ms |
4992 KB |
Output is correct |
24 |
Correct |
7 ms |
4992 KB |
Output is correct |
25 |
Correct |
7 ms |
4992 KB |
Output is correct |
26 |
Correct |
7 ms |
4992 KB |
Output is correct |
27 |
Correct |
7 ms |
4992 KB |
Output is correct |
28 |
Correct |
7 ms |
4992 KB |
Output is correct |
29 |
Correct |
7 ms |
4992 KB |
Output is correct |
30 |
Correct |
8 ms |
4992 KB |
Output is correct |
31 |
Correct |
7 ms |
5120 KB |
Output is correct |
32 |
Correct |
7 ms |
4992 KB |
Output is correct |
33 |
Correct |
7 ms |
4992 KB |
Output is correct |
34 |
Correct |
8 ms |
4992 KB |
Output is correct |
35 |
Correct |
7 ms |
4992 KB |
Output is correct |
36 |
Correct |
9 ms |
4992 KB |
Output is correct |
37 |
Correct |
7 ms |
4992 KB |
Output is correct |
38 |
Correct |
19 ms |
6144 KB |
Output is correct |
39 |
Correct |
17 ms |
6144 KB |
Output is correct |
40 |
Correct |
19 ms |
6348 KB |
Output is correct |
41 |
Correct |
23 ms |
6400 KB |
Output is correct |
42 |
Correct |
20 ms |
6400 KB |
Output is correct |
43 |
Correct |
21 ms |
6400 KB |
Output is correct |
44 |
Correct |
19 ms |
6400 KB |
Output is correct |
45 |
Correct |
17 ms |
6400 KB |
Output is correct |
46 |
Correct |
19 ms |
6272 KB |
Output is correct |
47 |
Correct |
19 ms |
6272 KB |
Output is correct |
48 |
Correct |
17 ms |
6272 KB |
Output is correct |
49 |
Correct |
16 ms |
6272 KB |
Output is correct |
50 |
Correct |
20 ms |
6528 KB |
Output is correct |
51 |
Correct |
21 ms |
6528 KB |
Output is correct |
52 |
Correct |
20 ms |
6400 KB |
Output is correct |
53 |
Correct |
20 ms |
6528 KB |
Output is correct |
54 |
Correct |
20 ms |
6476 KB |
Output is correct |
55 |
Correct |
20 ms |
6400 KB |
Output is correct |
56 |
Correct |
257 ms |
24152 KB |
Output is correct |
57 |
Correct |
263 ms |
24376 KB |
Output is correct |
58 |
Correct |
301 ms |
26328 KB |
Output is correct |
59 |
Correct |
306 ms |
26320 KB |
Output is correct |
60 |
Correct |
323 ms |
28116 KB |
Output is correct |
61 |
Correct |
299 ms |
26448 KB |
Output is correct |
62 |
Correct |
256 ms |
24360 KB |
Output is correct |
63 |
Correct |
304 ms |
25332 KB |
Output is correct |
64 |
Correct |
298 ms |
25552 KB |
Output is correct |
65 |
Correct |
323 ms |
27608 KB |
Output is correct |
66 |
Correct |
319 ms |
28116 KB |
Output is correct |
67 |
Correct |
320 ms |
27740 KB |
Output is correct |
68 |
Correct |
273 ms |
27560 KB |
Output is correct |
69 |
Correct |
239 ms |
27928 KB |
Output is correct |
70 |
Correct |
322 ms |
27864 KB |
Output is correct |
71 |
Correct |
279 ms |
27592 KB |
Output is correct |
72 |
Correct |
236 ms |
27544 KB |
Output is correct |
73 |
Correct |
330 ms |
27476 KB |
Output is correct |
74 |
Correct |
249 ms |
27096 KB |
Output is correct |
75 |
Correct |
200 ms |
26456 KB |
Output is correct |
76 |
Correct |
343 ms |
30424 KB |
Output is correct |
77 |
Correct |
355 ms |
30496 KB |
Output is correct |
78 |
Correct |
359 ms |
31188 KB |
Output is correct |
79 |
Correct |
372 ms |
28880 KB |
Output is correct |
80 |
Correct |
334 ms |
28500 KB |
Output is correct |
81 |
Correct |
314 ms |
28456 KB |
Output is correct |
82 |
Correct |
339 ms |
28496 KB |
Output is correct |
83 |
Correct |
355 ms |
28504 KB |
Output is correct |
84 |
Correct |
363 ms |
28444 KB |
Output is correct |
85 |
Correct |
331 ms |
28500 KB |
Output is correct |
86 |
Correct |
334 ms |
27864 KB |
Output is correct |
87 |
Correct |
330 ms |
28240 KB |
Output is correct |
88 |
Correct |
337 ms |
27352 KB |
Output is correct |
89 |
Correct |
370 ms |
28248 KB |
Output is correct |
90 |
Correct |
365 ms |
31832 KB |
Output is correct |
91 |
Correct |
338 ms |
28632 KB |
Output is correct |
92 |
Correct |
344 ms |
28408 KB |
Output is correct |
93 |
Correct |
365 ms |
28240 KB |
Output is correct |
94 |
Correct |
341 ms |
27344 KB |
Output is correct |
95 |
Correct |
344 ms |
28248 KB |
Output is correct |
96 |
Correct |
364 ms |
30932 KB |
Output is correct |
97 |
Correct |
369 ms |
30800 KB |
Output is correct |
98 |
Correct |
379 ms |
30800 KB |
Output is correct |
99 |
Correct |
367 ms |
30676 KB |
Output is correct |
100 |
Correct |
301 ms |
28096 KB |
Output is correct |
101 |
Correct |
384 ms |
30800 KB |
Output is correct |
102 |
Correct |
348 ms |
30036 KB |
Output is correct |
103 |
Correct |
356 ms |
28248 KB |
Output is correct |