This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* There's someone in my head but it's not me */
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
#define SZ(x) (int) x.size()
#define F first
#define S second
#define lc id << 1
#define rc lc | 1
const int N = 1e5 + 10;
struct ver { int l, r, t, c; };
int n, m, M[N]; ll dp[N]; array<set<pii>, 2> seg[N << 2]; ver A[N];
void build(int id = 1, int l = 1, int r = m + 1) {
for (int i = l; i < r; i++) {
seg[id][0].insert({A[i].l + A[i].t, i});
seg[id][1].insert({A[i].l - A[i].t, i});
}
if (r - l < 2) return;
int md = (l + r) >> 1;
build(lc, l, md), build(rc, md, r);
}
void remove(int p, int id = 1, int l = 1, int r = m + 1) {
seg[id][0].erase({A[p].l + A[p].t, p});
seg[id][1].erase({A[p].l - A[p].t, p});
if (r - l < 2) return;
int md = (l + r) >> 1;
if (p < md) remove(p, lc, l, md);
else remove(p, rc, md, r);
}
int get(int ql, int qr, int x, int t, int id = 1, int l = 1, int r = m + 1) {
if (qr <= l || r <= ql || seg[id][t].empty() || seg[id][t].begin()->F > x) return -1;
if (ql <= l && r <= qr) return seg[id][t].begin()->S;
int md = (l + r) >> 1, y; y = get(ql, qr, x, t, lc, l, md);
if (~y) return y;
return get(ql, qr, x, t, rc, md, r);
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d%d%d", &A[i].t, &A[i].l, &A[i].r, &A[i].c); A[i].r++;
}
sort(A + 1, A + m + 1, [&] (const ver &a, const ver &b) { return a.t < b.t; });
build();
fill(dp, dp + N, 1e18); priority_queue<pair<ll, int>> pq;
for (int i = 1; i <= m; i++) {
if (A[i].l < 2) pq.push({-(dp[i] = A[i].c), i}), remove(i);
}
while (SZ(pq)) {
int v = pq.top().S; pq.pop();
if (M[v]) continue;
M[v] = 1;
int x = get(1, v, A[v].r - A[v].t, 1);
while (~x) {
if (dp[x] > dp[v] + A[x].c)
dp[x] = dp[v] + A[x].c, pq.emplace(-dp[x], x);
remove(x);
//printf("%d -> %d\n", v, x);
x = get(1, v, A[v].r - A[v].t, 1);
}
x = get(v + 1, m + 1, A[v].r + A[v].t, 0);
while (~x) {
if (dp[x] > dp[v] + A[x].c)
dp[x] = dp[v] + A[x].c, pq.emplace(-dp[x], x);
remove(x);
//printf("%d -> %d\n", v, x);
x = get(v + 1, m + 1, A[v].r + A[v].t, 0);
}
}
ll ret = 1e18;
for (int i = 1; i <= m; i++)
if (A[i].r == n + 1) ret = min(ret, dp[i]);
printf("%lld\n", ret < 1e18 ? ret : -1);
return 0;
}
Compilation message (stderr)
treatment.cpp: In function 'int main()':
treatment.cpp:47:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
47 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
treatment.cpp:49:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
49 | scanf("%d%d%d%d", &A[i].t, &A[i].l, &A[i].r, &A[i].c); A[i].r++;
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |