This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
const int mxn=1e5+5;
const ll inf=1e18;
int t[mxn],L[mxn],R[mxn],c[mxn];
vector<int> adj[mxn];
vector<vector<int>> segtree1(mxn*4),segtree2(mxn*4);
vector<int> xs;
vector<bool> used(mxn);
vector<int> cur;
void update1(int id,int l=0,int r=xs.size()-1,int v=1){
segtree1[v].push_back(id);
if(l==r){
return;
}
int mid=(l+r)/2;
if(t[id]<=mid) update1(id,l,mid,v*2);
else update1(id,mid+1,r,v*2+1);
}
void update2(int id,int l=0,int r=xs.size()-1,int v=1){
segtree2[v].push_back(id);
if(l==r){
return;
}
int mid=(l+r)/2;
if(t[id]<=mid) update2(id,l,mid,v*2);
else update2(id,mid+1,r,v*2+1);
}
bool comp1(int a,int b){
return L[a]+xs[t[a]]>L[b]+xs[t[b]];
}
void init1(int l=0,int r=xs.size()-1,int v=1){
sort(all(segtree1[v]),comp1);
if(l==r){
return;
}
int mid=(l+r)/2;
init1(l,mid,v*2);
init1(mid+1,r,v*2+1);
}
bool comp2(int a,int b){
return L[a]-xs[t[a]]>L[b]-xs[t[b]];
}
void init2(int l=0,int r=xs.size()-1,int v=1){
sort(all(segtree2[v]),comp2);
if(l==r){
return;
}
int mid=(l+r)/2;
init2(l,mid,v*2);
init2(mid+1,r,v*2+1);
}
void query1(int tl,int tr,int id,int l=0,int r=xs.size()-1,int v=1){
if(r<tl or tr<l){
return;
}
if(tl<=l and r<=tr){
while(!segtree1[v].empty() and R[id]+xs[t[id]]>=L[segtree1[v].back()]+xs[t[segtree1[v].back()]]-1){
int u=segtree1[v].back();
//cout<<"dasdasd "<<id<<' '<<u<<'\n';
segtree1[v].pop_back();
if(used[u]) continue;
used[u]=true;
cur.push_back(u);
}
return;
}
int mid=(l+r)/2;
query1(tl,min(mid,tr),id,l,mid,v*2);
query1(max(mid+1,tl),tr,id,mid+1,r,v*2+1);
}
void query2(int tl,int tr,int id,int l=0,int r=xs.size()-1,int v=1){
if(r<tl or tr<l){
return;
}
if(tl<=l and r<=tr){
while(!segtree2[v].empty() and R[id]-xs[t[id]]>=L[segtree2[v].back()]-xs[t[segtree2[v].back()]]-1){
int u=segtree2[v].back();
segtree2[v].pop_back();
if(used[u]) continue;
used[u]=true;
cur.push_back(u);
}
return ;
}
int mid=(l+r)/2;
query2(tl,min(mid,tr),id,l,mid,v*2);
query2(max(mid+1,tl),tr,id,mid+1,r,v*2+1);
}
vector<ll> dij(int n){
vector<ll> d(n,inf);
d[0]=0;
used[0]=true;
priority_queue<pair<ll,int>,vector<pair<ll,int>>,greater<pair<ll,int>>> pq;
pq.push({0,0});
while(!pq.empty()){
auto tmp=pq.top();
pq.pop();
int v=tmp.s;
if(tmp.f!=d[v]) continue;
//cout<<d[v]<<' '<<v<<'\n';
for(auto u:adj[v]){
if(used[u]) continue;
d[u]=d[v]+c[u];
used[u]=true;
pq.push({d[u],u});
}
if(v==0 or v==n-1) continue;
cur.clear();
query1(t[v],xs.size()-1,v);
query2(0,t[v],v);
for(auto u:cur){
d[u]=d[v]+c[u];
pq.push({d[u],u});
}
}
return d;
}
int main() {_
int n,m;
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>t[i]>>L[i]>>R[i]>>c[i];
xs.push_back(t[i]);
}
sort(all(xs));
xs.resize(unique(all(xs))-xs.begin());
for(int i=1;i<=m;i++){
t[i]=lower_bound(all(xs),t[i])-xs.begin();
}
for(int i=1;i<=m;i++){
update1(i);
update2(i);
}
init1();
init2();
for(int i=1;i<=m;i++){
if(L[i]==1) adj[0].push_back(i);
if(R[i]==n) adj[i].push_back(m+1);
}
vector<ll> d=dij(m+2);
cout<<(d[m+1]==inf?-1:d[m+1])<<'\n';
return 0;
}
//maybe its multiset not set
//yeeorz
//laborz
Compilation message (stderr)
treatment.cpp: In function 'void setIO(std::string)':
treatment.cpp:15:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
treatment.cpp:16:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |