#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
#define mp(x, y) make_pair(x, y)
#define sz(v) ((int) (v).size())
#define all(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define BIT(x, y) (((x) >> (y)) & 1)
#define pb push_back
#define max_rng *max_element
#define min_rng *min_element
#define rep(i, n) for(int i = 1, _n = (n); i <= _n; ++i)
#define forn(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i)
#define ford(i, a, b) for(int i = (a), _b = (b); i >= _b; --i)
template <class X, class Y>
inline bool maximize(X &x, Y y) {
return (x < y ? x = y, true : false);
}
template <class X, class Y>
inline bool minimize(X &x, Y y) {
return (x > y ? x = y, true : false);
}
template <class X>
inline void compress(vector<X> &a) {
sort(all(a));
a.resize(unique(all(a)) - a.begin());
}
int ctz(ll x) { return __builtin_ctzll(x); }
int lg(ll x) { return 63 - __builtin_clzll(x); }
int popcount(ll x) { return __builtin_popcount(x); }
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) {
return l + abs((ll) rng()) % (r - l + 1);
}
const ll oo = (ll) 1e17;
const int inf = (ll) 2e9 + 7, mod = (ll) 1e9 + 7;
const int mxn = 2e5 + 5;
void add(int &x, int y) { x += y; if (x >= mod) x -= mod; }
void sub(int &x, int y) { x -= y; if (x < 0) x += mod; }
int n, m;
struct Treat {
int t, l, r, c;
void input() {
cin >> t >> l >> r >> c;
}
} treat[mxn];
int pos[mxn];
ll f[mxn];
priority_queue<pair<ll, ll>> mn1[mxn], mn2[mxn];
int main(void) {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define TASK "task"
if (fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
cin >> n >> m;
rep(i, m) treat[i].input();
vector<int> cps;
rep(i, m) cps.pb(treat[i].t);
compress(cps);
int len = sz(cps);
rep(i, m) {
pos[i] = lower_bound(all(cps), treat[i].t) - cps.begin() + 1;
for (int p = pos[i]; p <= n; p += p & -p) {
mn1[p].push(mp(-(treat[i].l - treat[i].t), i));
}
for (int p = pos[i]; p > 0; p -= p & -p) {
mn2[p].push(mp(-(treat[i].l + treat[i].t), i));
}
}
memset(f, 0x3f, sizeof f);
priority_queue<pair<ll, ll>> pq;
forn(i, 1, m) if (treat[i].l == 1) {
f[i] = treat[i].c;
pq.push({-f[i], i});
}
while (!pq.empty()) {
pair<ll, ll> top = pq.top(); pq.pop();
int u = top.se; ll dist = -top.fi;
if (f[u] != dist) continue;
if (treat[u].r == n) {
cout << dist << '\n';
exit(0);
}
for (int p = pos[u]; p > 0; p -= p & -p) {
while (!mn1[p].empty()) {
pair<ll, ll> top = mn1[p].top(); mn1[p].pop();
if (-top.fi > treat[u].r - treat[u].t + 1) break;
if (minimize(f[top.se], dist + treat[top.se].c)) {
pq.push(mp(-f[top.se], top.se));
}
}
}
for (int p = pos[u]; p <= n; p += p & -p) {
while (!mn2[p].empty()) {
pair<ll, ll> top = mn2[p].top(); mn2[p].pop();
if (-top.fi > treat[u].r + treat[u].t + 1) break;
if (minimize(f[top.se], dist + treat[top.se].c)) {
pq.push(mp(-f[top.se], top.se));
}
}
}
}
cout << -1 << '\n';
return 0;
}
Compilation message
treatment.cpp: In function 'int main()':
treatment.cpp:85:9: warning: unused variable 'len' [-Wunused-variable]
85 | int len = sz(cps);
| ^~~
treatment.cpp:74:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
74 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
treatment.cpp:75:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
75 | freopen(TASK".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
43 ms |
33488 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
14316 KB |
Output is correct |
2 |
Runtime error |
14 ms |
25948 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
14316 KB |
Output is correct |
2 |
Runtime error |
14 ms |
25948 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
43 ms |
33488 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |