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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
#define g4(a) get<4>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
using db = double;
using ll = long long;
using ld = long double;
using ii = pair<int, int>;
using iii = tuple<int, int, int>;
using iiii = tuple<int, int, int, int>;
using iiiii = tuple<int, int, int, int, int>;
template<class key, class value = null_type, class cmp = less<key> >
using ordered_set = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
int N, M, ans = (int)1e16, T[100005], L[100005], R[100005], C[100005], dist[100005], discT[100005];
bool seen[100005];
vector<int> all, dvec;
vector<ii> arr[2][100005];
priority_queue<iiiii, vector<iiiii>, greater<iiiii> > pq;
struct node {
node *left, *right;
int S, E;
set<ii> val;
node(int _s, int _e, vector<ii> arr[]) : S(_s), E(_e) {
if (S == E) {
for (auto i : arr[S]) {
val.insert(i);
}
return;
}
int M = (S + E) >> 1;
left = new node(S, M, arr);
right = new node(M + 1, E, arr);
for (auto cval : {left->val, right->val})
for (auto i : cval) {
val.insert(i);
}
}
void get_all(int l, int r, int x, int y) {
if (l > E || r < S) return;
if (l <= S && E <= r) {
vector<set<ii>::iterator> to_del;
for (auto it = val.lower_bound(mp(x, 0ll)); it != val.end() && it->first <= y; ++it) {
if (!seen[it->second]) {
all.pb(it->second);
}
to_del.pb(it);
}
for (auto it : to_del) {
val.erase(it);
}
return;
}
left->get_all(l, r, x, y);
right->get_all(l, r, x, y);
}
} *root[2];
main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> M;
for (int i = 1; i <= M; i++) {
cin >> T[i] >> L[i] >> R[i] >> C[i];
dvec.pb(T[i]);
}
sort(dvec.begin(), dvec.end());
dvec.erase(unique(dvec.begin(), dvec.end()), dvec.end());
for (int i = 1; i <= M; i++) {
discT[i] = lower_bound(dvec.begin(), dvec.end(), T[i]) - dvec.begin() + 1;
arr[0][discT[i]].eb(L[i] - T[i], i);
arr[1][discT[i]].eb(L[i] + T[i], i);
}
root[0] = new node(1, M, arr[0]);
root[1] = new node(1, M, arr[1]);
for (int i = 1; i <= M; i++) {
if (L[i] == 1) {
dist[i] = 0;
pq.emplace(C[i], 1, discT[i], 1 + R[i] - T[i], 0);
pq.emplace(C[i], discT[i], M, 1 + R[i] + T[i], 1);
seen[i] = 1;
} else {
dist[i] = (int)1e16;
}
}
while (!pq.empty()) {
auto [d, l, r, y, ty] = pq.top();
pq.pop();
auto proc = [&](int v) {
//~ cout << "PROC " << v << ' ' << d << '\n';
pq.emplace(d + C[v], 1, discT[v], 1 + R[v] - T[v], 0);
pq.emplace(d + C[v], discT[v], M, 1 + R[v] + T[v], 1);
seen[v] = 1;
};
all.clear();
root[ty]->get_all(l, r, (int)-1e16, y);
for (auto i : all) {
//~ cout << "INRANGE " << l << ' ' << r << ' ' << x << ' ' << y << ' ' << d << '\n';
dist[i] = d;
proc(i);
}
}
for (int i = 1; i <= M; i++) {
if (R[i] == N) {
//~ cout << "@ " << i << ' ' << dist[i] + C[i] << '\n';
ans = min(ans, dist[i] + C[i]);
}
}
if (ans >= (int)1e16) cout << "-1\n";
else cout << ans << '\n';
}
Compilation message (stderr)
treatment.cpp:73:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
73 | main() {
| ^~~~
# | 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... |