This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
const ll MOD=1e9+7;
const ll MOD2=998244353;
const ll N=1e5+5;
const ll K=350;
const ld pi=acos(-1);
const ll INF=(1LL<<60);
#define SQ(i) ((i)*(i))
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define setp setprecision
#define lwb lower_bound
#define SZ(_a) (ll)_a.size()
bool vis[N];
struct BIT{
set<pll> B[N];
void add(ll tox,pll ob){
while(tox<N){
B[tox].insert(ob);
tox+=(tox&-tox);
}
}
vector<ll> q(ll tox,ll lim){
vector<ll> tmp;
while(tox){
while(SZ(B[tox])){
pll i=*B[tox].begin();
if(i.X>lim)break;
if(!vis[i.Y])tmp.pb(i.Y);
vis[i.Y]=1;
B[tox].erase(B[tox].begin());
}
tox-=(tox&-tox);
}
return tmp;
}
}t1,t2;
ll n,m,dis[N],C[N],T[N],L[N],R[N],ans=INF,id[N];
vector<ll> v[N],lis;
priority_queue<pll,vector<pll>,greater<pll> > pq;
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n>>m;
REP1(i,m){
cin>>T[i]>>L[i]>>R[i]>>C[i];
lis.pb(T[i]);
}
sort(lis.begin(),lis.end());
lis.resize(unique(lis.begin(),lis.end())-lis.begin());
REP1(i,m)id[i]=lwb(lis.begin(),lis.end(),T[i])-lis.begin()+1;
REP1(i,m){
if(L[i]==1)continue;
t1.add(id[i],mp(L[i]-T[i]-1,i));
t2.add(m-id[i]+1,mp(L[i]+T[i]-1,i));
}
/*REP1(i,m)REP1(j,m){
if(id[i]>=id[j]&&R[i]-T[i]>=L[j]-T[j]-1){
v[i].pb(j);
}else if(id[i]<id[j]&&R[i]+T[i]>=L[j]+T[j]-1){
v[i].pb(j);
}
}*/
REP1(i,m){
if(L[i]==1)pq.push(mp(dis[i]=C[i],i)),vis[i]=1;
else dis[i]=INF;
}
while(SZ(pq)){
ll x=pq.top().Y,y=pq.top().X;pq.pop();
vector<ll> to1=t1.q(id[x],R[x]-T[x]),to2=t2.q(m-id[x],R[x]+T[x]);
for(auto i:to1){
vis[i]=1;
pq.push(mp(dis[i]=y+C[i],i));
}
for(auto i:to2){
vis[i]=1;
pq.push(mp(dis[i]=y+C[i],i));
}
}
REP1(i,m)if(R[i]==n)ans=min(ans,dis[i]);
cout<<(ans==INF?-1:ans)<<"\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |