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;
struct Lisan: vector<int> {
void done() { sort(begin(), end()); erase(unique(begin(), end()), end()); }
int operator () (const int& v) const { return std::lower_bound(begin(), end(), v) - begin(); }
int upper_bound (const int& v) const { return std::upper_bound(begin(), end(), v) - begin(); }
};
struct Treatment {
int T, L, R, C, x, y;
Treatment (const int& _T, const int& _L, const int& _R, const int& _C):
T(_T), L(_L), R(_R), C(_C), x(L + T), y(L - T) {}
};
struct SegmentTree {
int n; vector<vector<pair<int, int>>> val;
SegmentTree (const int& _n): n(_n), val(4 << __lg(n)) {}
void insert(int i, int l, int r, int p, pair<int, int> v) {
if (l + 1 == r) {
val[i].push_back(v);
return;
}
int m = (l + r) / 2;
if (p < m) insert(i * 2, l, m, p, v);
else insert(i * 2 + 1, m, r, p, v);
}
void insert(int x, int y, int i) { insert(1, 0, n, x, pair<int, int>(y, i)); }
void build(int i, int l, int r) {
if (l + 1 == r) {
sort(begin(val[i]), end(val[i]), greater<pair<int, int>>());
return;
}
int m = (l + r) / 2;
build(i * 2, l, m);
build(i*2+1, m, r);
val[i].reserve(val[i * 2].size() + val[i*2+1].size());
merge(begin(val[i * 2]), end(val[i * 2]), begin(val[i*2+1]), end(val[i*2+1]), back_inserter(val[i]), greater<pair<int, int>>());
}
void build() { build(1, 0, n); }
void collect(int i, int l, int r, int x, int y, vector<int>& ids) {
if (x <= l) return;
if (r <= x) {
while (!val[i].empty()) {
auto [yi, id] = val[i].back();
if (yi <= y) {
ids.emplace_back(id);
val[i].pop_back();
} else break;
}
return;
}
int m = (l + r) / 2;
collect(i * 2, l, m, x, y, ids);
if (m < x) collect(i * 2 + 1, m, r, x, y, ids);
}
void collect(int x, int y, vector<int>& ids) { collect(1, 0, n, x, y, ids); }
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int N, M;
cin >> N >> M;
vector<Treatment> ts;
Lisan lisan;
ts.reserve(M);
lisan.reserve(M);
for (int T, L, R, C, i = 0; i < M; ++i) {
cin >> T >> L >> R >> C;
ts.emplace_back(T, L, R, C);
lisan.emplace_back(ts.back().x);
}
lisan.done();
SegmentTree sgt(lisan.size());
for (int i = 0; i < M; ++i) {
const auto& [T, L, R, C, x, y] = ts[i];
sgt.insert(lisan(x), y, i);
}
sgt.build();
vector<long long> dis(M, LLONG_MAX);
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq;
for (int i = 0; i < M; ++i) {
if (ts[i].L == 1) {
dis[i] = ts[i].C;
pq.emplace(dis[i], i);
}
}
vector<int> vs;
vs.reserve(M);
while (!pq.empty()) {
auto [d, u] = pq.top(); pq.pop();
int x = ts[u].R + ts[u].T + 1;
int y = ts[u].R - ts[u].T + 1;
sgt.collect(lisan.upper_bound(x), y, vs);
for (const int& v: vs) {
if (d + ts[v].C < dis[v]) {
dis[v] = d + ts[v].C;
pq.emplace(dis[v], v);
}
}
vs.clear();
}
long long ans = LLONG_MAX;
for (int i = 0; i < M; ++i) {
if (ts[i].R == N) {
ans = min(ans, dis[i]);
}
}
if (ans == LLONG_MAX) ans = -1;
cout << 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... |