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<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXM = 1e5;
const ll INF = 1e18;
int N, M;
struct Data { ll t, l, r, c; };
struct Point
{
ll x, y, p;
bool operator < (const Point &p) const { return x<p.x; }
};
struct Queue
{
int v; ll w;
bool operator < (const Queue &p) const { return w>p.w; }
};
Data A[MAXM+10];
Point P[MAXM+10];
ll dist[MAXM+10];
bool vis[MAXM+10];
vector<pll> tree[MAXM*4+10];
void init(int node, int tl, int tr)
{
if(tl==tr)
{
tree[node].push_back({P[tl].y, P[tl].p});
return;
}
int mid=tl+tr>>1;
init(node*2, tl, mid);
init(node*2+1, mid+1, tr);
tree[node].resize(tree[node*2].size()+tree[node*2+1].size());
merge(tree[node*2].begin(), tree[node*2].end(), tree[node*2+1].begin(), tree[node*2+1].end(), tree[node].begin(), greater<pll>());
//for(auto it : tree[node]) printf("%lld %lld / ", it.first, it.second); printf(" => %d %d\n", tl, tr);
}
void query(int node, int tl, int tr, int x, ll y, vector<int> &V)
{
if(x<tl) return;
if(tr<=x)
{
while(!tree[node].empty() && tree[node].back().first<=y)
{
if(!vis[tree[node].back().second]) V.push_back(tree[node].back().second);
tree[node].pop_back();
}
return;
}
int mid=tl+tr>>1;
query(node*2, tl, mid, x, y, V);
query(node*2+1, mid+1, tr, x, y, V);
}
int main()
{
int i, j;
scanf("%d%d", &N, &M);
for(i=1; i<=M; i++) scanf("%lld%lld%lld%lld", &A[i].t, &A[i].l, &A[i].r, &A[i].c);
for(i=1; i<=M; i++) P[i]={-A[i].t+A[i].l, A[i].t+A[i].l, i};
sort(P+1, P+M+1);
init(1, 1, M);
for(i=1; i<=M; i++) dist[i]=INF;
priority_queue<Queue> PQ;
for(i=1; i<=M; i++) if(A[i].l==1)
{
vis[i]=true;
dist[i]=A[i].c;
PQ.push({i, A[i].c});
}
while(!PQ.empty())
{
int now=PQ.top().v; PQ.pop();
//printf("!%d\n", now);
ll x, y;
x=upper_bound(P+1, P+M+1, (Point){-A[now].t+A[now].r+1, -1, -1})-P-1;
y=A[now].t+A[now].r+1;
vector<int> V;
query(1, 1, M, x, y, V);
//printf("!!%lld %lld\n", x, y);
for(auto nxt : V)
{
vis[nxt]=true;
dist[nxt]=dist[now]+A[nxt].c;
PQ.push({nxt, dist[nxt]});
}
}
ll ans=INF;
for(i=1; i<=M; i++) if(A[i].r==N) ans=min(ans, dist[i]);
if(ans==INF) ans=-1;
printf("%lld\n", ans);
}
Compilation message (stderr)
treatment.cpp: In function 'void init(int, int, int)':
treatment.cpp:40:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
treatment.cpp: In function 'void query(int, int, int, int, ll, std::vector<int>&)':
treatment.cpp:60:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
treatment.cpp: In function 'int main()':
treatment.cpp:67:9: warning: unused variable 'j' [-Wunused-variable]
int i, j;
^
treatment.cpp:69:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &N, &M);
~~~~~^~~~~~~~~~~~~~~~
treatment.cpp:70:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(i=1; i<=M; i++) scanf("%lld%lld%lld%lld", &A[i].t, &A[i].l, &A[i].r, &A[i].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... |