//Challenge: Accepted
#include <bits/stdc++.h>
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
while (l != r) cout << *l << " ", l++;
cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 100005
#define pii pair<ll, ll>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
const ll inf = 1LL<<60;
vector<pii> lef[maxn];
struct segtree{
vector<pii> seg[4*maxn];
void init(int cur, int l, int r) {
if (r <= l) return;
if (r - l == 1) {
seg[cur] = lef[l];
return;
}
int m = (l + r) / 2;
init(cur*2, l, m), init(cur*2+1, m, r);
int ind = 0;
for (int i = 0;i < seg[cur*2].size();i++) {
while (ind < seg[cur*2+1].size() && seg[cur*2+1][ind] > seg[cur*2][i]) {
seg[cur].push_back(seg[cur*2+1][ind++]);
}
seg[cur].push_back(seg[cur*2][i]);
}
while (ind < seg[cur*2+1].size()) {
seg[cur].push_back(seg[cur*2+1][ind++]);
}
}
void getind(int cur, int l, int r, int ql, int qr, int y, vector<int> &ret, bool add) {
if (r <= l || ql >= r || qr <= l || seg[cur].size() == 0 || seg[cur].back().ff > y) return;
if (ql <= l && qr >= r) {
while (seg[cur].size() && seg[cur].back().ff <= y) {
if (add) ret.push_back(seg[cur].back().ss);
seg[cur].pop_back();
}
add = 0;
}
if (r - l == 1) return;
int m = (l + r) / 2;
getind(cur*2, l, m, ql, qr, y, ret, add);
getind(cur*2+1, m, r, ql, qr, y, ret, add);
}
} tr;
struct seg{
int t, l, r, x, y;
int cost;
seg(){t = l = r = cost = 0;}
} a[maxn];
ll dis[maxn];
int main() {
io
int L, n;
cin >> L >> n;
vector<int> vx;
for (int i = 1;i <= n;i++) {
cin >> a[i].t >> a[i].l >> a[i].r >> a[i].cost;
a[i].x = a[i].l - a[i].t;
a[i].y = a[i].l + a[i].t;
vx.push_back(a[i].x);
}
sort(vx.begin(), vx.end());
vx.resize(int(unique(vx.begin(), vx.end()) - vx.begin()));
for (int i = 1;i <= n;i++) {
a[i].x = lower_bound(vx.begin(), vx.end(), a[i].x) - vx.begin();
lef[a[i].x].push_back({a[i].y, i});
}
for (int i = 0;i < n;i++) sort(lef[i].begin(), lef[i].end(), [&](pii x, pii y){return x > y;});
tr.init(1, 0, n);
priority_queue<pii, vector<pii>, greater<pii> > pq;
for (int i = 1;i <= n;i++) {
dis[i] = inf;
if (a[i].l == 1) {
dis[i] = a[i].cost;
pq.push({dis[i], i});
}
}
dis[n+1] = inf;
while (pq.size()) {
auto [d, cur] = pq.top();
pq.pop();
if (d != dis[cur]) continue;
if (a[cur].r == L) dis[n+1] = min(dis[n+1], d);
vector<int> upd;
int xv = upper_bound(vx.begin(), vx.end(), a[cur].r - a[cur].t + 1) - vx.begin();
tr.getind(1, 0, n, 0, xv, a[cur].r + a[cur].t + 1, upd, 1);
debug(cur, d, xv);
for (int i:upd) {
if (d + a[i].cost < dis[i]) {
dis[i] = d + a[i].cost;
pq.push({dis[i], i});
}
}
}
cout << (dis[n+1] == inf ? -1 : dis[n+1]) << "\n";
}
/*
1000000000 16
6 2 2 1
4 999999997 999999999 4
2 999999997 1000000000 2
3 1 4 3
3 999999997 1000000000 3
5 999999998 999999999 3
6 999999999 999999999 1
2 1 4 2
6 3 999999998 1
999999987 1 1000000000 10
999999986 1 1000000000 10
999999985 1 1000000000 10
4 2 4 4
5 2 3 3
1 999999997 1000000000 1
1 1 4 1
*/
Compilation message
treatment.cpp: In member function 'void segtree::init(int, int, int)':
treatment.cpp:35:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for (int i = 0;i < seg[cur*2].size();i++) {
| ~~^~~~~~~~~~~~~~~~~~~
treatment.cpp:36:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | while (ind < seg[cur*2+1].size() && seg[cur*2+1][ind] > seg[cur*2][i]) {
| ~~~~^~~~~~~~~~~~~~~~~~~~~
treatment.cpp:41:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | while (ind < seg[cur*2+1].size()) {
| ~~~~^~~~~~~~~~~~~~~~~~~~~
treatment.cpp: In function 'int main()':
treatment.cpp:12:20: warning: statement has no effect [-Wunused-value]
12 | #define debug(...) 0
| ^
treatment.cpp:104:3: note: in expansion of macro 'debug'
104 | debug(cur, d, xv);
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
169 ms |
57040 KB |
Output is correct |
2 |
Correct |
149 ms |
57392 KB |
Output is correct |
3 |
Correct |
144 ms |
53632 KB |
Output is correct |
4 |
Correct |
136 ms |
53756 KB |
Output is correct |
5 |
Correct |
103 ms |
50616 KB |
Output is correct |
6 |
Correct |
100 ms |
53012 KB |
Output is correct |
7 |
Correct |
114 ms |
52372 KB |
Output is correct |
8 |
Correct |
80 ms |
48244 KB |
Output is correct |
9 |
Correct |
80 ms |
52928 KB |
Output is correct |
10 |
Correct |
98 ms |
52420 KB |
Output is correct |
11 |
Correct |
169 ms |
60100 KB |
Output is correct |
12 |
Correct |
170 ms |
60216 KB |
Output is correct |
13 |
Correct |
163 ms |
59760 KB |
Output is correct |
14 |
Correct |
170 ms |
59848 KB |
Output is correct |
15 |
Correct |
164 ms |
57408 KB |
Output is correct |
16 |
Correct |
159 ms |
57268 KB |
Output is correct |
17 |
Correct |
157 ms |
57444 KB |
Output is correct |
18 |
Correct |
165 ms |
59968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14292 KB |
Output is correct |
2 |
Correct |
8 ms |
14416 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14320 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
8 ms |
14548 KB |
Output is correct |
7 |
Correct |
8 ms |
14420 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
8 ms |
14348 KB |
Output is correct |
10 |
Correct |
8 ms |
14420 KB |
Output is correct |
11 |
Correct |
7 ms |
14400 KB |
Output is correct |
12 |
Correct |
8 ms |
14380 KB |
Output is correct |
13 |
Correct |
8 ms |
14420 KB |
Output is correct |
14 |
Correct |
8 ms |
14360 KB |
Output is correct |
15 |
Correct |
7 ms |
14420 KB |
Output is correct |
16 |
Correct |
8 ms |
14312 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
9 ms |
14364 KB |
Output is correct |
19 |
Correct |
8 ms |
14420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14292 KB |
Output is correct |
2 |
Correct |
8 ms |
14416 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14320 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
8 ms |
14548 KB |
Output is correct |
7 |
Correct |
8 ms |
14420 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
8 ms |
14348 KB |
Output is correct |
10 |
Correct |
8 ms |
14420 KB |
Output is correct |
11 |
Correct |
7 ms |
14400 KB |
Output is correct |
12 |
Correct |
8 ms |
14380 KB |
Output is correct |
13 |
Correct |
8 ms |
14420 KB |
Output is correct |
14 |
Correct |
8 ms |
14360 KB |
Output is correct |
15 |
Correct |
7 ms |
14420 KB |
Output is correct |
16 |
Correct |
8 ms |
14312 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
9 ms |
14364 KB |
Output is correct |
19 |
Correct |
8 ms |
14420 KB |
Output is correct |
20 |
Correct |
13 ms |
16348 KB |
Output is correct |
21 |
Correct |
14 ms |
16352 KB |
Output is correct |
22 |
Correct |
16 ms |
16476 KB |
Output is correct |
23 |
Correct |
17 ms |
16572 KB |
Output is correct |
24 |
Correct |
16 ms |
16456 KB |
Output is correct |
25 |
Correct |
16 ms |
16568 KB |
Output is correct |
26 |
Correct |
18 ms |
16556 KB |
Output is correct |
27 |
Correct |
14 ms |
16468 KB |
Output is correct |
28 |
Correct |
15 ms |
16480 KB |
Output is correct |
29 |
Correct |
16 ms |
16468 KB |
Output is correct |
30 |
Correct |
13 ms |
16484 KB |
Output is correct |
31 |
Correct |
13 ms |
16472 KB |
Output is correct |
32 |
Correct |
15 ms |
16620 KB |
Output is correct |
33 |
Correct |
14 ms |
16680 KB |
Output is correct |
34 |
Correct |
14 ms |
16468 KB |
Output is correct |
35 |
Correct |
15 ms |
16596 KB |
Output is correct |
36 |
Correct |
16 ms |
16596 KB |
Output is correct |
37 |
Correct |
15 ms |
16468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
169 ms |
57040 KB |
Output is correct |
2 |
Correct |
149 ms |
57392 KB |
Output is correct |
3 |
Correct |
144 ms |
53632 KB |
Output is correct |
4 |
Correct |
136 ms |
53756 KB |
Output is correct |
5 |
Correct |
103 ms |
50616 KB |
Output is correct |
6 |
Correct |
100 ms |
53012 KB |
Output is correct |
7 |
Correct |
114 ms |
52372 KB |
Output is correct |
8 |
Correct |
80 ms |
48244 KB |
Output is correct |
9 |
Correct |
80 ms |
52928 KB |
Output is correct |
10 |
Correct |
98 ms |
52420 KB |
Output is correct |
11 |
Correct |
169 ms |
60100 KB |
Output is correct |
12 |
Correct |
170 ms |
60216 KB |
Output is correct |
13 |
Correct |
163 ms |
59760 KB |
Output is correct |
14 |
Correct |
170 ms |
59848 KB |
Output is correct |
15 |
Correct |
164 ms |
57408 KB |
Output is correct |
16 |
Correct |
159 ms |
57268 KB |
Output is correct |
17 |
Correct |
157 ms |
57444 KB |
Output is correct |
18 |
Correct |
165 ms |
59968 KB |
Output is correct |
19 |
Correct |
7 ms |
14292 KB |
Output is correct |
20 |
Correct |
8 ms |
14416 KB |
Output is correct |
21 |
Correct |
8 ms |
14420 KB |
Output is correct |
22 |
Correct |
8 ms |
14320 KB |
Output is correct |
23 |
Correct |
8 ms |
14420 KB |
Output is correct |
24 |
Correct |
8 ms |
14548 KB |
Output is correct |
25 |
Correct |
8 ms |
14420 KB |
Output is correct |
26 |
Correct |
8 ms |
14420 KB |
Output is correct |
27 |
Correct |
8 ms |
14348 KB |
Output is correct |
28 |
Correct |
8 ms |
14420 KB |
Output is correct |
29 |
Correct |
7 ms |
14400 KB |
Output is correct |
30 |
Correct |
8 ms |
14380 KB |
Output is correct |
31 |
Correct |
8 ms |
14420 KB |
Output is correct |
32 |
Correct |
8 ms |
14360 KB |
Output is correct |
33 |
Correct |
7 ms |
14420 KB |
Output is correct |
34 |
Correct |
8 ms |
14312 KB |
Output is correct |
35 |
Correct |
8 ms |
14420 KB |
Output is correct |
36 |
Correct |
9 ms |
14364 KB |
Output is correct |
37 |
Correct |
8 ms |
14420 KB |
Output is correct |
38 |
Correct |
13 ms |
16348 KB |
Output is correct |
39 |
Correct |
14 ms |
16352 KB |
Output is correct |
40 |
Correct |
16 ms |
16476 KB |
Output is correct |
41 |
Correct |
17 ms |
16572 KB |
Output is correct |
42 |
Correct |
16 ms |
16456 KB |
Output is correct |
43 |
Correct |
16 ms |
16568 KB |
Output is correct |
44 |
Correct |
18 ms |
16556 KB |
Output is correct |
45 |
Correct |
14 ms |
16468 KB |
Output is correct |
46 |
Correct |
15 ms |
16480 KB |
Output is correct |
47 |
Correct |
16 ms |
16468 KB |
Output is correct |
48 |
Correct |
13 ms |
16484 KB |
Output is correct |
49 |
Correct |
13 ms |
16472 KB |
Output is correct |
50 |
Correct |
15 ms |
16620 KB |
Output is correct |
51 |
Correct |
14 ms |
16680 KB |
Output is correct |
52 |
Correct |
14 ms |
16468 KB |
Output is correct |
53 |
Correct |
15 ms |
16596 KB |
Output is correct |
54 |
Correct |
16 ms |
16596 KB |
Output is correct |
55 |
Correct |
15 ms |
16468 KB |
Output is correct |
56 |
Correct |
155 ms |
55828 KB |
Output is correct |
57 |
Correct |
188 ms |
59632 KB |
Output is correct |
58 |
Correct |
170 ms |
57676 KB |
Output is correct |
59 |
Correct |
172 ms |
58044 KB |
Output is correct |
60 |
Correct |
211 ms |
59100 KB |
Output is correct |
61 |
Correct |
168 ms |
57668 KB |
Output is correct |
62 |
Correct |
153 ms |
55824 KB |
Output is correct |
63 |
Correct |
168 ms |
58820 KB |
Output is correct |
64 |
Correct |
172 ms |
58820 KB |
Output is correct |
65 |
Correct |
170 ms |
59000 KB |
Output is correct |
66 |
Correct |
211 ms |
59000 KB |
Output is correct |
67 |
Correct |
223 ms |
59332 KB |
Output is correct |
68 |
Correct |
242 ms |
59284 KB |
Output is correct |
69 |
Correct |
214 ms |
59288 KB |
Output is correct |
70 |
Correct |
231 ms |
59460 KB |
Output is correct |
71 |
Correct |
239 ms |
59392 KB |
Output is correct |
72 |
Correct |
193 ms |
59464 KB |
Output is correct |
73 |
Correct |
234 ms |
59352 KB |
Output is correct |
74 |
Correct |
127 ms |
59340 KB |
Output is correct |
75 |
Correct |
117 ms |
59296 KB |
Output is correct |
76 |
Correct |
189 ms |
62056 KB |
Output is correct |
77 |
Correct |
201 ms |
62152 KB |
Output is correct |
78 |
Correct |
174 ms |
61204 KB |
Output is correct |
79 |
Correct |
242 ms |
59344 KB |
Output is correct |
80 |
Correct |
168 ms |
58964 KB |
Output is correct |
81 |
Correct |
146 ms |
59332 KB |
Output is correct |
82 |
Correct |
175 ms |
58176 KB |
Output is correct |
83 |
Correct |
181 ms |
58280 KB |
Output is correct |
84 |
Correct |
245 ms |
58600 KB |
Output is correct |
85 |
Correct |
190 ms |
59264 KB |
Output is correct |
86 |
Correct |
183 ms |
59284 KB |
Output is correct |
87 |
Correct |
200 ms |
59296 KB |
Output is correct |
88 |
Correct |
180 ms |
60784 KB |
Output is correct |
89 |
Correct |
182 ms |
59256 KB |
Output is correct |
90 |
Correct |
185 ms |
61816 KB |
Output is correct |
91 |
Correct |
212 ms |
60580 KB |
Output is correct |
92 |
Correct |
186 ms |
59232 KB |
Output is correct |
93 |
Correct |
247 ms |
59280 KB |
Output is correct |
94 |
Correct |
154 ms |
57844 KB |
Output is correct |
95 |
Correct |
229 ms |
59212 KB |
Output is correct |
96 |
Correct |
186 ms |
62204 KB |
Output is correct |
97 |
Correct |
224 ms |
62112 KB |
Output is correct |
98 |
Correct |
209 ms |
62156 KB |
Output is correct |
99 |
Correct |
200 ms |
62248 KB |
Output is correct |
100 |
Correct |
155 ms |
58360 KB |
Output is correct |
101 |
Correct |
197 ms |
62176 KB |
Output is correct |
102 |
Correct |
183 ms |
62516 KB |
Output is correct |
103 |
Correct |
163 ms |
59340 KB |
Output is correct |