#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
#define endl '\n'
#define ll long long
#define pi pair<ll, int>
#define f first
#define s second
const int inf = 2000000011;
const int maxn = 100000;
int n, m;
int id[maxn], w[maxn], x[maxn], y[maxn], z[maxn];
ll d[maxn];
priority_queue<pi, vector<pi>, greater<pi>> pq;
struct segTree{
int l, r;
segTree *left, *right;
int val;
segTree(int a, int b){
l = a, r = b;
if(l != r){
int mid = (l + r) / 2;
left = new segTree(l, mid);
right = new segTree(mid + 1, r);
val = min(left->val, right->val);
}else{
int it = id[l];
val = x[it] + w[it];
}
}
void qry(int a, int b, ll v){
int it = id[l];
if(a < x[it] - w[it] || b < val) return;
if(l == r){
if(d[it] == (ll)inf * inf){
d[it] = v + z[it];
pq.push({d[it], it});
}
val = inf;
return;
}
left->qry(a, b, v);
right->qry(a, b, v);
val = min(left->val, right->val);
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> m >> n;
for(int i = 0; i < n; i++){
cin >> w[i] >> x[i] >> y[i] >> z[i];
id[i] = i;
if(x[i] == 1){
d[i] = z[i];
pq.push({d[i], i});
}else{
d[i] = (ll)inf * inf;
}
}
sort(id, id + n, [&](int i, int j){
return x[i] - w[i] == x[j] - w[j] ? x[i] + w[i] < x[j] + w[j] : x[i] - w[i] < x[j] - w[j];
});
segTree tre(0, n - 1);
while(!pq.empty()){
int it = pq.top().s;
pq.pop();
tre.qry(y[it] - w[it] + 1, y[it] + w[it] + 1, d[it]);
}
ll ret = (ll)inf * inf;
for(int i = 0; i < n; i++){
if(y[i] == m) ret = min(ret, d[i]);
}
cout << (ret == (ll)inf * inf ? -1 : ret) << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
99 ms |
12408 KB |
Output is correct |
2 |
Correct |
93 ms |
15608 KB |
Output is correct |
3 |
Correct |
94 ms |
15732 KB |
Output is correct |
4 |
Correct |
92 ms |
15092 KB |
Output is correct |
5 |
Correct |
91 ms |
17808 KB |
Output is correct |
6 |
Correct |
84 ms |
15864 KB |
Output is correct |
7 |
Correct |
83 ms |
15608 KB |
Output is correct |
8 |
Correct |
72 ms |
15608 KB |
Output is correct |
9 |
Correct |
73 ms |
15608 KB |
Output is correct |
10 |
Correct |
71 ms |
15608 KB |
Output is correct |
11 |
Correct |
101 ms |
17776 KB |
Output is correct |
12 |
Correct |
103 ms |
17780 KB |
Output is correct |
13 |
Correct |
99 ms |
17776 KB |
Output is correct |
14 |
Correct |
95 ms |
17780 KB |
Output is correct |
15 |
Correct |
93 ms |
15608 KB |
Output is correct |
16 |
Correct |
92 ms |
15612 KB |
Output is correct |
17 |
Correct |
93 ms |
14844 KB |
Output is correct |
18 |
Correct |
95 ms |
17008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
9 ms |
1152 KB |
Output is correct |
21 |
Correct |
9 ms |
1152 KB |
Output is correct |
22 |
Correct |
9 ms |
1152 KB |
Output is correct |
23 |
Correct |
9 ms |
1152 KB |
Output is correct |
24 |
Correct |
9 ms |
1308 KB |
Output is correct |
25 |
Correct |
11 ms |
1152 KB |
Output is correct |
26 |
Correct |
10 ms |
1152 KB |
Output is correct |
27 |
Correct |
9 ms |
1152 KB |
Output is correct |
28 |
Correct |
10 ms |
1280 KB |
Output is correct |
29 |
Correct |
10 ms |
1152 KB |
Output is correct |
30 |
Correct |
8 ms |
1152 KB |
Output is correct |
31 |
Correct |
8 ms |
1152 KB |
Output is correct |
32 |
Correct |
10 ms |
1408 KB |
Output is correct |
33 |
Correct |
9 ms |
1408 KB |
Output is correct |
34 |
Correct |
9 ms |
1152 KB |
Output is correct |
35 |
Correct |
10 ms |
1408 KB |
Output is correct |
36 |
Correct |
9 ms |
1280 KB |
Output is correct |
37 |
Correct |
9 ms |
1152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
99 ms |
12408 KB |
Output is correct |
2 |
Correct |
93 ms |
15608 KB |
Output is correct |
3 |
Correct |
94 ms |
15732 KB |
Output is correct |
4 |
Correct |
92 ms |
15092 KB |
Output is correct |
5 |
Correct |
91 ms |
17808 KB |
Output is correct |
6 |
Correct |
84 ms |
15864 KB |
Output is correct |
7 |
Correct |
83 ms |
15608 KB |
Output is correct |
8 |
Correct |
72 ms |
15608 KB |
Output is correct |
9 |
Correct |
73 ms |
15608 KB |
Output is correct |
10 |
Correct |
71 ms |
15608 KB |
Output is correct |
11 |
Correct |
101 ms |
17776 KB |
Output is correct |
12 |
Correct |
103 ms |
17780 KB |
Output is correct |
13 |
Correct |
99 ms |
17776 KB |
Output is correct |
14 |
Correct |
95 ms |
17780 KB |
Output is correct |
15 |
Correct |
93 ms |
15608 KB |
Output is correct |
16 |
Correct |
92 ms |
15612 KB |
Output is correct |
17 |
Correct |
93 ms |
14844 KB |
Output is correct |
18 |
Correct |
95 ms |
17008 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
4 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
4 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
4 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |
34 |
Correct |
4 ms |
384 KB |
Output is correct |
35 |
Correct |
4 ms |
384 KB |
Output is correct |
36 |
Correct |
4 ms |
384 KB |
Output is correct |
37 |
Correct |
5 ms |
384 KB |
Output is correct |
38 |
Correct |
9 ms |
1152 KB |
Output is correct |
39 |
Correct |
9 ms |
1152 KB |
Output is correct |
40 |
Correct |
9 ms |
1152 KB |
Output is correct |
41 |
Correct |
9 ms |
1152 KB |
Output is correct |
42 |
Correct |
9 ms |
1308 KB |
Output is correct |
43 |
Correct |
11 ms |
1152 KB |
Output is correct |
44 |
Correct |
10 ms |
1152 KB |
Output is correct |
45 |
Correct |
9 ms |
1152 KB |
Output is correct |
46 |
Correct |
10 ms |
1280 KB |
Output is correct |
47 |
Correct |
10 ms |
1152 KB |
Output is correct |
48 |
Correct |
8 ms |
1152 KB |
Output is correct |
49 |
Correct |
8 ms |
1152 KB |
Output is correct |
50 |
Correct |
10 ms |
1408 KB |
Output is correct |
51 |
Correct |
9 ms |
1408 KB |
Output is correct |
52 |
Correct |
9 ms |
1152 KB |
Output is correct |
53 |
Correct |
10 ms |
1408 KB |
Output is correct |
54 |
Correct |
9 ms |
1280 KB |
Output is correct |
55 |
Correct |
9 ms |
1152 KB |
Output is correct |
56 |
Correct |
105 ms |
15988 KB |
Output is correct |
57 |
Correct |
106 ms |
16368 KB |
Output is correct |
58 |
Correct |
103 ms |
14712 KB |
Output is correct |
59 |
Correct |
111 ms |
15224 KB |
Output is correct |
60 |
Correct |
127 ms |
15864 KB |
Output is correct |
61 |
Correct |
106 ms |
14840 KB |
Output is correct |
62 |
Correct |
107 ms |
15988 KB |
Output is correct |
63 |
Correct |
108 ms |
15736 KB |
Output is correct |
64 |
Correct |
120 ms |
15764 KB |
Output is correct |
65 |
Correct |
108 ms |
15864 KB |
Output is correct |
66 |
Correct |
117 ms |
15864 KB |
Output is correct |
67 |
Correct |
130 ms |
16376 KB |
Output is correct |
68 |
Correct |
130 ms |
16352 KB |
Output is correct |
69 |
Correct |
122 ms |
16380 KB |
Output is correct |
70 |
Correct |
131 ms |
16504 KB |
Output is correct |
71 |
Correct |
132 ms |
16376 KB |
Output is correct |
72 |
Correct |
112 ms |
16376 KB |
Output is correct |
73 |
Correct |
133 ms |
16552 KB |
Output is correct |
74 |
Correct |
79 ms |
16376 KB |
Output is correct |
75 |
Correct |
76 ms |
16376 KB |
Output is correct |
76 |
Correct |
104 ms |
18160 KB |
Output is correct |
77 |
Correct |
125 ms |
18672 KB |
Output is correct |
78 |
Correct |
105 ms |
18164 KB |
Output is correct |
79 |
Correct |
146 ms |
16416 KB |
Output is correct |
80 |
Correct |
98 ms |
15992 KB |
Output is correct |
81 |
Correct |
84 ms |
16376 KB |
Output is correct |
82 |
Correct |
93 ms |
15352 KB |
Output is correct |
83 |
Correct |
103 ms |
15744 KB |
Output is correct |
84 |
Correct |
148 ms |
15736 KB |
Output is correct |
85 |
Correct |
125 ms |
16376 KB |
Output is correct |
86 |
Correct |
107 ms |
16376 KB |
Output is correct |
87 |
Correct |
120 ms |
16376 KB |
Output is correct |
88 |
Correct |
105 ms |
16376 KB |
Output is correct |
89 |
Correct |
126 ms |
16504 KB |
Output is correct |
90 |
Correct |
114 ms |
18544 KB |
Output is correct |
91 |
Correct |
121 ms |
17524 KB |
Output is correct |
92 |
Correct |
114 ms |
16376 KB |
Output is correct |
93 |
Correct |
150 ms |
16376 KB |
Output is correct |
94 |
Correct |
107 ms |
16504 KB |
Output is correct |
95 |
Correct |
135 ms |
16376 KB |
Output is correct |
96 |
Correct |
117 ms |
18544 KB |
Output is correct |
97 |
Correct |
121 ms |
18544 KB |
Output is correct |
98 |
Correct |
115 ms |
18640 KB |
Output is correct |
99 |
Correct |
121 ms |
18544 KB |
Output is correct |
100 |
Correct |
112 ms |
18548 KB |
Output is correct |
101 |
Correct |
133 ms |
18544 KB |
Output is correct |
102 |
Correct |
106 ms |
18672 KB |
Output is correct |
103 |
Correct |
102 ms |
16668 KB |
Output is correct |