#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define int long long
typedef pair<int,int> pii;
#define mp make_pair
#define eb emplace_back
const int mxm=200005,inf=1e17;
int t[mxm],l[mxm],r[mxm],c[mxm];
vector<int> adj[mxm];
int dist[mxm];bool vis[mxm];
vector<pii> adjl[mxm];
void main1(int n,int m){
vector<pair<int,pii> >disc;int nex=1;
disc.eb(1,mp(-1,-1));
disc.eb(n+1,mp(-1,-1));
for(int i=0;i<m;i++){
cin>>t[i]>>l[i]>>r[i]>>c[i];
disc.eb(l[i],mp(i,0));//edge from l[i] to r[i]+1
disc.eb(r[i]+1,mp(i,1));
}
sort(disc.begin(),disc.end());
for(int i=0;i<disc.size();i++){
if(i>0 && disc[i].f>disc[i-1].f)nex++;
if(disc[i].s.f==-1)continue;
if(disc[i].s.s==0){
l[disc[i].s.f]=nex;
}else r[disc[i].s.f]=nex;
}
for(int i=0;i<m;i++){
adjl[l[i]].eb(r[i],c[i]);
}
for(int i=1;i<=nex;i++)dist[i]=inf;
for(int i=2;i<=nex;i++)adjl[i].eb(i-1,0);
priority_queue<pii,vector<pii>,greater<pii> > pq;
int a;
pq.emplace(1,0);dist[1]=0;
while(!pq.empty()){
a=pq.top().f;
if(dist[a]<pq.top().s){
pq.pop();continue;
}
pq.pop();
for(pii x:adjl[a]){
if(dist[x.f]>dist[a]+x.s){
dist[x.f]=dist[a]+x.s;
pq.emplace(x.f,dist[x.f]);
}
}
}
if(dist[nex]==inf)cout<<"-1\n";
else cout<<dist[nex]<<'\n';
}
int32_t main(){
ios_base::sync_with_stdio(false);cin.tie(0);
int n,m;
cin>>n>>m;
//funny how subtasks 1-3 can be done with dijkstra, and ST4 is... 2d segtree with lazy nodes? idk
if(m>5000){
main1(n,m);
return 0;
}
for(int i=0;i<m;i++){
cin>>t[i]>>l[i]>>r[i]>>c[i];
if(l[i]==1)adj[m].push_back(i);
if(r[i]==n)adj[i].push_back(m+1);
}c[m]=c[m+1]=0;
for(int i=0;i<m;i++){
for(int j=i+1;j<m;j++){
//cout<<"Iteration "<<i<<' '<<j<<'\n';
if(t[i]>t[j])swap(i,j);//ensures t[j]>=t[i]
int a,b;
a=l[i]+t[j]-t[i];
b=r[i]-t[j]+t[i];
if(l[i]==1)a=1;
if(r[i]==n)b=n;
//compare range [a,b] with [l[j],r[j]]
b++;r[j]++;
if(b>=l[j])adj[i].push_back(j);
if(r[j]>=a)adj[j].push_back(i);
b--;r[j]--;
if(i>j)swap(i,j);
}
}
for(int i=0;i<m+2;i++)dist[i]=inf;
dist[m]=0;
while(true){
int a=-1;
for(int i=0;i<m+2;i++){
if(vis[i])continue;
if(a==-1 || dist[i]<dist[a])a=i;
}
if(a==-1)break;
vis[a]=true;
for(int x:adj[a]){
dist[x]=min(dist[x],dist[a]+c[x]);
}
}
if(dist[m+1]==inf)cout<<"-1\n";
else cout<<dist[m+1]<<'\n';
}
Compilation message
treatment.cpp: In function 'void main1(long long int, long long int)':
treatment.cpp:27:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for(int i=0;i<disc.size();i++){
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
23844 KB |
Output is correct |
2 |
Correct |
116 ms |
27144 KB |
Output is correct |
3 |
Correct |
99 ms |
24604 KB |
Output is correct |
4 |
Correct |
100 ms |
24612 KB |
Output is correct |
5 |
Correct |
82 ms |
22412 KB |
Output is correct |
6 |
Correct |
81 ms |
22664 KB |
Output is correct |
7 |
Correct |
90 ms |
23452 KB |
Output is correct |
8 |
Correct |
90 ms |
22320 KB |
Output is correct |
9 |
Correct |
78 ms |
22708 KB |
Output is correct |
10 |
Correct |
81 ms |
23844 KB |
Output is correct |
11 |
Correct |
181 ms |
29844 KB |
Output is correct |
12 |
Correct |
472 ms |
36240 KB |
Output is correct |
13 |
Correct |
345 ms |
30212 KB |
Output is correct |
14 |
Correct |
332 ms |
30088 KB |
Output is correct |
15 |
Correct |
205 ms |
30004 KB |
Output is correct |
16 |
Correct |
239 ms |
30032 KB |
Output is correct |
17 |
Correct |
170 ms |
29276 KB |
Output is correct |
18 |
Correct |
208 ms |
31604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
8 ms |
9676 KB |
Output is correct |
5 |
Correct |
7 ms |
9676 KB |
Output is correct |
6 |
Correct |
7 ms |
9748 KB |
Output is correct |
7 |
Correct |
6 ms |
9672 KB |
Output is correct |
8 |
Correct |
7 ms |
9704 KB |
Output is correct |
9 |
Correct |
6 ms |
9676 KB |
Output is correct |
10 |
Correct |
6 ms |
9676 KB |
Output is correct |
11 |
Correct |
6 ms |
9628 KB |
Output is correct |
12 |
Correct |
6 ms |
9676 KB |
Output is correct |
13 |
Correct |
6 ms |
9676 KB |
Output is correct |
14 |
Correct |
7 ms |
9676 KB |
Output is correct |
15 |
Correct |
6 ms |
9676 KB |
Output is correct |
16 |
Correct |
7 ms |
9676 KB |
Output is correct |
17 |
Correct |
6 ms |
9676 KB |
Output is correct |
18 |
Correct |
8 ms |
9676 KB |
Output is correct |
19 |
Correct |
8 ms |
9704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
8 ms |
9676 KB |
Output is correct |
5 |
Correct |
7 ms |
9676 KB |
Output is correct |
6 |
Correct |
7 ms |
9748 KB |
Output is correct |
7 |
Correct |
6 ms |
9672 KB |
Output is correct |
8 |
Correct |
7 ms |
9704 KB |
Output is correct |
9 |
Correct |
6 ms |
9676 KB |
Output is correct |
10 |
Correct |
6 ms |
9676 KB |
Output is correct |
11 |
Correct |
6 ms |
9628 KB |
Output is correct |
12 |
Correct |
6 ms |
9676 KB |
Output is correct |
13 |
Correct |
6 ms |
9676 KB |
Output is correct |
14 |
Correct |
7 ms |
9676 KB |
Output is correct |
15 |
Correct |
6 ms |
9676 KB |
Output is correct |
16 |
Correct |
7 ms |
9676 KB |
Output is correct |
17 |
Correct |
6 ms |
9676 KB |
Output is correct |
18 |
Correct |
8 ms |
9676 KB |
Output is correct |
19 |
Correct |
8 ms |
9704 KB |
Output is correct |
20 |
Correct |
509 ms |
176892 KB |
Output is correct |
21 |
Correct |
510 ms |
176996 KB |
Output is correct |
22 |
Correct |
162 ms |
12808 KB |
Output is correct |
23 |
Correct |
160 ms |
12896 KB |
Output is correct |
24 |
Correct |
381 ms |
122792 KB |
Output is correct |
25 |
Correct |
310 ms |
82436 KB |
Output is correct |
26 |
Correct |
226 ms |
77304 KB |
Output is correct |
27 |
Correct |
270 ms |
90968 KB |
Output is correct |
28 |
Correct |
362 ms |
121652 KB |
Output is correct |
29 |
Correct |
258 ms |
81316 KB |
Output is correct |
30 |
Correct |
253 ms |
87020 KB |
Output is correct |
31 |
Correct |
268 ms |
93340 KB |
Output is correct |
32 |
Correct |
534 ms |
168968 KB |
Output is correct |
33 |
Correct |
580 ms |
223692 KB |
Output is correct |
34 |
Correct |
440 ms |
143264 KB |
Output is correct |
35 |
Correct |
541 ms |
169100 KB |
Output is correct |
36 |
Correct |
621 ms |
221940 KB |
Output is correct |
37 |
Correct |
457 ms |
143472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
23844 KB |
Output is correct |
2 |
Correct |
116 ms |
27144 KB |
Output is correct |
3 |
Correct |
99 ms |
24604 KB |
Output is correct |
4 |
Correct |
100 ms |
24612 KB |
Output is correct |
5 |
Correct |
82 ms |
22412 KB |
Output is correct |
6 |
Correct |
81 ms |
22664 KB |
Output is correct |
7 |
Correct |
90 ms |
23452 KB |
Output is correct |
8 |
Correct |
90 ms |
22320 KB |
Output is correct |
9 |
Correct |
78 ms |
22708 KB |
Output is correct |
10 |
Correct |
81 ms |
23844 KB |
Output is correct |
11 |
Correct |
181 ms |
29844 KB |
Output is correct |
12 |
Correct |
472 ms |
36240 KB |
Output is correct |
13 |
Correct |
345 ms |
30212 KB |
Output is correct |
14 |
Correct |
332 ms |
30088 KB |
Output is correct |
15 |
Correct |
205 ms |
30004 KB |
Output is correct |
16 |
Correct |
239 ms |
30032 KB |
Output is correct |
17 |
Correct |
170 ms |
29276 KB |
Output is correct |
18 |
Correct |
208 ms |
31604 KB |
Output is correct |
19 |
Correct |
6 ms |
9676 KB |
Output is correct |
20 |
Correct |
6 ms |
9676 KB |
Output is correct |
21 |
Correct |
6 ms |
9676 KB |
Output is correct |
22 |
Correct |
8 ms |
9676 KB |
Output is correct |
23 |
Correct |
7 ms |
9676 KB |
Output is correct |
24 |
Correct |
7 ms |
9748 KB |
Output is correct |
25 |
Correct |
6 ms |
9672 KB |
Output is correct |
26 |
Correct |
7 ms |
9704 KB |
Output is correct |
27 |
Correct |
6 ms |
9676 KB |
Output is correct |
28 |
Correct |
6 ms |
9676 KB |
Output is correct |
29 |
Correct |
6 ms |
9628 KB |
Output is correct |
30 |
Correct |
6 ms |
9676 KB |
Output is correct |
31 |
Correct |
6 ms |
9676 KB |
Output is correct |
32 |
Correct |
7 ms |
9676 KB |
Output is correct |
33 |
Correct |
6 ms |
9676 KB |
Output is correct |
34 |
Correct |
7 ms |
9676 KB |
Output is correct |
35 |
Correct |
6 ms |
9676 KB |
Output is correct |
36 |
Correct |
8 ms |
9676 KB |
Output is correct |
37 |
Correct |
8 ms |
9704 KB |
Output is correct |
38 |
Correct |
509 ms |
176892 KB |
Output is correct |
39 |
Correct |
510 ms |
176996 KB |
Output is correct |
40 |
Correct |
162 ms |
12808 KB |
Output is correct |
41 |
Correct |
160 ms |
12896 KB |
Output is correct |
42 |
Correct |
381 ms |
122792 KB |
Output is correct |
43 |
Correct |
310 ms |
82436 KB |
Output is correct |
44 |
Correct |
226 ms |
77304 KB |
Output is correct |
45 |
Correct |
270 ms |
90968 KB |
Output is correct |
46 |
Correct |
362 ms |
121652 KB |
Output is correct |
47 |
Correct |
258 ms |
81316 KB |
Output is correct |
48 |
Correct |
253 ms |
87020 KB |
Output is correct |
49 |
Correct |
268 ms |
93340 KB |
Output is correct |
50 |
Correct |
534 ms |
168968 KB |
Output is correct |
51 |
Correct |
580 ms |
223692 KB |
Output is correct |
52 |
Correct |
440 ms |
143264 KB |
Output is correct |
53 |
Correct |
541 ms |
169100 KB |
Output is correct |
54 |
Correct |
621 ms |
221940 KB |
Output is correct |
55 |
Correct |
457 ms |
143472 KB |
Output is correct |
56 |
Incorrect |
90 ms |
23788 KB |
Output isn't correct |
57 |
Halted |
0 ms |
0 KB |
- |