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>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
#define F first
#define S second
const int MN = 1e5+5;
const ll inf = 1e12;
ll N, M, i, j, x, y, c, t, dis[MN], vs[MN], ans=1LL<<60;
struct idk{ll a,b,c,d;}arr[MN],go[MN];
map<ll,ll> mpx, mpy; vector<ll> ed;
struct pq{bool operator()(const pll&i,const pll&j){return i.S>j.S;}};
priority_queue<pll,vector<pll>,pq> q;
struct segtree{
vector<pii> st[6*MN]; ll n, c;
void init(ll N){n=N;}
void prep(){
for(int i=0;i<6*MN;i++)
sort(st[i].begin(),st[i].end(),[](pii i,pii j){return i.F>j.F;});
}
void upd(ll i,ll s,ll e,ll ind,ll val,ll id){
st[i].push_back({val,id});
if(s^e){
if((s+e)/2<ind) upd(2*i+1,(s+e)/2+1,e,ind,val,id);
else upd(2*i,s,(s+e)/2,ind,val,id);
}
}
inline void update(ll x,ll y,ll id){
upd(1,1,n,x,y,id);
}
void qu(ll i,ll s,ll e,ll ss,ll se,ll y){
if(s>=ss&&e<=se){
while(st[i].size()&&st[i].back().F<=y){
int id = st[i].back().S;
if(!vs[id]){
vs[id]=1;
q.push({id,c+arr[id].c});
}
st[i].pop_back();
}
}
else if((s+e)/2<ss) qu(2*i+1,(s+e)/2+1,e,ss,se,y);
else if((s+e)/2>=se) qu(2*i,s,(s+e)/2,ss,se,y);
else qu(2*i,s,(s+e)/2,ss,se,y), qu(2*i+1,(s+e)/2+1,e,ss,se,y);
}
inline void query(ll s,ll e,ll y,ll cur){
c = cur;
qu(1,1,n,s,e,y);
}
}st;
int main(){
scanf("%lld%lld",&N,&M);
for(i=1;i<=M;i++){
scanf("%lld%lld%lld%lld",&t,&x,&y,&c);
arr[i]={x,y,c,t};
if(x==1) q.push({i,c}), x=-inf, vs[i]=1;
if(y==N) ed.push_back(i), y=inf;
y++;
ll sx = t-y, ex = t-x;
ll sy = t+x, ey = t+y;
go[i]={sx,ex,sy,ey};
mpx[sx]=mpx[ex]=mpy[sy]=mpy[ey]=0;
}
i=0; st.init(mpx.size());
for(auto it=mpx.begin();it!=mpx.end();it++)
it->second = ++i;
i=0;
for(auto it=mpy.begin();it!=mpy.end();it++)
it->second = ++i;
for(i=1;i<=M;i++){
go[i].a=mpx[go[i].a]; go[i].b=mpx[go[i].b];
go[i].c=mpy[go[i].c]; go[i].d=mpy[go[i].d];
st.update(go[i].b,go[i].c,i);
}
st.prep();
memset(dis,-1,sizeof(dis));
while(q.size()){
pll x=q.top(); q.pop();
dis[x.F]=x.S;
int id = x.F;
st.query(go[id].a,mpx.size(),go[id].d,x.S);
}
for(auto v : ed){
if(dis[v]!=-1) ans=min(ans,dis[v]);
}
printf("%lld\n",ans==1LL<<60?-1:ans);
return 0;
}
Compilation message (stderr)
treatment.cpp: In function 'int main()':
treatment.cpp:57:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld",&N,&M);
~~~~~^~~~~~~~~~~~~~~~~~
treatment.cpp:59:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld%lld%lld",&t,&x,&y,&c);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |