#include "bits/stdc++.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x,y) (lower_bound(all(x),y)-x.begin())
#define ubd(x,y) (upper_bound(all(x),y)-x.begin())
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) { return (rng() % (y+1-x)) + x; } //inclusivesss
string inline to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){ return a==0?llabs(b):gcd(b%a,a); }
typedef long long ll;
typedef long double ld;
#define FOR(i,s,e) for(ll i=s;i<=ll(e);++i)
#define DEC(i,s,e) for(ll i=s;i>=ll(e);--i)
typedef pair<ll,ll>pi; typedef pair<ll,pi>spi; typedef pair<pi,pi>dpi;
#define LLINF ((long long)1e18)
#define INF int(1e9+1e6)
#define MAXN (100006)
ll N,n,C[MAXN],dist[MAXN];
spi A[MAXN];
vector<int> d;
vector<int> v; // fill with the indexes u want to push
bitset<MAXN>done;
struct node{
int s,e,m;
priority_queue<pi,vector<pi>,greater<pi>> one, two;
node*l,*r;
node(int S,int E) {
s=S,e=E,m=(s+e)>>1;
while(one.size())one.pop();
while(two.size())two.pop();
if(s^e)l=new node(s,m),r=new node(m+1,e);
}
void update(int x,int ind){
one.emplace(A[ind].f+A[ind].s.f-1, ind);
two.emplace(A[ind].s.f-A[ind].f-1, ind);
if(s==e)return;
if(x>m)r->update(x,ind);
else l->update(x,ind);
}
void rmq(int x,int y,ll val,int co){
if(x>y)return;
if(s==x&&e==y){
if(co==1){
while(one.size()){
if(done[one.top().s]){one.pop();continue;}
if(val<one.top().f)break;
ll x=one.top().s; one.pop();
done[x]=1;
v.pb(x);
}
}else{
while(two.size()){
if(done[two.top().s]){two.pop();continue;}
if(val<two.top().f)break;
ll x=two.top().s; two.pop();
done[x]=1;
v.pb(x);
}
}
return;
}
if(x>m)return r->rmq(x,y,val,co);
else if(y<=m)return l->rmq(x,y,val,co);
else return l->rmq(x,m,val,co),r->rmq(m+1,y,val,co);
}
}*seg;
int main(){
FAST
cin>>N>>n;
FOR(i,1,n)cin>>A[i].f>>A[i].s.f>>A[i].s.s>>C[i],d.pb(A[i].f);
sort(all(d)),d.resize(unique(all(d))-d.begin());
seg=new node(0,siz(d)-1);
FOR(i,1,n)seg->update(lbd(d,A[i].f),i);
mmst(dist,-1);
priority_queue<pi,vector<pi>,greater<pi>> pq;
FOR(i,1,n)if(A[i].s.f==1)dist[i]=C[i],pq.emplace(dist[i],i),done[i]=1;
while(pq.size()){
ll x=pq.top().s,di=pq.top().f;
pq.pop();
if(dist[x]^di) assert(0);
v.clear();
seg->rmq(0,lbd(d,A[x].f),A[x].s.s-A[x].f,2); // if T1 >= T2
for(auto i:v) {assert(done[i]);
dist[i]=di+C[i],pq.emplace(dist[i],i);
}
v.clear();
seg->rmq(lbd(d,A[x].f)+1,siz(d)-1,A[x].f+A[x].s.s,1); // if T1 < T2
for(auto i:v){assert(done[i]);
dist[i]=di+C[i],pq.emplace(dist[i],i);
}
}
ll ans=LLINF;
FOR(i,1,n)if(A[i].s.s==N&&(~dist[i]))ans=min(ans,dist[i]);
if(ans==LLINF)ans=-1;
cout<<ans<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
8680 KB |
Output is correct |
2 |
Correct |
70 ms |
8680 KB |
Output is correct |
3 |
Correct |
84 ms |
9448 KB |
Output is correct |
4 |
Correct |
83 ms |
8680 KB |
Output is correct |
5 |
Correct |
105 ms |
10856 KB |
Output is correct |
6 |
Correct |
90 ms |
8680 KB |
Output is correct |
7 |
Correct |
80 ms |
8680 KB |
Output is correct |
8 |
Correct |
61 ms |
8680 KB |
Output is correct |
9 |
Correct |
59 ms |
8680 KB |
Output is correct |
10 |
Correct |
58 ms |
8684 KB |
Output is correct |
11 |
Correct |
98 ms |
11752 KB |
Output is correct |
12 |
Correct |
96 ms |
11496 KB |
Output is correct |
13 |
Correct |
95 ms |
10852 KB |
Output is correct |
14 |
Correct |
99 ms |
10852 KB |
Output is correct |
15 |
Correct |
87 ms |
8680 KB |
Output is correct |
16 |
Correct |
97 ms |
8680 KB |
Output is correct |
17 |
Correct |
83 ms |
8684 KB |
Output is correct |
18 |
Correct |
89 ms |
11776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1152 KB |
Output is correct |
2 |
Correct |
5 ms |
1152 KB |
Output is correct |
3 |
Correct |
5 ms |
1152 KB |
Output is correct |
4 |
Correct |
5 ms |
1152 KB |
Output is correct |
5 |
Correct |
5 ms |
1152 KB |
Output is correct |
6 |
Correct |
5 ms |
1152 KB |
Output is correct |
7 |
Correct |
5 ms |
1152 KB |
Output is correct |
8 |
Correct |
5 ms |
1152 KB |
Output is correct |
9 |
Correct |
5 ms |
1152 KB |
Output is correct |
10 |
Correct |
5 ms |
1152 KB |
Output is correct |
11 |
Correct |
5 ms |
1152 KB |
Output is correct |
12 |
Correct |
5 ms |
1152 KB |
Output is correct |
13 |
Correct |
7 ms |
1152 KB |
Output is correct |
14 |
Correct |
5 ms |
1152 KB |
Output is correct |
15 |
Correct |
5 ms |
1152 KB |
Output is correct |
16 |
Correct |
5 ms |
1152 KB |
Output is correct |
17 |
Correct |
5 ms |
1152 KB |
Output is correct |
18 |
Correct |
5 ms |
1152 KB |
Output is correct |
19 |
Correct |
5 ms |
1152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1152 KB |
Output is correct |
2 |
Correct |
5 ms |
1152 KB |
Output is correct |
3 |
Correct |
5 ms |
1152 KB |
Output is correct |
4 |
Correct |
5 ms |
1152 KB |
Output is correct |
5 |
Correct |
5 ms |
1152 KB |
Output is correct |
6 |
Correct |
5 ms |
1152 KB |
Output is correct |
7 |
Correct |
5 ms |
1152 KB |
Output is correct |
8 |
Correct |
5 ms |
1152 KB |
Output is correct |
9 |
Correct |
5 ms |
1152 KB |
Output is correct |
10 |
Correct |
5 ms |
1152 KB |
Output is correct |
11 |
Correct |
5 ms |
1152 KB |
Output is correct |
12 |
Correct |
5 ms |
1152 KB |
Output is correct |
13 |
Correct |
7 ms |
1152 KB |
Output is correct |
14 |
Correct |
5 ms |
1152 KB |
Output is correct |
15 |
Correct |
5 ms |
1152 KB |
Output is correct |
16 |
Correct |
5 ms |
1152 KB |
Output is correct |
17 |
Correct |
5 ms |
1152 KB |
Output is correct |
18 |
Correct |
5 ms |
1152 KB |
Output is correct |
19 |
Correct |
5 ms |
1152 KB |
Output is correct |
20 |
Correct |
21 ms |
4864 KB |
Output is correct |
21 |
Correct |
21 ms |
4864 KB |
Output is correct |
22 |
Correct |
17 ms |
3840 KB |
Output is correct |
23 |
Correct |
15 ms |
3840 KB |
Output is correct |
24 |
Correct |
23 ms |
5888 KB |
Output is correct |
25 |
Correct |
23 ms |
5760 KB |
Output is correct |
26 |
Correct |
22 ms |
5760 KB |
Output is correct |
27 |
Correct |
20 ms |
5632 KB |
Output is correct |
28 |
Correct |
24 ms |
5888 KB |
Output is correct |
29 |
Correct |
22 ms |
5760 KB |
Output is correct |
30 |
Correct |
15 ms |
5632 KB |
Output is correct |
31 |
Correct |
15 ms |
5632 KB |
Output is correct |
32 |
Correct |
23 ms |
6016 KB |
Output is correct |
33 |
Correct |
24 ms |
6016 KB |
Output is correct |
34 |
Correct |
25 ms |
5084 KB |
Output is correct |
35 |
Correct |
23 ms |
6016 KB |
Output is correct |
36 |
Correct |
23 ms |
6016 KB |
Output is correct |
37 |
Correct |
22 ms |
4992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
8680 KB |
Output is correct |
2 |
Correct |
70 ms |
8680 KB |
Output is correct |
3 |
Correct |
84 ms |
9448 KB |
Output is correct |
4 |
Correct |
83 ms |
8680 KB |
Output is correct |
5 |
Correct |
105 ms |
10856 KB |
Output is correct |
6 |
Correct |
90 ms |
8680 KB |
Output is correct |
7 |
Correct |
80 ms |
8680 KB |
Output is correct |
8 |
Correct |
61 ms |
8680 KB |
Output is correct |
9 |
Correct |
59 ms |
8680 KB |
Output is correct |
10 |
Correct |
58 ms |
8684 KB |
Output is correct |
11 |
Correct |
98 ms |
11752 KB |
Output is correct |
12 |
Correct |
96 ms |
11496 KB |
Output is correct |
13 |
Correct |
95 ms |
10852 KB |
Output is correct |
14 |
Correct |
99 ms |
10852 KB |
Output is correct |
15 |
Correct |
87 ms |
8680 KB |
Output is correct |
16 |
Correct |
97 ms |
8680 KB |
Output is correct |
17 |
Correct |
83 ms |
8684 KB |
Output is correct |
18 |
Correct |
89 ms |
11776 KB |
Output is correct |
19 |
Correct |
5 ms |
1152 KB |
Output is correct |
20 |
Correct |
5 ms |
1152 KB |
Output is correct |
21 |
Correct |
5 ms |
1152 KB |
Output is correct |
22 |
Correct |
5 ms |
1152 KB |
Output is correct |
23 |
Correct |
5 ms |
1152 KB |
Output is correct |
24 |
Correct |
5 ms |
1152 KB |
Output is correct |
25 |
Correct |
5 ms |
1152 KB |
Output is correct |
26 |
Correct |
5 ms |
1152 KB |
Output is correct |
27 |
Correct |
5 ms |
1152 KB |
Output is correct |
28 |
Correct |
5 ms |
1152 KB |
Output is correct |
29 |
Correct |
5 ms |
1152 KB |
Output is correct |
30 |
Correct |
5 ms |
1152 KB |
Output is correct |
31 |
Correct |
7 ms |
1152 KB |
Output is correct |
32 |
Correct |
5 ms |
1152 KB |
Output is correct |
33 |
Correct |
5 ms |
1152 KB |
Output is correct |
34 |
Correct |
5 ms |
1152 KB |
Output is correct |
35 |
Correct |
5 ms |
1152 KB |
Output is correct |
36 |
Correct |
5 ms |
1152 KB |
Output is correct |
37 |
Correct |
5 ms |
1152 KB |
Output is correct |
38 |
Correct |
21 ms |
4864 KB |
Output is correct |
39 |
Correct |
21 ms |
4864 KB |
Output is correct |
40 |
Correct |
17 ms |
3840 KB |
Output is correct |
41 |
Correct |
15 ms |
3840 KB |
Output is correct |
42 |
Correct |
23 ms |
5888 KB |
Output is correct |
43 |
Correct |
23 ms |
5760 KB |
Output is correct |
44 |
Correct |
22 ms |
5760 KB |
Output is correct |
45 |
Correct |
20 ms |
5632 KB |
Output is correct |
46 |
Correct |
24 ms |
5888 KB |
Output is correct |
47 |
Correct |
22 ms |
5760 KB |
Output is correct |
48 |
Correct |
15 ms |
5632 KB |
Output is correct |
49 |
Correct |
15 ms |
5632 KB |
Output is correct |
50 |
Correct |
23 ms |
6016 KB |
Output is correct |
51 |
Correct |
24 ms |
6016 KB |
Output is correct |
52 |
Correct |
25 ms |
5084 KB |
Output is correct |
53 |
Correct |
23 ms |
6016 KB |
Output is correct |
54 |
Correct |
23 ms |
6016 KB |
Output is correct |
55 |
Correct |
22 ms |
4992 KB |
Output is correct |
56 |
Correct |
615 ms |
83420 KB |
Output is correct |
57 |
Correct |
608 ms |
83804 KB |
Output is correct |
58 |
Correct |
565 ms |
81980 KB |
Output is correct |
59 |
Correct |
582 ms |
82008 KB |
Output is correct |
60 |
Correct |
438 ms |
64476 KB |
Output is correct |
61 |
Correct |
572 ms |
82012 KB |
Output is correct |
62 |
Correct |
609 ms |
83548 KB |
Output is correct |
63 |
Correct |
576 ms |
99420 KB |
Output is correct |
64 |
Correct |
531 ms |
99392 KB |
Output is correct |
65 |
Correct |
110 ms |
11748 KB |
Output is correct |
66 |
Correct |
367 ms |
64220 KB |
Output is correct |
67 |
Correct |
697 ms |
99036 KB |
Output is correct |
68 |
Correct |
584 ms |
98652 KB |
Output is correct |
69 |
Correct |
485 ms |
95704 KB |
Output is correct |
70 |
Correct |
765 ms |
99292 KB |
Output is correct |
71 |
Correct |
560 ms |
98524 KB |
Output is correct |
72 |
Correct |
486 ms |
98516 KB |
Output is correct |
73 |
Correct |
725 ms |
99000 KB |
Output is correct |
74 |
Correct |
247 ms |
98780 KB |
Output is correct |
75 |
Correct |
237 ms |
98528 KB |
Output is correct |
76 |
Correct |
632 ms |
90972 KB |
Output is correct |
77 |
Correct |
677 ms |
103128 KB |
Output is correct |
78 |
Correct |
733 ms |
90136 KB |
Output is correct |
79 |
Correct |
802 ms |
99452 KB |
Output is correct |
80 |
Correct |
805 ms |
87272 KB |
Output is correct |
81 |
Correct |
408 ms |
99796 KB |
Output is correct |
82 |
Correct |
771 ms |
87472 KB |
Output is correct |
83 |
Correct |
806 ms |
99556 KB |
Output is correct |
84 |
Correct |
786 ms |
99748 KB |
Output is correct |
85 |
Correct |
615 ms |
99420 KB |
Output is correct |
86 |
Correct |
619 ms |
99544 KB |
Output is correct |
87 |
Correct |
667 ms |
99412 KB |
Output is correct |
88 |
Correct |
662 ms |
99292 KB |
Output is correct |
89 |
Correct |
665 ms |
99544 KB |
Output is correct |
90 |
Correct |
687 ms |
103452 KB |
Output is correct |
91 |
Correct |
669 ms |
101464 KB |
Output is correct |
92 |
Correct |
646 ms |
99420 KB |
Output is correct |
93 |
Correct |
786 ms |
99548 KB |
Output is correct |
94 |
Correct |
754 ms |
99680 KB |
Output is correct |
95 |
Correct |
752 ms |
99552 KB |
Output is correct |
96 |
Correct |
689 ms |
103384 KB |
Output is correct |
97 |
Correct |
698 ms |
102492 KB |
Output is correct |
98 |
Correct |
665 ms |
102492 KB |
Output is correct |
99 |
Correct |
713 ms |
102360 KB |
Output is correct |
100 |
Correct |
702 ms |
103132 KB |
Output is correct |
101 |
Correct |
695 ms |
103396 KB |
Output is correct |
102 |
Correct |
692 ms |
103516 KB |
Output is correct |
103 |
Correct |
563 ms |
99804 KB |
Output is correct |