/*
IN THE NAME OF GOD
*/
#include <bits/stdc++.h>
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef long double ld;
#define F first
#define S second
#define Mp make_pair
#define pb push_back
#define pf push_front
#define size(x) ((ll)x.size())
#define all(x) (x).begin(),(x).end()
#define kill(x) cout << x << '\n', exit(0);
#define fuck(x) cout << "(" << #x << " , " << x << ")" << endl
#define endl '\n'
const int M = 1e5+5, N = 3e5+23, lg = 17;
ll Mod = 1e9+7; //998244353;
inline ll MOD(ll a, ll mod=Mod) {a%=mod; (a<0)&&(a+=mod); return a;}
inline ll poww(ll a, ll b, ll mod=Mod) {
ll ans = 1;
a=MOD(a, mod);
while (b) {
if (b & 1) ans = MOD(ans*a, mod);
b >>= 1;
a = MOD(a*a, mod);
}
return ans;
}
int m, n, par[N], L[N], R[N], t[N], p[N], seg[N];
ll c[N], d[N * 2 * lg + N], id=1;
bool mark[N * 2 * lg + N];
vector<int> adj[N * 2 * lg + N];
vector<int> comp[2];
bool cmp(int i, int j) {
return (t[i] < t[j]);
}
void query(int v, int l, int r, int ind=1, int lc=1, int rc=(1<<lg)+1) {
if(lc >= r) return;
if(rc <= r) {
adj[v].pb(seg[ind]); return;
}
int mid = (lc+rc)/2;
query(v, l, r, 2*ind, lc, mid);
query(v, l, r, 2*ind+1, mid, rc);
}
void update(int ind, int v) {
adj[id].pb(seg[ind]);
adj[id].pb(v);
seg[ind] = id;
ind/=2; id++;
while(ind > 0) {
seg[ind] = id++;
adj[seg[ind]].pb(seg[2*ind]);
adj[seg[ind]].pb(seg[2*ind+1]);
ind /= 2;
}
}
int main () {
ios_base::sync_with_stdio(false), cin.tie(0);
cin>>n>>m; id = m + 1;
for(int i=1; i<=m; i++) {
cin>>t[i]>>L[i]>>R[i]>>c[i];
p[i] = i;
comp[0].pb(L[i]-t[i]);
comp[1].pb(L[i]+t[i]);
}
sort(p+1, p+m+1, cmp);
for(int i=(1<<(lg + 1)); i>=1; i--) {
seg[i] = id ++;
if(i < (1<<lg)) {
adj[seg[i]].pb(seg[2*i]);
adj[seg[i]].pb(seg[2*i+1]);
}
}
comp[0].pb(2e9); comp[1].pb(2e9);
sort(all(comp[0])); comp[0].resize(unique(all(comp[0]))-comp[0].begin());
sort(all(comp[1])); comp[1].resize(unique(all(comp[1]))-comp[1].begin());
for(int i=1; i<=m; i++) {
int ptr = lower_bound(all(comp[0]), R[p[i]]-t[p[i]]+2)-comp[0].begin();
if(ptr != 0) {
query(p[i], 1, ptr + 1);
}
ptr = lower_bound(all(comp[0]), L[p[i]]-t[p[i]])-comp[0].begin()+1;
update((1<<lg)+ptr-1, p[i]);
}
for(int i=(1<<(lg + 1)); i>=1; i--) {
seg[i] = id ++;
if(i < (1<<lg)) {
adj[seg[i]].pb(seg[2*i]);
adj[seg[i]].pb(seg[2*i+1]);
}
}
for(int i=m; i>=1; i--) {
int ptr = lower_bound(all(comp[1]), R[p[i]]+t[p[i]]+2)-comp[1].begin();
if(ptr != 0) {
query(p[i], 1, ptr + 1);
}
ptr = lower_bound(all(comp[1]), L[p[i]]+t[p[i]])-comp[1].begin()+1;
update((1<<lg)+ptr-1, p[i]);
}
priority_queue<pll, vector<pll>, greater<pll>> pq;
for(int i=1; i<=m; i++) {
if(L[i] == 1) {
pq.push({c[i], i});
}
}
while(size(pq) > 0) {
pll v = pq.top(); pq.pop();
if(mark[v.S] == 1) continue;
// fuck(v.S);
d[v.S] = v.F, mark[v.S] = 1;
for(auto u : adj[v.S]) {
if(mark[u] == 1) continue;
pq.push({v.F+(u <= m ? c[u] : 0ll), u});
}
}
ll ans = 2e18;
for(int i=1 ; i<=m; i++) {
if(d[i] == 0) continue;
if(R[i] == n) {
ans = min(ans, d[i]);
}
}
cout<<(ans==2e18 ? -1 : ans)<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
709 ms |
421572 KB |
Output is correct |
2 |
Correct |
479 ms |
419520 KB |
Output is correct |
3 |
Correct |
984 ms |
422644 KB |
Output is correct |
4 |
Correct |
873 ms |
421880 KB |
Output is correct |
5 |
Correct |
525 ms |
415548 KB |
Output is correct |
6 |
Correct |
526 ms |
414616 KB |
Output is correct |
7 |
Correct |
439 ms |
417432 KB |
Output is correct |
8 |
Correct |
341 ms |
391424 KB |
Output is correct |
9 |
Correct |
337 ms |
383428 KB |
Output is correct |
10 |
Correct |
321 ms |
383660 KB |
Output is correct |
11 |
Correct |
1066 ms |
424200 KB |
Output is correct |
12 |
Correct |
1067 ms |
424016 KB |
Output is correct |
13 |
Correct |
988 ms |
425516 KB |
Output is correct |
14 |
Correct |
944 ms |
425404 KB |
Output is correct |
15 |
Correct |
932 ms |
421788 KB |
Output is correct |
16 |
Correct |
923 ms |
421748 KB |
Output is correct |
17 |
Correct |
887 ms |
421824 KB |
Output is correct |
18 |
Correct |
903 ms |
423932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
256340 KB |
Output is correct |
2 |
Correct |
74 ms |
256340 KB |
Output is correct |
3 |
Correct |
67 ms |
256392 KB |
Output is correct |
4 |
Correct |
74 ms |
256332 KB |
Output is correct |
5 |
Correct |
70 ms |
256336 KB |
Output is correct |
6 |
Correct |
68 ms |
256336 KB |
Output is correct |
7 |
Correct |
69 ms |
256336 KB |
Output is correct |
8 |
Correct |
68 ms |
256384 KB |
Output is correct |
9 |
Correct |
67 ms |
256248 KB |
Output is correct |
10 |
Correct |
69 ms |
256296 KB |
Output is correct |
11 |
Correct |
80 ms |
256340 KB |
Output is correct |
12 |
Correct |
70 ms |
256340 KB |
Output is correct |
13 |
Correct |
74 ms |
256340 KB |
Output is correct |
14 |
Correct |
69 ms |
256336 KB |
Output is correct |
15 |
Correct |
68 ms |
256228 KB |
Output is correct |
16 |
Correct |
68 ms |
256252 KB |
Output is correct |
17 |
Correct |
70 ms |
256268 KB |
Output is correct |
18 |
Correct |
69 ms |
256340 KB |
Output is correct |
19 |
Correct |
72 ms |
256340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
256340 KB |
Output is correct |
2 |
Correct |
74 ms |
256340 KB |
Output is correct |
3 |
Correct |
67 ms |
256392 KB |
Output is correct |
4 |
Correct |
74 ms |
256332 KB |
Output is correct |
5 |
Correct |
70 ms |
256336 KB |
Output is correct |
6 |
Correct |
68 ms |
256336 KB |
Output is correct |
7 |
Correct |
69 ms |
256336 KB |
Output is correct |
8 |
Correct |
68 ms |
256384 KB |
Output is correct |
9 |
Correct |
67 ms |
256248 KB |
Output is correct |
10 |
Correct |
69 ms |
256296 KB |
Output is correct |
11 |
Correct |
80 ms |
256340 KB |
Output is correct |
12 |
Correct |
70 ms |
256340 KB |
Output is correct |
13 |
Correct |
74 ms |
256340 KB |
Output is correct |
14 |
Correct |
69 ms |
256336 KB |
Output is correct |
15 |
Correct |
68 ms |
256228 KB |
Output is correct |
16 |
Correct |
68 ms |
256252 KB |
Output is correct |
17 |
Correct |
70 ms |
256268 KB |
Output is correct |
18 |
Correct |
69 ms |
256340 KB |
Output is correct |
19 |
Correct |
72 ms |
256340 KB |
Output is correct |
20 |
Correct |
89 ms |
264528 KB |
Output is correct |
21 |
Correct |
87 ms |
264528 KB |
Output is correct |
22 |
Correct |
86 ms |
264276 KB |
Output is correct |
23 |
Correct |
83 ms |
264280 KB |
Output is correct |
24 |
Correct |
90 ms |
264788 KB |
Output is correct |
25 |
Correct |
94 ms |
264252 KB |
Output is correct |
26 |
Correct |
88 ms |
264272 KB |
Output is correct |
27 |
Correct |
85 ms |
264272 KB |
Output is correct |
28 |
Correct |
90 ms |
264532 KB |
Output is correct |
29 |
Correct |
88 ms |
264272 KB |
Output is correct |
30 |
Correct |
80 ms |
262740 KB |
Output is correct |
31 |
Correct |
80 ms |
262548 KB |
Output is correct |
32 |
Correct |
93 ms |
264784 KB |
Output is correct |
33 |
Correct |
93 ms |
264652 KB |
Output is correct |
34 |
Correct |
103 ms |
264276 KB |
Output is correct |
35 |
Correct |
94 ms |
264736 KB |
Output is correct |
36 |
Correct |
92 ms |
264652 KB |
Output is correct |
37 |
Correct |
91 ms |
264332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
709 ms |
421572 KB |
Output is correct |
2 |
Correct |
479 ms |
419520 KB |
Output is correct |
3 |
Correct |
984 ms |
422644 KB |
Output is correct |
4 |
Correct |
873 ms |
421880 KB |
Output is correct |
5 |
Correct |
525 ms |
415548 KB |
Output is correct |
6 |
Correct |
526 ms |
414616 KB |
Output is correct |
7 |
Correct |
439 ms |
417432 KB |
Output is correct |
8 |
Correct |
341 ms |
391424 KB |
Output is correct |
9 |
Correct |
337 ms |
383428 KB |
Output is correct |
10 |
Correct |
321 ms |
383660 KB |
Output is correct |
11 |
Correct |
1066 ms |
424200 KB |
Output is correct |
12 |
Correct |
1067 ms |
424016 KB |
Output is correct |
13 |
Correct |
988 ms |
425516 KB |
Output is correct |
14 |
Correct |
944 ms |
425404 KB |
Output is correct |
15 |
Correct |
932 ms |
421788 KB |
Output is correct |
16 |
Correct |
923 ms |
421748 KB |
Output is correct |
17 |
Correct |
887 ms |
421824 KB |
Output is correct |
18 |
Correct |
903 ms |
423932 KB |
Output is correct |
19 |
Correct |
73 ms |
256340 KB |
Output is correct |
20 |
Correct |
74 ms |
256340 KB |
Output is correct |
21 |
Correct |
67 ms |
256392 KB |
Output is correct |
22 |
Correct |
74 ms |
256332 KB |
Output is correct |
23 |
Correct |
70 ms |
256336 KB |
Output is correct |
24 |
Correct |
68 ms |
256336 KB |
Output is correct |
25 |
Correct |
69 ms |
256336 KB |
Output is correct |
26 |
Correct |
68 ms |
256384 KB |
Output is correct |
27 |
Correct |
67 ms |
256248 KB |
Output is correct |
28 |
Correct |
69 ms |
256296 KB |
Output is correct |
29 |
Correct |
80 ms |
256340 KB |
Output is correct |
30 |
Correct |
70 ms |
256340 KB |
Output is correct |
31 |
Correct |
74 ms |
256340 KB |
Output is correct |
32 |
Correct |
69 ms |
256336 KB |
Output is correct |
33 |
Correct |
68 ms |
256228 KB |
Output is correct |
34 |
Correct |
68 ms |
256252 KB |
Output is correct |
35 |
Correct |
70 ms |
256268 KB |
Output is correct |
36 |
Correct |
69 ms |
256340 KB |
Output is correct |
37 |
Correct |
72 ms |
256340 KB |
Output is correct |
38 |
Correct |
89 ms |
264528 KB |
Output is correct |
39 |
Correct |
87 ms |
264528 KB |
Output is correct |
40 |
Correct |
86 ms |
264276 KB |
Output is correct |
41 |
Correct |
83 ms |
264280 KB |
Output is correct |
42 |
Correct |
90 ms |
264788 KB |
Output is correct |
43 |
Correct |
94 ms |
264252 KB |
Output is correct |
44 |
Correct |
88 ms |
264272 KB |
Output is correct |
45 |
Correct |
85 ms |
264272 KB |
Output is correct |
46 |
Correct |
90 ms |
264532 KB |
Output is correct |
47 |
Correct |
88 ms |
264272 KB |
Output is correct |
48 |
Correct |
80 ms |
262740 KB |
Output is correct |
49 |
Correct |
80 ms |
262548 KB |
Output is correct |
50 |
Correct |
93 ms |
264784 KB |
Output is correct |
51 |
Correct |
93 ms |
264652 KB |
Output is correct |
52 |
Correct |
103 ms |
264276 KB |
Output is correct |
53 |
Correct |
94 ms |
264736 KB |
Output is correct |
54 |
Correct |
92 ms |
264652 KB |
Output is correct |
55 |
Correct |
91 ms |
264332 KB |
Output is correct |
56 |
Correct |
716 ms |
424564 KB |
Output is correct |
57 |
Correct |
731 ms |
424704 KB |
Output is correct |
58 |
Correct |
619 ms |
422388 KB |
Output is correct |
59 |
Correct |
593 ms |
423576 KB |
Output is correct |
60 |
Correct |
490 ms |
423364 KB |
Output is correct |
61 |
Correct |
592 ms |
422344 KB |
Output is correct |
62 |
Correct |
724 ms |
424748 KB |
Output is correct |
63 |
Correct |
442 ms |
423364 KB |
Output is correct |
64 |
Correct |
455 ms |
423488 KB |
Output is correct |
65 |
Correct |
736 ms |
422968 KB |
Output is correct |
66 |
Correct |
490 ms |
423372 KB |
Output is correct |
67 |
Correct |
846 ms |
424036 KB |
Output is correct |
68 |
Correct |
685 ms |
423876 KB |
Output is correct |
69 |
Correct |
579 ms |
423696 KB |
Output is correct |
70 |
Correct |
908 ms |
424072 KB |
Output is correct |
71 |
Correct |
659 ms |
423928 KB |
Output is correct |
72 |
Correct |
597 ms |
423740 KB |
Output is correct |
73 |
Correct |
824 ms |
424544 KB |
Output is correct |
74 |
Correct |
348 ms |
390084 KB |
Output is correct |
75 |
Correct |
326 ms |
389048 KB |
Output is correct |
76 |
Correct |
1004 ms |
427836 KB |
Output is correct |
77 |
Correct |
1016 ms |
427852 KB |
Output is correct |
78 |
Correct |
959 ms |
427456 KB |
Output is correct |
79 |
Correct |
788 ms |
423876 KB |
Output is correct |
80 |
Correct |
904 ms |
423388 KB |
Output is correct |
81 |
Correct |
356 ms |
391544 KB |
Output is correct |
82 |
Correct |
922 ms |
423244 KB |
Output is correct |
83 |
Correct |
942 ms |
423492 KB |
Output is correct |
84 |
Correct |
829 ms |
423108 KB |
Output is correct |
85 |
Correct |
533 ms |
423112 KB |
Output is correct |
86 |
Correct |
544 ms |
423124 KB |
Output is correct |
87 |
Correct |
607 ms |
423420 KB |
Output is correct |
88 |
Correct |
603 ms |
422600 KB |
Output is correct |
89 |
Correct |
574 ms |
423112 KB |
Output is correct |
90 |
Correct |
882 ms |
427084 KB |
Output is correct |
91 |
Correct |
754 ms |
425268 KB |
Output is correct |
92 |
Correct |
596 ms |
423408 KB |
Output is correct |
93 |
Correct |
788 ms |
423824 KB |
Output is correct |
94 |
Correct |
780 ms |
421832 KB |
Output is correct |
95 |
Correct |
747 ms |
423620 KB |
Output is correct |
96 |
Correct |
947 ms |
427016 KB |
Output is correct |
97 |
Correct |
969 ms |
427404 KB |
Output is correct |
98 |
Correct |
969 ms |
427312 KB |
Output is correct |
99 |
Correct |
1000 ms |
427324 KB |
Output is correct |
100 |
Correct |
770 ms |
425780 KB |
Output is correct |
101 |
Correct |
989 ms |
427320 KB |
Output is correct |
102 |
Correct |
981 ms |
428080 KB |
Output is correct |
103 |
Correct |
592 ms |
422720 KB |
Output is correct |