#define here cerr<<"===========================================\n"
#define dbg(x) cerr<<#x<<": "<<x<<endl;
#include "bits/stdc++.h"
//#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 1000005
ll n,m;
struct tupl{
ll t,l,r,c;
} a[maxn];
ll tsz = 1,tsz2 = 1;
ll ls[maxx],rs[maxx];
ll ls2[maxx],rs2[maxx];
set<pll> t[maxx],t2[maxn];
void upd(ll v,ll tl,ll tr,ll i,pll p,bool f){
if(f) t[v].insert(p);
else t[v].erase(p);
if(tl==tr) return;
ll mid = (tl+tr)/2;
if(i<=mid){
if(!ls[v]) ls[v] = ++tsz;
upd(ls[v],tl,mid,i,p,f);
}else{
if(!rs[v]) rs[v] = ++tsz;
upd(rs[v],mid+1,tr,i,p,f);
}
}
void upd2(ll v,ll tl,ll tr,ll i,pll p,bool f){
if(f) t2[v].insert(p);
else t2[v].erase(p);
if(tl==tr) return;
ll mid = (tl+tr)/2;
if(i<=mid){
if(!ls2[v]) ls2[v] = ++tsz2;
upd2(ls2[v],tl,mid,i,p,f);
}else{
if(!rs2[v]) rs2[v] = ++tsz2;
upd2(rs2[v],mid+1,tr,i,p,f);
}
}
set<ll> s;
void get(ll v,ll tl,ll tr,ll l,ll r,ll x){
if(l>r) return;
if(tl==l&&tr==r){
auto it = t[v].begin();
while(it!=t[v].end()&&(*it).fi<=x){
s.insert((*it).sc);
it++;
}
return;
}
ll mid = (tl+tr)/2;
if(ls[v]) get(ls[v],tl,mid,l,min(mid,r),x);
if(rs[v]) get(rs[v],mid+1,tr,max(mid+1,l),r,x);
}
void get2(ll v,ll tl,ll tr,ll l,ll r,ll x){
if(l>r) return;
if(tl==l&&tr==r){
auto it = t2[v].begin();
while(it!=t2[v].end()&&(*it).fi<=x){
s.insert((*it).sc);
it++;
}
return;
}
ll mid = (tl+tr)/2;
if(ls2[v]) get2(ls2[v],tl,mid,l,min(mid,r),x);
if(rs2[v]) get2(rs2[v],mid+1,tr,max(mid+1,l),r,x);
}
ll d[maxn];
void tc(){
cin >> m >> n;
for(ll i = 1;i<=n;i++) cin >> a[i].t >> a[i].l >> a[i].r >> a[i].c;
for(ll i = 1;i<=n;i++) a[i].l--;
ll mxt = 0;
for(ll i = 1;i<=n;i++) mxt = max(mxt,a[i].t);
for(ll i = 1;i<=n;i++){
upd(1,1,mxt,a[i].t,{a[i].l+a[i].t,i},1);
upd2(1,1,mxt,a[i].t,{a[i].l-a[i].t,i},1);
}
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});
upd(1,1,mxt,a[i].t,{a[i].l+a[i].t,i},0);
upd2(1,1,mxt,a[i].t,{a[i].l-a[i].t,i},0);
}
}
while(sz(pq)){
pll p = pq.top();
ll i = p.sc;
pq.pop();
if(-p.fi!=d[i]) continue;
s.clear();
get(1,1,mxt,a[i].t,mxt,a[i].r+a[i].t);
get2(1,1,mxt,1,a[i].t,a[i].r-a[i].t);
for(ll j : s){
if(j==i) continue;
d[j] = d[i] + a[j].c;
upd(1,1,mxt,a[j].t,{a[j].l+a[j].t,j},0);
upd2(1,1,mxt,a[j].t,{a[j].l-a[j].t,j},0);
pq.push({-d[j],j});
}
}
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
139 ms |
68544 KB |
Output is correct |
2 |
Correct |
136 ms |
68524 KB |
Output is correct |
3 |
Correct |
150 ms |
69596 KB |
Output is correct |
4 |
Correct |
154 ms |
69616 KB |
Output is correct |
5 |
Correct |
153 ms |
70672 KB |
Output is correct |
6 |
Correct |
134 ms |
68656 KB |
Output is correct |
7 |
Correct |
114 ms |
68504 KB |
Output is correct |
8 |
Correct |
108 ms |
68476 KB |
Output is correct |
9 |
Correct |
101 ms |
68512 KB |
Output is correct |
10 |
Correct |
106 ms |
68444 KB |
Output is correct |
11 |
Correct |
262 ms |
74000 KB |
Output is correct |
12 |
Correct |
278 ms |
71796 KB |
Output is correct |
13 |
Correct |
203 ms |
70760 KB |
Output is correct |
14 |
Correct |
204 ms |
70644 KB |
Output is correct |
15 |
Correct |
203 ms |
68600 KB |
Output is correct |
16 |
Correct |
183 ms |
68484 KB |
Output is correct |
17 |
Correct |
187 ms |
68552 KB |
Output is correct |
18 |
Correct |
313 ms |
73064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
51992 KB |
Output is correct |
2 |
Correct |
28 ms |
52044 KB |
Output is correct |
3 |
Correct |
24 ms |
52028 KB |
Output is correct |
4 |
Correct |
24 ms |
52044 KB |
Output is correct |
5 |
Correct |
25 ms |
52052 KB |
Output is correct |
6 |
Correct |
26 ms |
52068 KB |
Output is correct |
7 |
Correct |
24 ms |
52052 KB |
Output is correct |
8 |
Correct |
25 ms |
52044 KB |
Output is correct |
9 |
Correct |
29 ms |
52052 KB |
Output is correct |
10 |
Correct |
26 ms |
52044 KB |
Output is correct |
11 |
Correct |
31 ms |
52024 KB |
Output is correct |
12 |
Correct |
25 ms |
52020 KB |
Output is correct |
13 |
Correct |
23 ms |
52176 KB |
Output is correct |
14 |
Correct |
23 ms |
52100 KB |
Output is correct |
15 |
Correct |
25 ms |
51996 KB |
Output is correct |
16 |
Correct |
26 ms |
51928 KB |
Output is correct |
17 |
Correct |
25 ms |
51988 KB |
Output is correct |
18 |
Correct |
25 ms |
52000 KB |
Output is correct |
19 |
Correct |
30 ms |
51924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
51992 KB |
Output is correct |
2 |
Correct |
28 ms |
52044 KB |
Output is correct |
3 |
Correct |
24 ms |
52028 KB |
Output is correct |
4 |
Correct |
24 ms |
52044 KB |
Output is correct |
5 |
Correct |
25 ms |
52052 KB |
Output is correct |
6 |
Correct |
26 ms |
52068 KB |
Output is correct |
7 |
Correct |
24 ms |
52052 KB |
Output is correct |
8 |
Correct |
25 ms |
52044 KB |
Output is correct |
9 |
Correct |
29 ms |
52052 KB |
Output is correct |
10 |
Correct |
26 ms |
52044 KB |
Output is correct |
11 |
Correct |
31 ms |
52024 KB |
Output is correct |
12 |
Correct |
25 ms |
52020 KB |
Output is correct |
13 |
Correct |
23 ms |
52176 KB |
Output is correct |
14 |
Correct |
23 ms |
52100 KB |
Output is correct |
15 |
Correct |
25 ms |
51996 KB |
Output is correct |
16 |
Correct |
26 ms |
51928 KB |
Output is correct |
17 |
Correct |
25 ms |
51988 KB |
Output is correct |
18 |
Correct |
25 ms |
52000 KB |
Output is correct |
19 |
Correct |
30 ms |
51924 KB |
Output is correct |
20 |
Correct |
67 ms |
60232 KB |
Output is correct |
21 |
Correct |
64 ms |
60268 KB |
Output is correct |
22 |
Correct |
91 ms |
72012 KB |
Output is correct |
23 |
Correct |
95 ms |
72060 KB |
Output is correct |
24 |
Correct |
122 ms |
74740 KB |
Output is correct |
25 |
Correct |
107 ms |
74168 KB |
Output is correct |
26 |
Correct |
95 ms |
73372 KB |
Output is correct |
27 |
Correct |
88 ms |
72552 KB |
Output is correct |
28 |
Correct |
120 ms |
74700 KB |
Output is correct |
29 |
Correct |
93 ms |
74124 KB |
Output is correct |
30 |
Correct |
78 ms |
73140 KB |
Output is correct |
31 |
Correct |
88 ms |
72440 KB |
Output is correct |
32 |
Correct |
130 ms |
74704 KB |
Output is correct |
33 |
Correct |
101 ms |
66892 KB |
Output is correct |
34 |
Correct |
69 ms |
60952 KB |
Output is correct |
35 |
Correct |
117 ms |
74736 KB |
Output is correct |
36 |
Correct |
111 ms |
67040 KB |
Output is correct |
37 |
Correct |
78 ms |
60876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
139 ms |
68544 KB |
Output is correct |
2 |
Correct |
136 ms |
68524 KB |
Output is correct |
3 |
Correct |
150 ms |
69596 KB |
Output is correct |
4 |
Correct |
154 ms |
69616 KB |
Output is correct |
5 |
Correct |
153 ms |
70672 KB |
Output is correct |
6 |
Correct |
134 ms |
68656 KB |
Output is correct |
7 |
Correct |
114 ms |
68504 KB |
Output is correct |
8 |
Correct |
108 ms |
68476 KB |
Output is correct |
9 |
Correct |
101 ms |
68512 KB |
Output is correct |
10 |
Correct |
106 ms |
68444 KB |
Output is correct |
11 |
Correct |
262 ms |
74000 KB |
Output is correct |
12 |
Correct |
278 ms |
71796 KB |
Output is correct |
13 |
Correct |
203 ms |
70760 KB |
Output is correct |
14 |
Correct |
204 ms |
70644 KB |
Output is correct |
15 |
Correct |
203 ms |
68600 KB |
Output is correct |
16 |
Correct |
183 ms |
68484 KB |
Output is correct |
17 |
Correct |
187 ms |
68552 KB |
Output is correct |
18 |
Correct |
313 ms |
73064 KB |
Output is correct |
19 |
Correct |
26 ms |
51992 KB |
Output is correct |
20 |
Correct |
28 ms |
52044 KB |
Output is correct |
21 |
Correct |
24 ms |
52028 KB |
Output is correct |
22 |
Correct |
24 ms |
52044 KB |
Output is correct |
23 |
Correct |
25 ms |
52052 KB |
Output is correct |
24 |
Correct |
26 ms |
52068 KB |
Output is correct |
25 |
Correct |
24 ms |
52052 KB |
Output is correct |
26 |
Correct |
25 ms |
52044 KB |
Output is correct |
27 |
Correct |
29 ms |
52052 KB |
Output is correct |
28 |
Correct |
26 ms |
52044 KB |
Output is correct |
29 |
Correct |
31 ms |
52024 KB |
Output is correct |
30 |
Correct |
25 ms |
52020 KB |
Output is correct |
31 |
Correct |
23 ms |
52176 KB |
Output is correct |
32 |
Correct |
23 ms |
52100 KB |
Output is correct |
33 |
Correct |
25 ms |
51996 KB |
Output is correct |
34 |
Correct |
26 ms |
51928 KB |
Output is correct |
35 |
Correct |
25 ms |
51988 KB |
Output is correct |
36 |
Correct |
25 ms |
52000 KB |
Output is correct |
37 |
Correct |
30 ms |
51924 KB |
Output is correct |
38 |
Correct |
67 ms |
60232 KB |
Output is correct |
39 |
Correct |
64 ms |
60268 KB |
Output is correct |
40 |
Correct |
91 ms |
72012 KB |
Output is correct |
41 |
Correct |
95 ms |
72060 KB |
Output is correct |
42 |
Correct |
122 ms |
74740 KB |
Output is correct |
43 |
Correct |
107 ms |
74168 KB |
Output is correct |
44 |
Correct |
95 ms |
73372 KB |
Output is correct |
45 |
Correct |
88 ms |
72552 KB |
Output is correct |
46 |
Correct |
120 ms |
74700 KB |
Output is correct |
47 |
Correct |
93 ms |
74124 KB |
Output is correct |
48 |
Correct |
78 ms |
73140 KB |
Output is correct |
49 |
Correct |
88 ms |
72440 KB |
Output is correct |
50 |
Correct |
130 ms |
74704 KB |
Output is correct |
51 |
Correct |
101 ms |
66892 KB |
Output is correct |
52 |
Correct |
69 ms |
60952 KB |
Output is correct |
53 |
Correct |
117 ms |
74736 KB |
Output is correct |
54 |
Correct |
111 ms |
67040 KB |
Output is correct |
55 |
Correct |
78 ms |
60876 KB |
Output is correct |
56 |
Correct |
2156 ms |
269324 KB |
Output is correct |
57 |
Runtime error |
2342 ms |
524288 KB |
Execution killed with signal 11 |
58 |
Halted |
0 ms |
0 KB |
- |