#include "bits/stdc++.h"
#define here cerr<<"===========================================\n"
#pragma GCC optimize("Ofast")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-finline")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-fno-stack-protector")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-funroll-loops")
#pragma GCC target("abm")
#pragma GCC target("avx")
#pragma GCC target("mmx")
#pragma GCC target("popcnt")
#pragma GCC target("sse")
#pragma GCC target("sse2")
#pragma GCC target("sse3")
#pragma GCC target("sse4")
#pragma GCC target("ssse3")
#pragma GCC target("tune=native")
#define dbg(x) cerr<<#x<<": "<<x<<endl;
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
#define ld double
#define ll long long
#define llinf 100000000000000000LL // 10^17
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) (ll)(a.size())
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {cerr<<#a<<": ";for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
#define cer(a) {cerr<<#a<<": ";for(ll x_ : a) cerr<<x_<< " ";cerr<<endl;}
#define daj_mi_malo_vremena ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
using namespace std;
//using namespace __gnu_pbds;
/*
ll add(ll x,ll y){
x+=y;
if(x<0){
x%=mod;
x+=mod;
}else{
if(x>=mod) x%=mod;
}
return x;
}
ll mul(ll a,ll b){
ll ans = (a*b)%mod;
if(ans<0) ans+=mod;
return ans;
}
typedef tree<int,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
typedef tree<int,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_multiset;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll rnd(ll l,ll r){
return uniform_int_distribution<ll>(l,r)(rng);
}
*/
#define maxn 100005
#define maxx 400025
ll n,m;
struct tupl{
ll t,l,r,c;
} a[maxn];
ll tsz = 1,tsz2 = 1;
pll t[4*maxn],t2[4*maxn];
void upd(ll v,ll tl,ll tr,ll i,pll p){
if(tl==tr){t[v] = p;return;}
ll mid = (tl+tr)/2;
if(i<=mid) upd(2*v,tl,mid,i,p);
else upd(2*v+1,mid+1,tr,i,p);
t[v] = min(t[2*v],t[2*v+1]);
}
void upd2(ll v,ll tl,ll tr,ll i,pll p){
if(tl==tr){t2[v] = p;return;}
ll mid = (tl+tr)/2;
if(i<=mid) upd2(2*v,tl,mid,i,p);
else upd2(2*v+1,mid+1,tr,i,p);
t2[v] = min(t2[2*v],t2[2*v+1]);
}
pll get(ll v,ll tl,ll tr,ll l,ll r){
if(l>r) return {llinf,0};
if(tl==l&&tr==r) return t[v];
ll mid = (tl+tr)/2;
return min(get(2*v,tl,mid,l,min(mid,r)),get(2*v+1,mid+1,tr,max(mid+1,l),r));
}
pll get2(ll v,ll tl,ll tr,ll l,ll r){
if(l>r) return {llinf,0};
if(tl==l&&tr==r) return t2[v];
ll mid = (tl+tr)/2;
return min(get2(2*v,tl,mid,l,min(mid,r)),get2(2*v+1,mid+1,tr,max(mid+1,l),r));
}
bool cmp(tupl x,tupl y){
return x.t<y.t;
}
ll d[maxn];
set<ll> st;
map<ll,ll> mni,mxi;
ll it = 0;
ll mxt;
void del(ll i){
upd(1,1,mxt,i,{llinf,0});
upd2(1,1,mxt,i,{llinf,0});
}
void tc(){
cin >> m >> n;
mxt = n;
for(ll i = 1;i<=n;i++) cin >> a[i].t >> a[i].l >> a[i].r >> a[i].c;
sort(a+1,a+1+n,cmp);
for(ll i = 1;i<=n;i++) mni[a[i].t] = llinf;
for(ll i = 1;i<=n;i++) mni[a[i].t] = min(mni[a[i].t],i);
for(ll i = 1;i<=n;i++) mxi[a[i].t] = 0;
for(ll i = 1;i<=n;i++) mxi[a[i].t] = max(mxi[a[i].t],i);
for(ll i = 1;i<=n;i++) a[i].l--;
for(ll i = 1;i<=n;i++) del(i);
for(ll i = 1;i<=n;i++){
if(a[i].l==0) continue;
upd(1,1,mxt,i,{a[i].t+a[i].l,i});
upd2(1,1,mxt,i,{-a[i].t+a[i].l,i});
}
for(ll i = 1;i<=n;i++) d[i] = llinf;
priority_queue<pll> pq;
for(ll i = 1;i<=n;i++){
if(a[i].l==0){
d[i] = a[i].c;
pq.push({-d[i],i});
}
}
while(sz(pq)){
pll p = pq.top();
ll i = p.sc;
pq.pop();
if(-p.fi!=d[i]) continue;
p = get(1,1,mxt,mni[a[i].t],mxt);
ll x = a[i].r+a[i].t;
while(p.fi<=x){
ll j = p.sc;
del(j);
d[j] = d[i] + a[j].c;
pq.push({-d[j],j});
p = get(1,1,mxt,mni[a[i].t],mxt);
}
p = get2(1,1,mxt,1,mxi[a[i].t]);
x = a[i].r-a[i].t;
while(p.fi<=x){
ll j = p.sc;
del(j);
d[j] = d[i] + a[j].c;
pq.push({-d[j],j});
p = get2(1,1,mxt,1,mxi[a[i].t]);
}
}
ll ans = llinf;
for(ll i = 1;i<=n;i++) if(a[i].r==m) ans = min(ans,d[i]);
if(ans==llinf) ans = -1;
cout<<ans<<endl;
}
int main(){
daj_mi_malo_vremena
int t; t = 1;
while(t--){
tc();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
158 ms |
12364 KB |
Output is correct |
2 |
Correct |
146 ms |
12448 KB |
Output is correct |
3 |
Correct |
147 ms |
13508 KB |
Output is correct |
4 |
Correct |
144 ms |
13008 KB |
Output is correct |
5 |
Correct |
155 ms |
14708 KB |
Output is correct |
6 |
Correct |
141 ms |
12620 KB |
Output is correct |
7 |
Correct |
127 ms |
12364 KB |
Output is correct |
8 |
Correct |
93 ms |
12400 KB |
Output is correct |
9 |
Correct |
92 ms |
12436 KB |
Output is correct |
10 |
Correct |
96 ms |
12380 KB |
Output is correct |
11 |
Correct |
192 ms |
14580 KB |
Output is correct |
12 |
Correct |
178 ms |
14636 KB |
Output is correct |
13 |
Correct |
182 ms |
14524 KB |
Output is correct |
14 |
Correct |
182 ms |
14596 KB |
Output is correct |
15 |
Correct |
163 ms |
12500 KB |
Output is correct |
16 |
Correct |
169 ms |
12500 KB |
Output is correct |
17 |
Correct |
168 ms |
12452 KB |
Output is correct |
18 |
Correct |
174 ms |
14536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
9 ms |
1412 KB |
Output is correct |
21 |
Correct |
9 ms |
1364 KB |
Output is correct |
22 |
Correct |
9 ms |
1124 KB |
Output is correct |
23 |
Correct |
9 ms |
1008 KB |
Output is correct |
24 |
Correct |
13 ms |
1812 KB |
Output is correct |
25 |
Correct |
12 ms |
1620 KB |
Output is correct |
26 |
Correct |
14 ms |
1620 KB |
Output is correct |
27 |
Correct |
16 ms |
1672 KB |
Output is correct |
28 |
Correct |
12 ms |
1808 KB |
Output is correct |
29 |
Correct |
14 ms |
1688 KB |
Output is correct |
30 |
Correct |
7 ms |
1644 KB |
Output is correct |
31 |
Correct |
7 ms |
1620 KB |
Output is correct |
32 |
Correct |
12 ms |
1876 KB |
Output is correct |
33 |
Correct |
13 ms |
1876 KB |
Output is correct |
34 |
Correct |
14 ms |
1468 KB |
Output is correct |
35 |
Correct |
13 ms |
1828 KB |
Output is correct |
36 |
Correct |
12 ms |
1892 KB |
Output is correct |
37 |
Correct |
13 ms |
1448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
158 ms |
12364 KB |
Output is correct |
2 |
Correct |
146 ms |
12448 KB |
Output is correct |
3 |
Correct |
147 ms |
13508 KB |
Output is correct |
4 |
Correct |
144 ms |
13008 KB |
Output is correct |
5 |
Correct |
155 ms |
14708 KB |
Output is correct |
6 |
Correct |
141 ms |
12620 KB |
Output is correct |
7 |
Correct |
127 ms |
12364 KB |
Output is correct |
8 |
Correct |
93 ms |
12400 KB |
Output is correct |
9 |
Correct |
92 ms |
12436 KB |
Output is correct |
10 |
Correct |
96 ms |
12380 KB |
Output is correct |
11 |
Correct |
192 ms |
14580 KB |
Output is correct |
12 |
Correct |
178 ms |
14636 KB |
Output is correct |
13 |
Correct |
182 ms |
14524 KB |
Output is correct |
14 |
Correct |
182 ms |
14596 KB |
Output is correct |
15 |
Correct |
163 ms |
12500 KB |
Output is correct |
16 |
Correct |
169 ms |
12500 KB |
Output is correct |
17 |
Correct |
168 ms |
12452 KB |
Output is correct |
18 |
Correct |
174 ms |
14536 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
0 ms |
340 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
0 ms |
340 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
9 ms |
1412 KB |
Output is correct |
39 |
Correct |
9 ms |
1364 KB |
Output is correct |
40 |
Correct |
9 ms |
1124 KB |
Output is correct |
41 |
Correct |
9 ms |
1008 KB |
Output is correct |
42 |
Correct |
13 ms |
1812 KB |
Output is correct |
43 |
Correct |
12 ms |
1620 KB |
Output is correct |
44 |
Correct |
14 ms |
1620 KB |
Output is correct |
45 |
Correct |
16 ms |
1672 KB |
Output is correct |
46 |
Correct |
12 ms |
1808 KB |
Output is correct |
47 |
Correct |
14 ms |
1688 KB |
Output is correct |
48 |
Correct |
7 ms |
1644 KB |
Output is correct |
49 |
Correct |
7 ms |
1620 KB |
Output is correct |
50 |
Correct |
12 ms |
1876 KB |
Output is correct |
51 |
Correct |
13 ms |
1876 KB |
Output is correct |
52 |
Correct |
14 ms |
1468 KB |
Output is correct |
53 |
Correct |
13 ms |
1828 KB |
Output is correct |
54 |
Correct |
12 ms |
1892 KB |
Output is correct |
55 |
Correct |
13 ms |
1448 KB |
Output is correct |
56 |
Correct |
253 ms |
19856 KB |
Output is correct |
57 |
Correct |
272 ms |
19332 KB |
Output is correct |
58 |
Correct |
254 ms |
18688 KB |
Output is correct |
59 |
Correct |
260 ms |
18752 KB |
Output is correct |
60 |
Correct |
241 ms |
13680 KB |
Output is correct |
61 |
Correct |
256 ms |
18708 KB |
Output is correct |
62 |
Correct |
257 ms |
19780 KB |
Output is correct |
63 |
Correct |
270 ms |
24948 KB |
Output is correct |
64 |
Correct |
271 ms |
24968 KB |
Output is correct |
65 |
Correct |
176 ms |
12488 KB |
Output is correct |
66 |
Correct |
210 ms |
13656 KB |
Output is correct |
67 |
Correct |
427 ms |
25036 KB |
Output is correct |
68 |
Correct |
390 ms |
25020 KB |
Output is correct |
69 |
Correct |
345 ms |
23908 KB |
Output is correct |
70 |
Correct |
452 ms |
25320 KB |
Output is correct |
71 |
Correct |
382 ms |
24972 KB |
Output is correct |
72 |
Correct |
346 ms |
24904 KB |
Output is correct |
73 |
Correct |
448 ms |
25196 KB |
Output is correct |
74 |
Correct |
181 ms |
24976 KB |
Output is correct |
75 |
Correct |
176 ms |
24880 KB |
Output is correct |
76 |
Correct |
418 ms |
22464 KB |
Output is correct |
77 |
Correct |
448 ms |
31204 KB |
Output is correct |
78 |
Correct |
418 ms |
25916 KB |
Output is correct |
79 |
Correct |
442 ms |
28804 KB |
Output is correct |
80 |
Correct |
444 ms |
23936 KB |
Output is correct |
81 |
Correct |
180 ms |
28836 KB |
Output is correct |
82 |
Correct |
408 ms |
23320 KB |
Output is correct |
83 |
Correct |
413 ms |
28032 KB |
Output is correct |
84 |
Correct |
429 ms |
28144 KB |
Output is correct |
85 |
Correct |
291 ms |
28836 KB |
Output is correct |
86 |
Correct |
311 ms |
28908 KB |
Output is correct |
87 |
Correct |
428 ms |
28844 KB |
Output is correct |
88 |
Correct |
338 ms |
28800 KB |
Output is correct |
89 |
Correct |
331 ms |
28920 KB |
Output is correct |
90 |
Correct |
456 ms |
31024 KB |
Output is correct |
91 |
Correct |
382 ms |
29976 KB |
Output is correct |
92 |
Correct |
320 ms |
28776 KB |
Output is correct |
93 |
Correct |
442 ms |
28832 KB |
Output is correct |
94 |
Correct |
410 ms |
29052 KB |
Output is correct |
95 |
Correct |
411 ms |
28836 KB |
Output is correct |
96 |
Correct |
465 ms |
31100 KB |
Output is correct |
97 |
Correct |
461 ms |
30932 KB |
Output is correct |
98 |
Correct |
443 ms |
31040 KB |
Output is correct |
99 |
Correct |
457 ms |
30976 KB |
Output is correct |
100 |
Correct |
416 ms |
30908 KB |
Output is correct |
101 |
Correct |
432 ms |
30960 KB |
Output is correct |
102 |
Correct |
453 ms |
31036 KB |
Output is correct |
103 |
Correct |
303 ms |
29132 KB |
Output is correct |