#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, int> pli;
const int MAXN = 1e5;
const int inf = 1e6;
int t, n;
pii points[2*MAXN];
int cost[MAXN];
vector<int> snodes;
vector<int> enodes;
int xsort[2*MAXN];
int ysort[2*MAXN];
int xinv[2*MAXN];
int yinv[2*MAXN];
ll dist[2*MAXN];
struct compx {
bool operator()(int a, int b) {
pii compa = pii(-points[a].first, points[a].second);
pii compb = pii(-points[b].first, points[b].second);
pii compa2 = pii(a&1, a);
pii compb2 = pii(b&1, b);
return (compa == compb) ? (compa2 < compb2) : (compa < compb);
}
} compx;
struct compy {
bool operator()(int a, int b) {
pii compa = pii(points[a].second, -points[a].first);
pii compb = pii(points[b].second, -points[b].first);
pii compa2 = pii(a&1, a);
pii compb2 = pii(b&1, b);
return (compa == compb) ? (compa2 < compb2) : (compa < compb);
}
} compy;
class stree {
public:
int lp, rp;
stree *l = nullptr;
stree *r = nullptr;
int mn; // min by y
stree(int lv, int rv) {
lp = lv;
rp = rv;
if (lp < rp) {
int mid = (lp+rp)/2;
l = new stree(lp, mid);
r = new stree(mid+1, rp);
mn = min(l->mn, r->mn);
}
else mn = yinv[xsort[lp]];
}
int get_mn(int lv, int rv) { // returns position of element in ysort/2
if (lp > rv || rp < lv) return inf;
if (lp >= lv && rp <= rv) return mn;
return min(l->get_mn(lv, rv), r->get_mn(lv, rv));
}
void del(int p) { // position of element in xsort
if (lp > p || rp < p) return;
if (lp == rp) {
mn = inf;
return;
}
l->del(p);
r->del(p);
mn = min(l->mn, r->mn);
}
};
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> t >> n;
for (int i = 0; i < n; i++) {
int x, y1, y2;
cin >> x >> y1 >> y2 >> cost[i];
if (y1 == 1) snodes.push_back(i);
if (y2 == t) enodes.push_back(i);
points[2*i] = pii(x-y1, x+y1);
points[2*i+1] = pii(x-y2-1, x+y2+1);
} // and prev, good
iota(xsort, xsort+2*n, 0);
iota(ysort, ysort+2*n, 0);
sort(xsort, xsort+2*n, compx);
sort(ysort, ysort+2*n, compy);
for (int i = 0; i < 2*n; i++) {
xinv[xsort[i]] = i;
yinv[ysort[i]] = i;
}
priority_queue<pli, vector<pli>, greater<pli>> pq; // and onwards
stree *tree = new stree(0, 2*n-1);
fill(dist, dist+2*n, -1);
for (int v: snodes) {
dist[v] = cost[v];
pq.push(pli(cost[v], v));
tree->del(xinv[2*v]);
tree->del(xinv[2*v+1]);
}
while (!pq.empty()) { // and onwards, good
pli tp = pq.top();
pq.pop();
int xpos = xinv[2*tp.second+1]-1;
int ypos = yinv[2*tp.second+1]-1;
int curv;
while ((curv = tree->get_mn(0, xpos)) <= ypos) { // good
curv = ysort[curv];
curv /= 2;
tree->del(xinv[2*curv]);
tree->del(xinv[2*curv+1]);
dist[curv] = dist[tp.second]+cost[curv];
pq.push(pli(dist[curv], curv));
}
}
ll ans = -1;
for (int v: enodes) {
if ((dist[v] != -1) && (ans == -1 || dist[v] < ans)) ans = dist[v];
}
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
145 ms |
26260 KB |
Output is correct |
2 |
Correct |
114 ms |
26176 KB |
Output is correct |
3 |
Correct |
172 ms |
27396 KB |
Output is correct |
4 |
Correct |
178 ms |
28832 KB |
Output is correct |
5 |
Correct |
185 ms |
31040 KB |
Output is correct |
6 |
Correct |
147 ms |
29160 KB |
Output is correct |
7 |
Correct |
136 ms |
28856 KB |
Output is correct |
8 |
Correct |
92 ms |
28828 KB |
Output is correct |
9 |
Correct |
85 ms |
28748 KB |
Output is correct |
10 |
Correct |
82 ms |
28820 KB |
Output is correct |
11 |
Correct |
241 ms |
31288 KB |
Output is correct |
12 |
Correct |
242 ms |
31196 KB |
Output is correct |
13 |
Correct |
215 ms |
30980 KB |
Output is correct |
14 |
Correct |
220 ms |
31012 KB |
Output is correct |
15 |
Correct |
172 ms |
28904 KB |
Output is correct |
16 |
Correct |
154 ms |
28876 KB |
Output is correct |
17 |
Correct |
154 ms |
28100 KB |
Output is correct |
18 |
Correct |
215 ms |
30448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
7 ms |
1752 KB |
Output is correct |
21 |
Correct |
7 ms |
1748 KB |
Output is correct |
22 |
Correct |
8 ms |
1876 KB |
Output is correct |
23 |
Correct |
7 ms |
1736 KB |
Output is correct |
24 |
Correct |
8 ms |
1876 KB |
Output is correct |
25 |
Correct |
8 ms |
1844 KB |
Output is correct |
26 |
Correct |
8 ms |
1748 KB |
Output is correct |
27 |
Correct |
6 ms |
1756 KB |
Output is correct |
28 |
Correct |
8 ms |
1864 KB |
Output is correct |
29 |
Correct |
7 ms |
1744 KB |
Output is correct |
30 |
Correct |
5 ms |
1756 KB |
Output is correct |
31 |
Correct |
5 ms |
1752 KB |
Output is correct |
32 |
Correct |
8 ms |
2004 KB |
Output is correct |
33 |
Correct |
8 ms |
2004 KB |
Output is correct |
34 |
Correct |
7 ms |
1748 KB |
Output is correct |
35 |
Correct |
9 ms |
1996 KB |
Output is correct |
36 |
Correct |
8 ms |
2012 KB |
Output is correct |
37 |
Correct |
6 ms |
1756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
145 ms |
26260 KB |
Output is correct |
2 |
Correct |
114 ms |
26176 KB |
Output is correct |
3 |
Correct |
172 ms |
27396 KB |
Output is correct |
4 |
Correct |
178 ms |
28832 KB |
Output is correct |
5 |
Correct |
185 ms |
31040 KB |
Output is correct |
6 |
Correct |
147 ms |
29160 KB |
Output is correct |
7 |
Correct |
136 ms |
28856 KB |
Output is correct |
8 |
Correct |
92 ms |
28828 KB |
Output is correct |
9 |
Correct |
85 ms |
28748 KB |
Output is correct |
10 |
Correct |
82 ms |
28820 KB |
Output is correct |
11 |
Correct |
241 ms |
31288 KB |
Output is correct |
12 |
Correct |
242 ms |
31196 KB |
Output is correct |
13 |
Correct |
215 ms |
30980 KB |
Output is correct |
14 |
Correct |
220 ms |
31012 KB |
Output is correct |
15 |
Correct |
172 ms |
28904 KB |
Output is correct |
16 |
Correct |
154 ms |
28876 KB |
Output is correct |
17 |
Correct |
154 ms |
28100 KB |
Output is correct |
18 |
Correct |
215 ms |
30448 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
336 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
7 ms |
1752 KB |
Output is correct |
39 |
Correct |
7 ms |
1748 KB |
Output is correct |
40 |
Correct |
8 ms |
1876 KB |
Output is correct |
41 |
Correct |
7 ms |
1736 KB |
Output is correct |
42 |
Correct |
8 ms |
1876 KB |
Output is correct |
43 |
Correct |
8 ms |
1844 KB |
Output is correct |
44 |
Correct |
8 ms |
1748 KB |
Output is correct |
45 |
Correct |
6 ms |
1756 KB |
Output is correct |
46 |
Correct |
8 ms |
1864 KB |
Output is correct |
47 |
Correct |
7 ms |
1744 KB |
Output is correct |
48 |
Correct |
5 ms |
1756 KB |
Output is correct |
49 |
Correct |
5 ms |
1752 KB |
Output is correct |
50 |
Correct |
8 ms |
2004 KB |
Output is correct |
51 |
Correct |
8 ms |
2004 KB |
Output is correct |
52 |
Correct |
7 ms |
1748 KB |
Output is correct |
53 |
Correct |
9 ms |
1996 KB |
Output is correct |
54 |
Correct |
8 ms |
2012 KB |
Output is correct |
55 |
Correct |
6 ms |
1756 KB |
Output is correct |
56 |
Correct |
184 ms |
29604 KB |
Output is correct |
57 |
Correct |
185 ms |
29984 KB |
Output is correct |
58 |
Correct |
155 ms |
28080 KB |
Output is correct |
59 |
Correct |
174 ms |
28456 KB |
Output is correct |
60 |
Correct |
187 ms |
29212 KB |
Output is correct |
61 |
Correct |
151 ms |
27992 KB |
Output is correct |
62 |
Correct |
193 ms |
29604 KB |
Output is correct |
63 |
Correct |
162 ms |
28960 KB |
Output is correct |
64 |
Correct |
139 ms |
28864 KB |
Output is correct |
65 |
Correct |
142 ms |
29012 KB |
Output is correct |
66 |
Correct |
168 ms |
29112 KB |
Output is correct |
67 |
Correct |
241 ms |
29832 KB |
Output is correct |
68 |
Correct |
181 ms |
29628 KB |
Output is correct |
69 |
Correct |
156 ms |
29544 KB |
Output is correct |
70 |
Correct |
233 ms |
29856 KB |
Output is correct |
71 |
Correct |
190 ms |
29620 KB |
Output is correct |
72 |
Correct |
146 ms |
29644 KB |
Output is correct |
73 |
Correct |
223 ms |
29868 KB |
Output is correct |
74 |
Correct |
83 ms |
29552 KB |
Output is correct |
75 |
Correct |
74 ms |
29612 KB |
Output is correct |
76 |
Correct |
259 ms |
31652 KB |
Output is correct |
77 |
Correct |
269 ms |
31968 KB |
Output is correct |
78 |
Correct |
213 ms |
31312 KB |
Output is correct |
79 |
Correct |
252 ms |
29672 KB |
Output is correct |
80 |
Correct |
161 ms |
29252 KB |
Output is correct |
81 |
Correct |
110 ms |
29552 KB |
Output is correct |
82 |
Correct |
169 ms |
28608 KB |
Output is correct |
83 |
Correct |
182 ms |
28872 KB |
Output is correct |
84 |
Correct |
246 ms |
28892 KB |
Output is correct |
85 |
Correct |
157 ms |
29616 KB |
Output is correct |
86 |
Correct |
138 ms |
29516 KB |
Output is correct |
87 |
Correct |
171 ms |
29516 KB |
Output is correct |
88 |
Correct |
146 ms |
29628 KB |
Output is correct |
89 |
Correct |
145 ms |
29704 KB |
Output is correct |
90 |
Correct |
240 ms |
31740 KB |
Output is correct |
91 |
Correct |
232 ms |
30740 KB |
Output is correct |
92 |
Correct |
174 ms |
29632 KB |
Output is correct |
93 |
Correct |
233 ms |
29636 KB |
Output is correct |
94 |
Correct |
187 ms |
29900 KB |
Output is correct |
95 |
Correct |
226 ms |
29612 KB |
Output is correct |
96 |
Correct |
251 ms |
31800 KB |
Output is correct |
97 |
Correct |
294 ms |
31752 KB |
Output is correct |
98 |
Correct |
280 ms |
31812 KB |
Output is correct |
99 |
Correct |
313 ms |
31812 KB |
Output is correct |
100 |
Correct |
243 ms |
31928 KB |
Output is correct |
101 |
Correct |
266 ms |
31684 KB |
Output is correct |
102 |
Correct |
279 ms |
32036 KB |
Output is correct |
103 |
Correct |
167 ms |
29972 KB |
Output is correct |