#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#define ep emplace
#define eb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef pair<int,int>pi;
typedef pair<ll,ll>pl;
const int inf=1e9+7;
const ll INF=1e18;
const int mx=100010;
struct query
{
int t,l,r,c;
};
vector<pi>adj[mx*2];
ll dis[mx*2];
bool chk[mx*2];
inline void dijk(int n,int s)
{
fill(dis,dis+n,INF);
priority_queue<pl,vector<pl>,greater<pl> >pq;
pq.ep(dis[s]=0,s);
while(!pq.empty())
{
int x=pq.top().se;
ll w=pq.top().fi;
pq.pop();
if(dis[x]!=w)
continue;
for(pi&t:adj[x])
if(dis[t.fi]>w+t.se)
pq.ep(dis[t.fi]=w+t.se,t.fi);
}
return;
}
int nct;
int nd[mx*4];
vector<query>qv;
void con(int n,int s,int e,const vector<int>&v)
{
if(s==e)
{
nd[n]=v[s];
return;
}
nd[n]=nct++;
int m=s+(e-s)/2;
con(n*2,s,m,v);
con(n*2+1,m+1,e,v);
adj[nd[n]].eb(nd[n*2],s==m?qv[v[s]].c:0);
adj[nd[n]].eb(nd[n*2+1],m+1==e?qv[v[e]].c:0);
return;
}
void put(int n,int s,int e,int S,int E,int st)
{
if(s>E||S>e)
return;
if(S<=s&&e<=E)
{
adj[st].eb(nd[n],s==e?qv[nd[n]].c:0);
return;
}
int m=s+(e-s)/2;
put(n*2,s,m,S,E,st);
put(n*2+1,m+1,e,S,E,st);
return;
}
void dnc(const vector<int>&v,int s,int e)
{
if(s==e)
return;
int m=s+(e-s)/2;
dnc(v,s,m);
dnc(v,m+1,e);
vector<int>lv,rv;
for(int i=s;i<=m;i++)
lv.eb(v[i]);
for(int i=m;i++<e;)
rv.eb(v[i]);
sort(all(lv),[&](const int&x,const int&y){return qv[x].l-qv[x].t<qv[y].l-qv[y].t;});
sort(all(rv),[&](const int&x,const int&y){return qv[x].l+qv[x].t<qv[y].l+qv[y].t;});
{
int lmx=(int)lv.size()-1;
con(1,0,lmx,lv);
for(int&t:rv)
{
int sc=-1,ec=lmx;
int cval=qv[t].r-qv[t].t+1;
while(sc<ec)
{
int mc=sc+(ec-sc+1)/2;
if(cval>=qv[lv[mc]].l-qv[lv[mc]].t)
sc=mc;
else
ec=mc-1;
}
if(sc>=0)
put(1,0,lmx,0,sc,t);
}
}
{
int rmx=(int)rv.size()-1;
con(1,0,rmx,rv);
for(int&t:lv)
{
int sc=-1,ec=rmx;
int cval=qv[t].r+qv[t].t+1;
while(sc<ec)
{
int mc=sc+(ec-sc+1)/2;
if(cval>=qv[rv[mc]].l+qv[rv[mc]].t)
sc=mc;
else
ec=mc-1;
}
if(sc>=0)
put(1,0,rmx,0,sc,t);
}
}
return;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n,m;
cin>>n>>m;
qv.resize(m);
for(auto&t:qv)
cin>>t.t>>t.l>>t.r>>t.c;
for(int i=0;i<m;i++)
{
if(qv[i].l==1)
adj[m+1].eb(i,qv[i].c);
if(qv[i].r==n)
adj[i].eb(m,0);
}
nct=m+2;
vector<int>cv(m);
for(int i=0;i<m;i++)
cv[i]=i;
sort(all(cv),[&](const int&x,const int&y){return qv[x].t<qv[y].t;});
dnc(cv,0,m-1);
dijk(nct,m+1);
if(dis[m]==INF)
dis[m]=-1;
cout<<dis[m]<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
108 ms |
28536 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
4 ms |
4992 KB |
Output is correct |
6 |
Correct |
5 ms |
4992 KB |
Output is correct |
7 |
Correct |
4 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
5 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
4 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
15 |
Correct |
3 ms |
4992 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
4992 KB |
Output is correct |
18 |
Correct |
4 ms |
4992 KB |
Output is correct |
19 |
Correct |
3 ms |
4992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
4 ms |
4992 KB |
Output is correct |
6 |
Correct |
5 ms |
4992 KB |
Output is correct |
7 |
Correct |
4 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
5 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
4 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
15 |
Correct |
3 ms |
4992 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
4992 KB |
Output is correct |
18 |
Correct |
4 ms |
4992 KB |
Output is correct |
19 |
Correct |
3 ms |
4992 KB |
Output is correct |
20 |
Correct |
31 ms |
8824 KB |
Output is correct |
21 |
Correct |
32 ms |
8824 KB |
Output is correct |
22 |
Correct |
26 ms |
8056 KB |
Output is correct |
23 |
Correct |
25 ms |
8064 KB |
Output is correct |
24 |
Correct |
42 ms |
9848 KB |
Output is correct |
25 |
Correct |
37 ms |
9336 KB |
Output is correct |
26 |
Correct |
33 ms |
9080 KB |
Output is correct |
27 |
Correct |
30 ms |
9080 KB |
Output is correct |
28 |
Correct |
38 ms |
9720 KB |
Output is correct |
29 |
Correct |
40 ms |
9340 KB |
Output is correct |
30 |
Correct |
29 ms |
9208 KB |
Output is correct |
31 |
Correct |
30 ms |
9088 KB |
Output is correct |
32 |
Correct |
40 ms |
10104 KB |
Output is correct |
33 |
Correct |
40 ms |
9600 KB |
Output is correct |
34 |
Correct |
39 ms |
9592 KB |
Output is correct |
35 |
Correct |
44 ms |
10104 KB |
Output is correct |
36 |
Correct |
37 ms |
9464 KB |
Output is correct |
37 |
Correct |
39 ms |
9592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
108 ms |
28536 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |