#include "bits/stdc++.h"
#define here cerr<<"===========================================\n"
#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 |
233 ms |
12436 KB |
Output is correct |
2 |
Correct |
135 ms |
12440 KB |
Output is correct |
3 |
Correct |
164 ms |
13644 KB |
Output is correct |
4 |
Correct |
150 ms |
13040 KB |
Output is correct |
5 |
Correct |
155 ms |
14536 KB |
Output is correct |
6 |
Correct |
157 ms |
12588 KB |
Output is correct |
7 |
Correct |
140 ms |
12400 KB |
Output is correct |
8 |
Correct |
92 ms |
12384 KB |
Output is correct |
9 |
Correct |
96 ms |
12364 KB |
Output is correct |
10 |
Correct |
134 ms |
12364 KB |
Output is correct |
11 |
Correct |
179 ms |
14532 KB |
Output is correct |
12 |
Correct |
202 ms |
14644 KB |
Output is correct |
13 |
Correct |
260 ms |
14600 KB |
Output is correct |
14 |
Correct |
187 ms |
14616 KB |
Output is correct |
15 |
Correct |
242 ms |
12500 KB |
Output is correct |
16 |
Correct |
181 ms |
12492 KB |
Output is correct |
17 |
Correct |
175 ms |
12452 KB |
Output is correct |
18 |
Correct |
192 ms |
14544 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 |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
10 ms |
1364 KB |
Output is correct |
21 |
Correct |
9 ms |
1480 KB |
Output is correct |
22 |
Correct |
9 ms |
1120 KB |
Output is correct |
23 |
Correct |
8 ms |
1128 KB |
Output is correct |
24 |
Correct |
12 ms |
1748 KB |
Output is correct |
25 |
Correct |
16 ms |
1648 KB |
Output is correct |
26 |
Correct |
13 ms |
1620 KB |
Output is correct |
27 |
Correct |
22 ms |
1676 KB |
Output is correct |
28 |
Correct |
12 ms |
1816 KB |
Output is correct |
29 |
Correct |
12 ms |
1620 KB |
Output is correct |
30 |
Correct |
7 ms |
1620 KB |
Output is correct |
31 |
Correct |
7 ms |
1620 KB |
Output is correct |
32 |
Correct |
12 ms |
1896 KB |
Output is correct |
33 |
Correct |
13 ms |
1876 KB |
Output is correct |
34 |
Correct |
12 ms |
1416 KB |
Output is correct |
35 |
Correct |
12 ms |
1876 KB |
Output is correct |
36 |
Correct |
12 ms |
1876 KB |
Output is correct |
37 |
Correct |
12 ms |
1416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
233 ms |
12436 KB |
Output is correct |
2 |
Correct |
135 ms |
12440 KB |
Output is correct |
3 |
Correct |
164 ms |
13644 KB |
Output is correct |
4 |
Correct |
150 ms |
13040 KB |
Output is correct |
5 |
Correct |
155 ms |
14536 KB |
Output is correct |
6 |
Correct |
157 ms |
12588 KB |
Output is correct |
7 |
Correct |
140 ms |
12400 KB |
Output is correct |
8 |
Correct |
92 ms |
12384 KB |
Output is correct |
9 |
Correct |
96 ms |
12364 KB |
Output is correct |
10 |
Correct |
134 ms |
12364 KB |
Output is correct |
11 |
Correct |
179 ms |
14532 KB |
Output is correct |
12 |
Correct |
202 ms |
14644 KB |
Output is correct |
13 |
Correct |
260 ms |
14600 KB |
Output is correct |
14 |
Correct |
187 ms |
14616 KB |
Output is correct |
15 |
Correct |
242 ms |
12500 KB |
Output is correct |
16 |
Correct |
181 ms |
12492 KB |
Output is correct |
17 |
Correct |
175 ms |
12452 KB |
Output is correct |
18 |
Correct |
192 ms |
14544 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 |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
10 ms |
1364 KB |
Output is correct |
39 |
Correct |
9 ms |
1480 KB |
Output is correct |
40 |
Correct |
9 ms |
1120 KB |
Output is correct |
41 |
Correct |
8 ms |
1128 KB |
Output is correct |
42 |
Correct |
12 ms |
1748 KB |
Output is correct |
43 |
Correct |
16 ms |
1648 KB |
Output is correct |
44 |
Correct |
13 ms |
1620 KB |
Output is correct |
45 |
Correct |
22 ms |
1676 KB |
Output is correct |
46 |
Correct |
12 ms |
1816 KB |
Output is correct |
47 |
Correct |
12 ms |
1620 KB |
Output is correct |
48 |
Correct |
7 ms |
1620 KB |
Output is correct |
49 |
Correct |
7 ms |
1620 KB |
Output is correct |
50 |
Correct |
12 ms |
1896 KB |
Output is correct |
51 |
Correct |
13 ms |
1876 KB |
Output is correct |
52 |
Correct |
12 ms |
1416 KB |
Output is correct |
53 |
Correct |
12 ms |
1876 KB |
Output is correct |
54 |
Correct |
12 ms |
1876 KB |
Output is correct |
55 |
Correct |
12 ms |
1416 KB |
Output is correct |
56 |
Correct |
267 ms |
19836 KB |
Output is correct |
57 |
Correct |
248 ms |
19248 KB |
Output is correct |
58 |
Correct |
253 ms |
18728 KB |
Output is correct |
59 |
Correct |
250 ms |
18672 KB |
Output is correct |
60 |
Correct |
232 ms |
13644 KB |
Output is correct |
61 |
Correct |
264 ms |
18800 KB |
Output is correct |
62 |
Correct |
239 ms |
19804 KB |
Output is correct |
63 |
Correct |
257 ms |
24940 KB |
Output is correct |
64 |
Correct |
262 ms |
25036 KB |
Output is correct |
65 |
Correct |
163 ms |
12436 KB |
Output is correct |
66 |
Correct |
205 ms |
13604 KB |
Output is correct |
67 |
Correct |
423 ms |
25068 KB |
Output is correct |
68 |
Correct |
370 ms |
24908 KB |
Output is correct |
69 |
Correct |
333 ms |
23956 KB |
Output is correct |
70 |
Correct |
430 ms |
25164 KB |
Output is correct |
71 |
Correct |
393 ms |
24964 KB |
Output is correct |
72 |
Correct |
354 ms |
24932 KB |
Output is correct |
73 |
Correct |
438 ms |
25108 KB |
Output is correct |
74 |
Correct |
166 ms |
24964 KB |
Output is correct |
75 |
Correct |
166 ms |
24932 KB |
Output is correct |
76 |
Correct |
415 ms |
22484 KB |
Output is correct |
77 |
Correct |
467 ms |
27084 KB |
Output is correct |
78 |
Correct |
407 ms |
22504 KB |
Output is correct |
79 |
Correct |
412 ms |
25044 KB |
Output is correct |
80 |
Correct |
412 ms |
20488 KB |
Output is correct |
81 |
Correct |
169 ms |
24936 KB |
Output is correct |
82 |
Correct |
376 ms |
20576 KB |
Output is correct |
83 |
Correct |
444 ms |
25068 KB |
Output is correct |
84 |
Correct |
405 ms |
25000 KB |
Output is correct |
85 |
Correct |
287 ms |
24952 KB |
Output is correct |
86 |
Correct |
285 ms |
24928 KB |
Output is correct |
87 |
Correct |
311 ms |
25032 KB |
Output is correct |
88 |
Correct |
315 ms |
24808 KB |
Output is correct |
89 |
Correct |
300 ms |
25000 KB |
Output is correct |
90 |
Correct |
433 ms |
27108 KB |
Output is correct |
91 |
Correct |
370 ms |
26052 KB |
Output is correct |
92 |
Correct |
318 ms |
24904 KB |
Output is correct |
93 |
Correct |
406 ms |
25048 KB |
Output is correct |
94 |
Correct |
389 ms |
25076 KB |
Output is correct |
95 |
Correct |
370 ms |
25080 KB |
Output is correct |
96 |
Correct |
430 ms |
27196 KB |
Output is correct |
97 |
Correct |
446 ms |
27148 KB |
Output is correct |
98 |
Correct |
423 ms |
27116 KB |
Output is correct |
99 |
Correct |
451 ms |
27264 KB |
Output is correct |
100 |
Correct |
419 ms |
27072 KB |
Output is correct |
101 |
Correct |
436 ms |
27052 KB |
Output is correct |
102 |
Correct |
463 ms |
27108 KB |
Output is correct |
103 |
Correct |
280 ms |
25304 KB |
Output is correct |