#include <iostream>
#include<vector>
#include<set>
#include<map>
#include<fstream>
#include<algorithm>
using namespace std;
#define ll long long
#define pi pair<ll,ll>
#define x first
#define y second
#define vi vector<int>
#define all(v) v.begin(),v.end()
#define l x.x
#define r x.y
#define t y.x
#define p y.y
#define maxn (1<<17)
#define maxs (1<<18)
ll min(ll a, ll b) { return a < b ? a : b; }
ll ab(ll n) { return n > 0 ? n : -n; }
vector<int> le(maxs), ri(maxs);
vector<set<pi>> v(maxs);
void bld() {
for (int i = maxn; i < maxs; i++) {
le[i] = ri[i] = i - maxn;
}
for (int i = maxn - 1; i > 0; i--) {
le[i] = le[i << 1];
ri[i] = ri[(i << 1) + 1];
}
}
void insert(int i, int j, int ind) {
//cout << i << ' ' << j << ' ' << ind << endl;
i = 2 * (i + maxn);
while (i >>= 1) v[i].insert({j, ind});
}
void del(int i, int j, int ind) {
i = 2 * (i + maxn);
while (i >>= 1) v[i].erase(v[i].find({ j, ind }));
}
void que(int i, int ex, int ey,vi&res) {
if (le[i] > ex) return;
if (ri[i] <= ex) {
//for (auto it : v[i]) {cout << it.x << ' ' << it.y << " ";}cout << endl;
for (auto it : v[i]) {
if (it.x > ey) break;
res.push_back(it.y);
}
}
else {
que(i << 1, ex, ey, res);
que((i << 1)+1, ex, ey, res);
}
}
int s, n;
long long func(int S, int N, vector<int> T, vector<int> L, vector<int> R, vector<int> P) {
s = S; n = N;
bld();
//N <= 5000
//vector<pair<pi, pi>> vec(n);
//for (int i = 0; i < n; i++) vec[i] = { {L[i],R[i]}, {T[i],P[i]} };
map<int, int> mp;
for (int i = 0; i < n; i++) mp[L[i] - T[i]] = 0;
int c = 0;
for (auto it : mp) mp[it.x] = c++;
//vector<ll> dist(n, 1e18);
//for (int i = 0; i < n; i++) if (L[i] == 1) dist[i] = P[i];
ll res = 1e18;
set<pair<ll, int>> st;
for (int i = 0; i < n; i++) {
if (L[i] == 1) st.insert({ P[i],i });
else insert(mp[L[i]-T[i]],L[i]+T[i],i);
}
while (st.size()) {
auto it = st.begin();
int i = it->y;
ll dst = it->x;
st.erase(it);
//cout << i << ' ' << dst << endl;
if (R[i] == s) res = min(res, dst);
auto lt = mp.upper_bound(R[i] - T[i] + 1);
if (lt == mp.begin()) continue;
lt--;
int u = lt->y;
//cout << u << endl;
vector<int> vec;
que(1, u, R[i]+T[i]+1, vec);
//cout << "vec: "; for (int j : vec) cout << j << ' '; cout << endl;
for (int j : vec) {
st.insert({ dst + P[j],j });
del(mp[L[j] - T[j]], L[j] + T[j], j);
}
/*for (int j = 0; j < n; j++) {
if (R[i] - T[i] + 1 >= L[j] - T[j] && R[i] + T[i] + 1 >= L[j] + T[j]) {
if (dist[j] > P[j] + dst) {
st.erase(st.find({ dist[j],j }));
dist[j] = P[j] + dst;
st.insert({ dist[j],j });
}
}
}*/
}
//ll res = 1e18;
//for (int i = 0; i < n; i++) if (R[i] == s && dist[i] < res) res = dist[i];
return res == 1e18 ? -1 : res;
/*map<int, ll> mp;
vector<pair<pi, pi>> vec(n);
for (int i = 0; i < n; i++) vec[i] = { {L[i],R[i]}, {T[i],P[i]} };
sort(all(vec));
mp[s + 1] = 0;
for (int i = n - 1; i >= 0; i--) {
auto it = mp.upper_bound(vec[i].r + 1);
if (it != mp.begin()) {
it--;
if (mp.find(vec[i].l) == mp.end()) mp[vec[i].l] = vec[i].p + it->y;
else mp[vec[i].l] = min(mp[vec[i].l], vec[i].p + it->y);
auto it = mp.find(vec[i].l);
while (next(it) != mp.end() && it->y < next(it)->y) mp.erase(next(it));
}
}
if (mp.find(1) != mp.end()) return mp[1];
return -1;*/
}
void run_tests() {
string name = "03-00.txt";
for (int i = 1; i <= 18; i++) {
cout << "TEST " << i << endl;
name[3] = '0' + i / 10;
name[4] = '0' + i % 10;
ifstream fin("in/" + name);
ifstream fout("out/" + name);
int S, N;
fin >> S >> N;
vector<int> T(N), L(N), R(N), P(N);
for (int i = 0; i < N; i++) fin >> T[i] >> L[i] >> R[i] >> P[i];
ll u = func(S, N, T, L, R, P),u1;
fout >> u1;
if (u != u1) {
cout << i << endl;
cout << S << ' ' << N << endl;
cout << u << ' ' << u1 << endl;
//for (int i = 0; i < N; i++) cout << T[i] << ' ' << L[i] << ' ' << R[i] << ' ' << P[i] << endl;
return;
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
//run_tests();
//return 0;
int S, N;
cin >> S >> N;
vector<int> T(N), L(N), R(N), P(N);
for (int i = 0; i < N; i++) cin >> T[i] >> L[i] >> R[i] >> P[i];
cout << func(S, N, move(T), move(L), move(R), move(P)) << endl;
return 0;
}
/*
10 5
2 5 10 3
1 1 6 5
5 2 8 3
7 6 10 4
4 1 3 1
7
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
746 ms |
137140 KB |
Output is correct |
2 |
Correct |
798 ms |
137128 KB |
Output is correct |
3 |
Correct |
506 ms |
107168 KB |
Output is correct |
4 |
Correct |
497 ms |
107128 KB |
Output is correct |
5 |
Correct |
431 ms |
131148 KB |
Output is correct |
6 |
Correct |
453 ms |
132180 KB |
Output is correct |
7 |
Correct |
375 ms |
132436 KB |
Output is correct |
8 |
Correct |
523 ms |
131020 KB |
Output is correct |
9 |
Correct |
553 ms |
131932 KB |
Output is correct |
10 |
Correct |
500 ms |
132604 KB |
Output is correct |
11 |
Correct |
911 ms |
137528 KB |
Output is correct |
12 |
Correct |
904 ms |
137320 KB |
Output is correct |
13 |
Correct |
911 ms |
136868 KB |
Output is correct |
14 |
Correct |
942 ms |
136680 KB |
Output is correct |
15 |
Correct |
790 ms |
137012 KB |
Output is correct |
16 |
Correct |
843 ms |
136784 KB |
Output is correct |
17 |
Correct |
742 ms |
135928 KB |
Output is correct |
18 |
Correct |
886 ms |
137188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
14684 KB |
Output is correct |
2 |
Correct |
4 ms |
14624 KB |
Output is correct |
3 |
Correct |
5 ms |
14684 KB |
Output is correct |
4 |
Correct |
4 ms |
14684 KB |
Output is correct |
5 |
Correct |
4 ms |
14872 KB |
Output is correct |
6 |
Correct |
4 ms |
14856 KB |
Output is correct |
7 |
Correct |
5 ms |
14684 KB |
Output is correct |
8 |
Correct |
5 ms |
14684 KB |
Output is correct |
9 |
Correct |
4 ms |
14684 KB |
Output is correct |
10 |
Correct |
4 ms |
14804 KB |
Output is correct |
11 |
Correct |
4 ms |
14684 KB |
Output is correct |
12 |
Correct |
4 ms |
14684 KB |
Output is correct |
13 |
Correct |
4 ms |
14684 KB |
Output is correct |
14 |
Correct |
4 ms |
14872 KB |
Output is correct |
15 |
Correct |
4 ms |
14684 KB |
Output is correct |
16 |
Correct |
4 ms |
14684 KB |
Output is correct |
17 |
Correct |
4 ms |
14684 KB |
Output is correct |
18 |
Correct |
5 ms |
14684 KB |
Output is correct |
19 |
Correct |
4 ms |
14840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
14684 KB |
Output is correct |
2 |
Correct |
4 ms |
14624 KB |
Output is correct |
3 |
Correct |
5 ms |
14684 KB |
Output is correct |
4 |
Correct |
4 ms |
14684 KB |
Output is correct |
5 |
Correct |
4 ms |
14872 KB |
Output is correct |
6 |
Correct |
4 ms |
14856 KB |
Output is correct |
7 |
Correct |
5 ms |
14684 KB |
Output is correct |
8 |
Correct |
5 ms |
14684 KB |
Output is correct |
9 |
Correct |
4 ms |
14684 KB |
Output is correct |
10 |
Correct |
4 ms |
14804 KB |
Output is correct |
11 |
Correct |
4 ms |
14684 KB |
Output is correct |
12 |
Correct |
4 ms |
14684 KB |
Output is correct |
13 |
Correct |
4 ms |
14684 KB |
Output is correct |
14 |
Correct |
4 ms |
14872 KB |
Output is correct |
15 |
Correct |
4 ms |
14684 KB |
Output is correct |
16 |
Correct |
4 ms |
14684 KB |
Output is correct |
17 |
Correct |
4 ms |
14684 KB |
Output is correct |
18 |
Correct |
5 ms |
14684 KB |
Output is correct |
19 |
Correct |
4 ms |
14840 KB |
Output is correct |
20 |
Correct |
19 ms |
19508 KB |
Output is correct |
21 |
Correct |
19 ms |
19292 KB |
Output is correct |
22 |
Correct |
26 ms |
20828 KB |
Output is correct |
23 |
Correct |
24 ms |
20828 KB |
Output is correct |
24 |
Correct |
24 ms |
20056 KB |
Output is correct |
25 |
Correct |
27 ms |
20828 KB |
Output is correct |
26 |
Correct |
24 ms |
20828 KB |
Output is correct |
27 |
Correct |
21 ms |
20832 KB |
Output is correct |
28 |
Correct |
26 ms |
20584 KB |
Output is correct |
29 |
Correct |
25 ms |
20816 KB |
Output is correct |
30 |
Correct |
16 ms |
20828 KB |
Output is correct |
31 |
Correct |
17 ms |
20836 KB |
Output is correct |
32 |
Correct |
29 ms |
21024 KB |
Output is correct |
33 |
Correct |
28 ms |
20820 KB |
Output is correct |
34 |
Correct |
23 ms |
20824 KB |
Output is correct |
35 |
Correct |
27 ms |
20828 KB |
Output is correct |
36 |
Correct |
28 ms |
20828 KB |
Output is correct |
37 |
Correct |
23 ms |
20828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
746 ms |
137140 KB |
Output is correct |
2 |
Correct |
798 ms |
137128 KB |
Output is correct |
3 |
Correct |
506 ms |
107168 KB |
Output is correct |
4 |
Correct |
497 ms |
107128 KB |
Output is correct |
5 |
Correct |
431 ms |
131148 KB |
Output is correct |
6 |
Correct |
453 ms |
132180 KB |
Output is correct |
7 |
Correct |
375 ms |
132436 KB |
Output is correct |
8 |
Correct |
523 ms |
131020 KB |
Output is correct |
9 |
Correct |
553 ms |
131932 KB |
Output is correct |
10 |
Correct |
500 ms |
132604 KB |
Output is correct |
11 |
Correct |
911 ms |
137528 KB |
Output is correct |
12 |
Correct |
904 ms |
137320 KB |
Output is correct |
13 |
Correct |
911 ms |
136868 KB |
Output is correct |
14 |
Correct |
942 ms |
136680 KB |
Output is correct |
15 |
Correct |
790 ms |
137012 KB |
Output is correct |
16 |
Correct |
843 ms |
136784 KB |
Output is correct |
17 |
Correct |
742 ms |
135928 KB |
Output is correct |
18 |
Correct |
886 ms |
137188 KB |
Output is correct |
19 |
Correct |
4 ms |
14684 KB |
Output is correct |
20 |
Correct |
4 ms |
14624 KB |
Output is correct |
21 |
Correct |
5 ms |
14684 KB |
Output is correct |
22 |
Correct |
4 ms |
14684 KB |
Output is correct |
23 |
Correct |
4 ms |
14872 KB |
Output is correct |
24 |
Correct |
4 ms |
14856 KB |
Output is correct |
25 |
Correct |
5 ms |
14684 KB |
Output is correct |
26 |
Correct |
5 ms |
14684 KB |
Output is correct |
27 |
Correct |
4 ms |
14684 KB |
Output is correct |
28 |
Correct |
4 ms |
14804 KB |
Output is correct |
29 |
Correct |
4 ms |
14684 KB |
Output is correct |
30 |
Correct |
4 ms |
14684 KB |
Output is correct |
31 |
Correct |
4 ms |
14684 KB |
Output is correct |
32 |
Correct |
4 ms |
14872 KB |
Output is correct |
33 |
Correct |
4 ms |
14684 KB |
Output is correct |
34 |
Correct |
4 ms |
14684 KB |
Output is correct |
35 |
Correct |
4 ms |
14684 KB |
Output is correct |
36 |
Correct |
5 ms |
14684 KB |
Output is correct |
37 |
Correct |
4 ms |
14840 KB |
Output is correct |
38 |
Correct |
19 ms |
19508 KB |
Output is correct |
39 |
Correct |
19 ms |
19292 KB |
Output is correct |
40 |
Correct |
26 ms |
20828 KB |
Output is correct |
41 |
Correct |
24 ms |
20828 KB |
Output is correct |
42 |
Correct |
24 ms |
20056 KB |
Output is correct |
43 |
Correct |
27 ms |
20828 KB |
Output is correct |
44 |
Correct |
24 ms |
20828 KB |
Output is correct |
45 |
Correct |
21 ms |
20832 KB |
Output is correct |
46 |
Correct |
26 ms |
20584 KB |
Output is correct |
47 |
Correct |
25 ms |
20816 KB |
Output is correct |
48 |
Correct |
16 ms |
20828 KB |
Output is correct |
49 |
Correct |
17 ms |
20836 KB |
Output is correct |
50 |
Correct |
29 ms |
21024 KB |
Output is correct |
51 |
Correct |
28 ms |
20820 KB |
Output is correct |
52 |
Correct |
23 ms |
20824 KB |
Output is correct |
53 |
Correct |
27 ms |
20828 KB |
Output is correct |
54 |
Correct |
28 ms |
20828 KB |
Output is correct |
55 |
Correct |
23 ms |
20828 KB |
Output is correct |
56 |
Correct |
554 ms |
108488 KB |
Output is correct |
57 |
Correct |
690 ms |
110140 KB |
Output is correct |
58 |
Correct |
782 ms |
135528 KB |
Output is correct |
59 |
Correct |
802 ms |
135984 KB |
Output is correct |
60 |
Correct |
1012 ms |
137252 KB |
Output is correct |
61 |
Correct |
776 ms |
135360 KB |
Output is correct |
62 |
Correct |
532 ms |
108732 KB |
Output is correct |
63 |
Correct |
688 ms |
136852 KB |
Output is correct |
64 |
Correct |
685 ms |
136840 KB |
Output is correct |
65 |
Correct |
694 ms |
137096 KB |
Output is correct |
66 |
Correct |
1049 ms |
137332 KB |
Output is correct |
67 |
Correct |
1228 ms |
136712 KB |
Output is correct |
68 |
Correct |
982 ms |
137612 KB |
Output is correct |
69 |
Correct |
627 ms |
137812 KB |
Output is correct |
70 |
Correct |
1332 ms |
137064 KB |
Output is correct |
71 |
Correct |
1054 ms |
137692 KB |
Output is correct |
72 |
Correct |
603 ms |
137552 KB |
Output is correct |
73 |
Correct |
1281 ms |
137096 KB |
Output is correct |
74 |
Correct |
795 ms |
137808 KB |
Output is correct |
75 |
Correct |
588 ms |
137452 KB |
Output is correct |
76 |
Correct |
931 ms |
137760 KB |
Output is correct |
77 |
Correct |
1373 ms |
138300 KB |
Output is correct |
78 |
Correct |
875 ms |
137308 KB |
Output is correct |
79 |
Correct |
1525 ms |
137732 KB |
Output is correct |
80 |
Correct |
786 ms |
137420 KB |
Output is correct |
81 |
Correct |
1202 ms |
137708 KB |
Output is correct |
82 |
Correct |
910 ms |
136304 KB |
Output is correct |
83 |
Correct |
1101 ms |
136852 KB |
Output is correct |
84 |
Correct |
1484 ms |
136812 KB |
Output is correct |
85 |
Correct |
1188 ms |
137676 KB |
Output is correct |
86 |
Correct |
1129 ms |
137828 KB |
Output is correct |
87 |
Correct |
1158 ms |
137640 KB |
Output is correct |
88 |
Correct |
805 ms |
136552 KB |
Output is correct |
89 |
Correct |
1224 ms |
137576 KB |
Output is correct |
90 |
Correct |
1312 ms |
137988 KB |
Output is correct |
91 |
Correct |
1431 ms |
138180 KB |
Output is correct |
92 |
Correct |
1332 ms |
138068 KB |
Output is correct |
93 |
Correct |
1633 ms |
137756 KB |
Output is correct |
94 |
Correct |
998 ms |
135128 KB |
Output is correct |
95 |
Correct |
1390 ms |
137536 KB |
Output is correct |
96 |
Correct |
1437 ms |
137924 KB |
Output is correct |
97 |
Correct |
1516 ms |
138032 KB |
Output is correct |
98 |
Correct |
1490 ms |
137676 KB |
Output is correct |
99 |
Correct |
1521 ms |
137736 KB |
Output is correct |
100 |
Correct |
1041 ms |
125372 KB |
Output is correct |
101 |
Correct |
1495 ms |
137788 KB |
Output is correct |
102 |
Correct |
1449 ms |
138104 KB |
Output is correct |
103 |
Correct |
1319 ms |
137800 KB |
Output is correct |