#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key
typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<int> vi;
typedef long double ld;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
struct segment
{
int t;
int l,r;
int cost;
segment(int _t, int _l, int _r, int _cost): t(_t), l(_l), r(_r), cost(_cost){}
bool operator < (const segment &s) const
{
if(t!=s.t) return t<s.t;
if(l!=s.l) return l<s.l;
if(r!=s.r) return r<s.r;
return cost<s.cost;
}
};
ll N; int n;
ll dist[333333];
vector<segment> vec;
vector<pair<int,int> > st[2][455555];
const ll INF = ll(1e18);
void add(int ty, int id, int l, int r, int pos, int nd)
{
if(pos>=r||pos<l) return ;
if(ty==0) st[ty][id].pb({vec[nd].l+vec[nd].t,nd});
else st[ty][id].pb({vec[nd].l-vec[nd].t,nd});
if(r-l<2) return ;
int mid=(l+r)>>1;
add(ty,id*2,l,mid,pos,nd); add(ty,id*2+1,mid,r,pos,nd);
}
void getnodes(int ty, int id, int l, int r, int ql, int qr, int X, vi &nodes) //<= X
{
if(ql>=r||l>=qr) return ;
if(ql<=l&&r<=qr)
{
while(!st[ty][id].empty()&&st[ty][id].back().fi<=X)
{
nodes.pb(st[ty][id].back().se);
st[ty][id].pop_back();
}
return ;
}
int mid=(l+r)>>1;
getnodes(ty,id*2,l,mid,ql,qr,X,nodes);
getnodes(ty,id*2+1,mid,r,ql,qr,X,nodes);
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>N>>n;
vector<int> Tcoord;
for(int i=0;i<n;i++)
{
int t,l,r,c; cin>>t>>l>>r>>c; l--;
vec.pb(segment(t,l,r,c));
Tcoord.pb(t);
}
sort(Tcoord.begin(),Tcoord.end());
Tcoord.erase(unique(Tcoord.begin(),Tcoord.end()),Tcoord.end());
ll ans=ll(1e18);
for(int i=0;i<n;i++)
{
dist[i]=ll(1e18);
if(vec[i].l==0) continue;
add(0,1,0,Tcoord.size(),lower_bound(Tcoord.begin(),Tcoord.end(),vec[i].t)-Tcoord.begin(),i);
add(1,1,0,Tcoord.size(),lower_bound(Tcoord.begin(),Tcoord.end(),vec[i].t)-Tcoord.begin(),i);
}
for(int i=0;i<422222;i++)
{
for(int j=0;j<2;j++)
{
sort(st[j][i].rbegin(),st[j][i].rend());
}
}
priority_queue<ii,vector<ii>,greater<ii> > pq;
for(int i=0;i<n;i++)
{
if(vec[i].l==0)
{
pq.push({vec[i].cost,i});
dist[i]=vec[i].cost;
}
}
while(!pq.empty())
{
ii cur = pq.top(); pq.pop();
int u = cur.se; ll d = cur.fi;
int pos = lower_bound(Tcoord.begin(),Tcoord.end(),vec[u].t)-Tcoord.begin();
vector<int> nodes;
//T_j<T_i, search in st 1
getnodes(1,1,0,Tcoord.size(),0,pos,vec[u].r-vec[u].t,nodes);
//T_j>=T_i, search in st 0
getnodes(0,1,0,Tcoord.size(),pos,Tcoord.size(),vec[u].r+vec[u].t,nodes);
sort(nodes.begin(),nodes.end());
nodes.erase(unique(nodes.begin(),nodes.end()),nodes.end());
for(int x:nodes)
{
if(dist[x]>=INF)
{
dist[x]=min(dist[x],d+vec[x].cost);
pq.push({dist[x],x});
}
}
}
for(int i=0;i<n;i++)
{
if(vec[i].r==N)
{
ans=min(ans,dist[i]);
}
}
if(ans>=ll(1e17)) ans=-1;
cout<<ans<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
100 ms |
27816 KB |
Output is correct |
2 |
Correct |
99 ms |
27816 KB |
Output is correct |
3 |
Correct |
103 ms |
28456 KB |
Output is correct |
4 |
Correct |
98 ms |
27432 KB |
Output is correct |
5 |
Correct |
112 ms |
30340 KB |
Output is correct |
6 |
Correct |
103 ms |
27820 KB |
Output is correct |
7 |
Correct |
99 ms |
27820 KB |
Output is correct |
8 |
Correct |
92 ms |
27820 KB |
Output is correct |
9 |
Correct |
94 ms |
27820 KB |
Output is correct |
10 |
Correct |
90 ms |
27816 KB |
Output is correct |
11 |
Correct |
121 ms |
30432 KB |
Output is correct |
12 |
Correct |
123 ms |
30472 KB |
Output is correct |
13 |
Correct |
117 ms |
29992 KB |
Output is correct |
14 |
Correct |
112 ms |
29992 KB |
Output is correct |
15 |
Correct |
117 ms |
27820 KB |
Output is correct |
16 |
Correct |
117 ms |
27944 KB |
Output is correct |
17 |
Correct |
104 ms |
27908 KB |
Output is correct |
18 |
Correct |
110 ms |
30504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
21760 KB |
Output is correct |
2 |
Correct |
19 ms |
21760 KB |
Output is correct |
3 |
Correct |
19 ms |
21760 KB |
Output is correct |
4 |
Correct |
19 ms |
21760 KB |
Output is correct |
5 |
Correct |
19 ms |
21760 KB |
Output is correct |
6 |
Correct |
18 ms |
21760 KB |
Output is correct |
7 |
Correct |
19 ms |
21760 KB |
Output is correct |
8 |
Correct |
18 ms |
21760 KB |
Output is correct |
9 |
Correct |
19 ms |
21760 KB |
Output is correct |
10 |
Correct |
19 ms |
21760 KB |
Output is correct |
11 |
Correct |
19 ms |
21760 KB |
Output is correct |
12 |
Correct |
19 ms |
21760 KB |
Output is correct |
13 |
Correct |
20 ms |
21760 KB |
Output is correct |
14 |
Correct |
19 ms |
21760 KB |
Output is correct |
15 |
Correct |
18 ms |
21760 KB |
Output is correct |
16 |
Correct |
19 ms |
21760 KB |
Output is correct |
17 |
Correct |
19 ms |
21760 KB |
Output is correct |
18 |
Correct |
19 ms |
21760 KB |
Output is correct |
19 |
Correct |
19 ms |
21760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
21760 KB |
Output is correct |
2 |
Correct |
19 ms |
21760 KB |
Output is correct |
3 |
Correct |
19 ms |
21760 KB |
Output is correct |
4 |
Correct |
19 ms |
21760 KB |
Output is correct |
5 |
Correct |
19 ms |
21760 KB |
Output is correct |
6 |
Correct |
18 ms |
21760 KB |
Output is correct |
7 |
Correct |
19 ms |
21760 KB |
Output is correct |
8 |
Correct |
18 ms |
21760 KB |
Output is correct |
9 |
Correct |
19 ms |
21760 KB |
Output is correct |
10 |
Correct |
19 ms |
21760 KB |
Output is correct |
11 |
Correct |
19 ms |
21760 KB |
Output is correct |
12 |
Correct |
19 ms |
21760 KB |
Output is correct |
13 |
Correct |
20 ms |
21760 KB |
Output is correct |
14 |
Correct |
19 ms |
21760 KB |
Output is correct |
15 |
Correct |
18 ms |
21760 KB |
Output is correct |
16 |
Correct |
19 ms |
21760 KB |
Output is correct |
17 |
Correct |
19 ms |
21760 KB |
Output is correct |
18 |
Correct |
19 ms |
21760 KB |
Output is correct |
19 |
Correct |
19 ms |
21760 KB |
Output is correct |
20 |
Correct |
36 ms |
23160 KB |
Output is correct |
21 |
Correct |
40 ms |
23296 KB |
Output is correct |
22 |
Correct |
33 ms |
23168 KB |
Output is correct |
23 |
Correct |
32 ms |
23168 KB |
Output is correct |
24 |
Correct |
39 ms |
23672 KB |
Output is correct |
25 |
Correct |
41 ms |
23808 KB |
Output is correct |
26 |
Correct |
38 ms |
23848 KB |
Output is correct |
27 |
Correct |
38 ms |
23808 KB |
Output is correct |
28 |
Correct |
39 ms |
23672 KB |
Output is correct |
29 |
Correct |
38 ms |
23808 KB |
Output is correct |
30 |
Correct |
32 ms |
23808 KB |
Output is correct |
31 |
Correct |
32 ms |
23808 KB |
Output is correct |
32 |
Correct |
47 ms |
24064 KB |
Output is correct |
33 |
Correct |
42 ms |
24064 KB |
Output is correct |
34 |
Correct |
41 ms |
23552 KB |
Output is correct |
35 |
Correct |
43 ms |
24064 KB |
Output is correct |
36 |
Correct |
42 ms |
24064 KB |
Output is correct |
37 |
Correct |
40 ms |
23552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
100 ms |
27816 KB |
Output is correct |
2 |
Correct |
99 ms |
27816 KB |
Output is correct |
3 |
Correct |
103 ms |
28456 KB |
Output is correct |
4 |
Correct |
98 ms |
27432 KB |
Output is correct |
5 |
Correct |
112 ms |
30340 KB |
Output is correct |
6 |
Correct |
103 ms |
27820 KB |
Output is correct |
7 |
Correct |
99 ms |
27820 KB |
Output is correct |
8 |
Correct |
92 ms |
27820 KB |
Output is correct |
9 |
Correct |
94 ms |
27820 KB |
Output is correct |
10 |
Correct |
90 ms |
27816 KB |
Output is correct |
11 |
Correct |
121 ms |
30432 KB |
Output is correct |
12 |
Correct |
123 ms |
30472 KB |
Output is correct |
13 |
Correct |
117 ms |
29992 KB |
Output is correct |
14 |
Correct |
112 ms |
29992 KB |
Output is correct |
15 |
Correct |
117 ms |
27820 KB |
Output is correct |
16 |
Correct |
117 ms |
27944 KB |
Output is correct |
17 |
Correct |
104 ms |
27908 KB |
Output is correct |
18 |
Correct |
110 ms |
30504 KB |
Output is correct |
19 |
Correct |
19 ms |
21760 KB |
Output is correct |
20 |
Correct |
19 ms |
21760 KB |
Output is correct |
21 |
Correct |
19 ms |
21760 KB |
Output is correct |
22 |
Correct |
19 ms |
21760 KB |
Output is correct |
23 |
Correct |
19 ms |
21760 KB |
Output is correct |
24 |
Correct |
18 ms |
21760 KB |
Output is correct |
25 |
Correct |
19 ms |
21760 KB |
Output is correct |
26 |
Correct |
18 ms |
21760 KB |
Output is correct |
27 |
Correct |
19 ms |
21760 KB |
Output is correct |
28 |
Correct |
19 ms |
21760 KB |
Output is correct |
29 |
Correct |
19 ms |
21760 KB |
Output is correct |
30 |
Correct |
19 ms |
21760 KB |
Output is correct |
31 |
Correct |
20 ms |
21760 KB |
Output is correct |
32 |
Correct |
19 ms |
21760 KB |
Output is correct |
33 |
Correct |
18 ms |
21760 KB |
Output is correct |
34 |
Correct |
19 ms |
21760 KB |
Output is correct |
35 |
Correct |
19 ms |
21760 KB |
Output is correct |
36 |
Correct |
19 ms |
21760 KB |
Output is correct |
37 |
Correct |
19 ms |
21760 KB |
Output is correct |
38 |
Correct |
36 ms |
23160 KB |
Output is correct |
39 |
Correct |
40 ms |
23296 KB |
Output is correct |
40 |
Correct |
33 ms |
23168 KB |
Output is correct |
41 |
Correct |
32 ms |
23168 KB |
Output is correct |
42 |
Correct |
39 ms |
23672 KB |
Output is correct |
43 |
Correct |
41 ms |
23808 KB |
Output is correct |
44 |
Correct |
38 ms |
23848 KB |
Output is correct |
45 |
Correct |
38 ms |
23808 KB |
Output is correct |
46 |
Correct |
39 ms |
23672 KB |
Output is correct |
47 |
Correct |
38 ms |
23808 KB |
Output is correct |
48 |
Correct |
32 ms |
23808 KB |
Output is correct |
49 |
Correct |
32 ms |
23808 KB |
Output is correct |
50 |
Correct |
47 ms |
24064 KB |
Output is correct |
51 |
Correct |
42 ms |
24064 KB |
Output is correct |
52 |
Correct |
41 ms |
23552 KB |
Output is correct |
53 |
Correct |
43 ms |
24064 KB |
Output is correct |
54 |
Correct |
42 ms |
24064 KB |
Output is correct |
55 |
Correct |
40 ms |
23552 KB |
Output is correct |
56 |
Correct |
586 ms |
53552 KB |
Output is correct |
57 |
Correct |
560 ms |
53548 KB |
Output is correct |
58 |
Correct |
658 ms |
60072 KB |
Output is correct |
59 |
Correct |
646 ms |
60072 KB |
Output is correct |
60 |
Correct |
459 ms |
55080 KB |
Output is correct |
61 |
Correct |
705 ms |
60120 KB |
Output is correct |
62 |
Correct |
605 ms |
53292 KB |
Output is correct |
63 |
Correct |
747 ms |
66984 KB |
Output is correct |
64 |
Correct |
717 ms |
67148 KB |
Output is correct |
65 |
Correct |
136 ms |
29200 KB |
Output is correct |
66 |
Correct |
509 ms |
54188 KB |
Output is correct |
67 |
Correct |
719 ms |
65972 KB |
Output is correct |
68 |
Correct |
647 ms |
66348 KB |
Output is correct |
69 |
Correct |
575 ms |
64800 KB |
Output is correct |
70 |
Correct |
822 ms |
66216 KB |
Output is correct |
71 |
Correct |
668 ms |
66088 KB |
Output is correct |
72 |
Correct |
600 ms |
65840 KB |
Output is correct |
73 |
Correct |
830 ms |
66192 KB |
Output is correct |
74 |
Correct |
417 ms |
65936 KB |
Output is correct |
75 |
Correct |
380 ms |
65576 KB |
Output is correct |
76 |
Correct |
782 ms |
65704 KB |
Output is correct |
77 |
Correct |
867 ms |
69544 KB |
Output is correct |
78 |
Correct |
789 ms |
65064 KB |
Output is correct |
79 |
Correct |
859 ms |
66780 KB |
Output is correct |
80 |
Correct |
790 ms |
62376 KB |
Output is correct |
81 |
Correct |
593 ms |
66216 KB |
Output is correct |
82 |
Correct |
803 ms |
63016 KB |
Output is correct |
83 |
Correct |
869 ms |
67240 KB |
Output is correct |
84 |
Correct |
847 ms |
66456 KB |
Output is correct |
85 |
Correct |
737 ms |
66104 KB |
Output is correct |
86 |
Correct |
741 ms |
66340 KB |
Output is correct |
87 |
Correct |
780 ms |
66344 KB |
Output is correct |
88 |
Correct |
794 ms |
66600 KB |
Output is correct |
89 |
Correct |
767 ms |
66344 KB |
Output is correct |
90 |
Correct |
873 ms |
69544 KB |
Output is correct |
91 |
Correct |
798 ms |
68392 KB |
Output is correct |
92 |
Correct |
744 ms |
66472 KB |
Output is correct |
93 |
Correct |
837 ms |
66216 KB |
Output is correct |
94 |
Correct |
855 ms |
66472 KB |
Output is correct |
95 |
Correct |
827 ms |
66348 KB |
Output is correct |
96 |
Correct |
871 ms |
69544 KB |
Output is correct |
97 |
Correct |
835 ms |
69344 KB |
Output is correct |
98 |
Correct |
860 ms |
69188 KB |
Output is correct |
99 |
Correct |
860 ms |
69416 KB |
Output is correct |
100 |
Correct |
804 ms |
67136 KB |
Output is correct |
101 |
Correct |
853 ms |
69232 KB |
Output is correct |
102 |
Correct |
862 ms |
70292 KB |
Output is correct |
103 |
Correct |
759 ms |
66680 KB |
Output is correct |