// Judges with GCC >= 12 only needs Ofast
// #pragma GCC optimize("O3,no-stack-protector,fast-math,unroll-loops,tree-vectorize")
// MLE optimization
// #pragma GCC optimize("conserve-stack")
// Old judges
// #pragma GCC target("sse4.2,popcnt,lzcnt,abm,mmx,fma,bmi,bmi2")
// New judges. Test with assert(__builtin_cpu_supports("avx2"));
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native")
// Atcoder
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma")
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
- insert(x),erase(x)
- find_by_order(k): return iterator to the k-th smallest element
- order_of_key(x): the number of elements that are strictly smaller
*/
#include<bits/stdc++.h>
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
uniform_real_distribution<> pp(0.0,1.0);
//#define int long long
#define ld long double
#define pii pair<int,int>
#define piii pair<int,pii>
#define mpp make_pair
#define fi first
#define se second
const long long inf=1e18;
const int mod=1e9+7;
const int maxn=100005;
const int bl=650;
const int maxs=655;
const int maxm=200005;
const int maxq=500005;
const int maxl=20;
const int maxa=250000;
const int root=3;
/*
int power(int a,int n){
int res=1;
while(n){
if(n&1) res=res*a%mod;
a=a*a%mod;n>>=1;
}
return res;
}
const int iroot=power(3,mod-2);
*/
const int base=101;
struct treat{
int t,l,r,c;
bool operator<(treat o){return t<o.t;}
};
int cur,cc[maxn];
vector<vector<int>> edge;
struct BIT{
int n;
vector<int> bit;
void init(int N){n=N;bit.assign(n+1,0);}
void update(int x,int val){
for(int i=x;i<=n;i+=(i&(-i))){
if(bit[i]) edge.push_back({bit[i],val}),bit[i]=cur++;
else bit[i]=val;
}
}
void query(int x,int val){
for(int i=x;i>=1;i-=(i&(-i))) if(bit[i]) edge[val].push_back(bit[i]);
}
};
treat S[maxn];
void solve(){
int n,m;cin >> n >> m;
cur=m+1;edge.resize(cur);
for(int i=1;i<=m;i++) cin >> S[i].t >> S[i].l >> S[i].r >> S[i].c,S[i].l--;
sort(S+1,S+m+1);
BIT ss;ss.init(m);
vector<pii> com;
auto get = [&](int x,int y){return upper_bound(com.begin(),com.end(),mpp(x,y))-com.begin();};
for(int i=1;i<=m;i++) com.push_back({S[i].l-S[i].t,i});
sort(com.begin(),com.end());
for(int i=1;i<=m;i++){
int p=get(S[i].r-S[i].t,m),q=get(S[i].l-S[i].t,i);
ss.query(p,i);ss.update(q,i);
}
ss.init(m);com.clear();
for(int i=1;i<=m;i++) com.push_back({S[i].l+S[i].t,i});
sort(com.begin(),com.end());
for(int i=m;i>=1;i--){
int p=get(S[i].r+S[i].t,m),q=get(S[i].l+S[i].t,i);
ss.query(p,i);ss.update(q,i);
}
vector<long long> dist(cur,inf);
priority_queue<pair<long long,int>,vector<pair<long long,int>>,greater<pair<long long,int>>> pq;
for(int i=1;i<=m;i++) if(S[i].l==0) pq.push({dist[i]=S[i].c,i});
while(!pq.empty()){
auto [d,u]=pq.top();pq.pop();
if(u<=m && S[u].r==n){
cout << d << '\n';
return;
}
if(dist[u]!=d) continue;
for(int v:edge[u]){
long long nd=d+(v<=m?S[v].c:0);
if(nd<dist[v]) pq.push({dist[v]=nd,v});
}
}
cout << -1 << '\n';
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
int test=1;//cin >> test;
while(test--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
528 ms |
138752 KB |
Output is correct |
2 |
Correct |
353 ms |
138712 KB |
Output is correct |
3 |
Correct |
487 ms |
137400 KB |
Output is correct |
4 |
Correct |
502 ms |
137224 KB |
Output is correct |
5 |
Correct |
405 ms |
135448 KB |
Output is correct |
6 |
Correct |
443 ms |
136908 KB |
Output is correct |
7 |
Correct |
388 ms |
138152 KB |
Output is correct |
8 |
Correct |
216 ms |
135400 KB |
Output is correct |
9 |
Correct |
218 ms |
136968 KB |
Output is correct |
10 |
Correct |
212 ms |
138028 KB |
Output is correct |
11 |
Correct |
266 ms |
138288 KB |
Output is correct |
12 |
Correct |
300 ms |
138308 KB |
Output is correct |
13 |
Correct |
704 ms |
138664 KB |
Output is correct |
14 |
Correct |
702 ms |
138672 KB |
Output is correct |
15 |
Correct |
638 ms |
138676 KB |
Output is correct |
16 |
Correct |
649 ms |
138800 KB |
Output is correct |
17 |
Correct |
613 ms |
137904 KB |
Output is correct |
18 |
Correct |
269 ms |
137528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
10 ms |
4836 KB |
Output is correct |
21 |
Correct |
10 ms |
4836 KB |
Output is correct |
22 |
Correct |
9 ms |
4636 KB |
Output is correct |
23 |
Correct |
10 ms |
4580 KB |
Output is correct |
24 |
Correct |
11 ms |
4836 KB |
Output is correct |
25 |
Correct |
17 ms |
4716 KB |
Output is correct |
26 |
Correct |
11 ms |
4736 KB |
Output is correct |
27 |
Correct |
12 ms |
4688 KB |
Output is correct |
28 |
Correct |
11 ms |
4804 KB |
Output is correct |
29 |
Correct |
16 ms |
4740 KB |
Output is correct |
30 |
Correct |
8 ms |
4708 KB |
Output is correct |
31 |
Correct |
9 ms |
4676 KB |
Output is correct |
32 |
Correct |
10 ms |
4964 KB |
Output is correct |
33 |
Correct |
9 ms |
4948 KB |
Output is correct |
34 |
Correct |
13 ms |
4756 KB |
Output is correct |
35 |
Correct |
9 ms |
4836 KB |
Output is correct |
36 |
Correct |
9 ms |
4952 KB |
Output is correct |
37 |
Correct |
13 ms |
4660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
528 ms |
138752 KB |
Output is correct |
2 |
Correct |
353 ms |
138712 KB |
Output is correct |
3 |
Correct |
487 ms |
137400 KB |
Output is correct |
4 |
Correct |
502 ms |
137224 KB |
Output is correct |
5 |
Correct |
405 ms |
135448 KB |
Output is correct |
6 |
Correct |
443 ms |
136908 KB |
Output is correct |
7 |
Correct |
388 ms |
138152 KB |
Output is correct |
8 |
Correct |
216 ms |
135400 KB |
Output is correct |
9 |
Correct |
218 ms |
136968 KB |
Output is correct |
10 |
Correct |
212 ms |
138028 KB |
Output is correct |
11 |
Correct |
266 ms |
138288 KB |
Output is correct |
12 |
Correct |
300 ms |
138308 KB |
Output is correct |
13 |
Correct |
704 ms |
138664 KB |
Output is correct |
14 |
Correct |
702 ms |
138672 KB |
Output is correct |
15 |
Correct |
638 ms |
138676 KB |
Output is correct |
16 |
Correct |
649 ms |
138800 KB |
Output is correct |
17 |
Correct |
613 ms |
137904 KB |
Output is correct |
18 |
Correct |
269 ms |
137528 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
10 ms |
4836 KB |
Output is correct |
39 |
Correct |
10 ms |
4836 KB |
Output is correct |
40 |
Correct |
9 ms |
4636 KB |
Output is correct |
41 |
Correct |
10 ms |
4580 KB |
Output is correct |
42 |
Correct |
11 ms |
4836 KB |
Output is correct |
43 |
Correct |
17 ms |
4716 KB |
Output is correct |
44 |
Correct |
11 ms |
4736 KB |
Output is correct |
45 |
Correct |
12 ms |
4688 KB |
Output is correct |
46 |
Correct |
11 ms |
4804 KB |
Output is correct |
47 |
Correct |
16 ms |
4740 KB |
Output is correct |
48 |
Correct |
8 ms |
4708 KB |
Output is correct |
49 |
Correct |
9 ms |
4676 KB |
Output is correct |
50 |
Correct |
10 ms |
4964 KB |
Output is correct |
51 |
Correct |
9 ms |
4948 KB |
Output is correct |
52 |
Correct |
13 ms |
4756 KB |
Output is correct |
53 |
Correct |
9 ms |
4836 KB |
Output is correct |
54 |
Correct |
9 ms |
4952 KB |
Output is correct |
55 |
Correct |
13 ms |
4660 KB |
Output is correct |
56 |
Correct |
418 ms |
137892 KB |
Output is correct |
57 |
Correct |
386 ms |
138060 KB |
Output is correct |
58 |
Correct |
358 ms |
136016 KB |
Output is correct |
59 |
Correct |
379 ms |
136412 KB |
Output is correct |
60 |
Correct |
317 ms |
133700 KB |
Output is correct |
61 |
Correct |
358 ms |
135924 KB |
Output is correct |
62 |
Correct |
373 ms |
137928 KB |
Output is correct |
63 |
Correct |
188 ms |
131536 KB |
Output is correct |
64 |
Correct |
188 ms |
131496 KB |
Output is correct |
65 |
Correct |
511 ms |
138724 KB |
Output is correct |
66 |
Correct |
280 ms |
133684 KB |
Output is correct |
67 |
Correct |
698 ms |
139500 KB |
Output is correct |
68 |
Correct |
555 ms |
139376 KB |
Output is correct |
69 |
Correct |
462 ms |
139264 KB |
Output is correct |
70 |
Correct |
735 ms |
139568 KB |
Output is correct |
71 |
Correct |
614 ms |
139508 KB |
Output is correct |
72 |
Correct |
518 ms |
139188 KB |
Output is correct |
73 |
Correct |
712 ms |
139340 KB |
Output is correct |
74 |
Correct |
234 ms |
139164 KB |
Output is correct |
75 |
Correct |
233 ms |
138556 KB |
Output is correct |
76 |
Correct |
296 ms |
139292 KB |
Output is correct |
77 |
Correct |
287 ms |
140816 KB |
Output is correct |
78 |
Correct |
811 ms |
138992 KB |
Output is correct |
79 |
Correct |
754 ms |
139304 KB |
Output is correct |
80 |
Correct |
673 ms |
139060 KB |
Output is correct |
81 |
Correct |
253 ms |
139272 KB |
Output is correct |
82 |
Correct |
743 ms |
138244 KB |
Output is correct |
83 |
Correct |
649 ms |
138456 KB |
Output is correct |
84 |
Correct |
692 ms |
138512 KB |
Output is correct |
85 |
Correct |
401 ms |
139316 KB |
Output is correct |
86 |
Correct |
427 ms |
139224 KB |
Output is correct |
87 |
Correct |
431 ms |
139260 KB |
Output is correct |
88 |
Correct |
427 ms |
138028 KB |
Output is correct |
89 |
Correct |
427 ms |
138968 KB |
Output is correct |
90 |
Correct |
362 ms |
139476 KB |
Output is correct |
91 |
Correct |
245 ms |
139320 KB |
Output is correct |
92 |
Correct |
455 ms |
139336 KB |
Output is correct |
93 |
Correct |
580 ms |
139308 KB |
Output is correct |
94 |
Correct |
552 ms |
138092 KB |
Output is correct |
95 |
Correct |
483 ms |
138912 KB |
Output is correct |
96 |
Correct |
279 ms |
139440 KB |
Output is correct |
97 |
Correct |
395 ms |
140092 KB |
Output is correct |
98 |
Correct |
417 ms |
140208 KB |
Output is correct |
99 |
Correct |
362 ms |
140144 KB |
Output is correct |
100 |
Correct |
353 ms |
136488 KB |
Output is correct |
101 |
Correct |
720 ms |
139956 KB |
Output is correct |
102 |
Correct |
301 ms |
139992 KB |
Output is correct |
103 |
Correct |
382 ms |
139228 KB |
Output is correct |