#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 = 1e5 + 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 {
ll t, l, r, c;
void input() {
cin >> t >> l >> r >> c;
}
} treat[mxn];
ll pos[mxn];
ll f[mxn];
bool visited[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<ll> 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 <= len; 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) {
pq.push({-treat[i].c, i});
}
while (!pq.empty()) {
pair<ll, ll> top = pq.top(); pq.pop();
int u = top.se; ll dist = -top.fi;
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();
if (visited[top.se]) {
mn1[p].pop();
continue;
}
if (-top.fi > treat[u].r - treat[u].t + 1) break;
mn1[p].pop();
visited[top.se] = true;
pq.push(mp(-(dist + treat[top.se].c), top.se));
}
}
for (int p = pos[u]; p <= len; p += p & -p) {
while (!mn2[p].empty()) {
pair<ll, ll> top = mn2[p].top();
if (visited[top.se]) {
mn2[p].pop();
continue;
}
if (-top.fi > treat[u].r + treat[u].t + 1) break;
mn2[p].pop();
visited[top.se] = true;
pq.push(mp(-(dist + treat[top.se].c), top.se));
}
}
}
cout << -1 << '\n';
return 0;
}
Compilation message
treatment.cpp: In function 'int main()':
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".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
treatment.cpp:76:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | freopen(TASK".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
18664 KB |
Output is correct |
2 |
Correct |
46 ms |
18656 KB |
Output is correct |
3 |
Correct |
71 ms |
19444 KB |
Output is correct |
4 |
Correct |
65 ms |
19452 KB |
Output is correct |
5 |
Correct |
60 ms |
21596 KB |
Output is correct |
6 |
Correct |
65 ms |
18664 KB |
Output is correct |
7 |
Correct |
61 ms |
18708 KB |
Output is correct |
8 |
Correct |
36 ms |
18412 KB |
Output is correct |
9 |
Correct |
34 ms |
18676 KB |
Output is correct |
10 |
Correct |
40 ms |
18416 KB |
Output is correct |
11 |
Correct |
57 ms |
21488 KB |
Output is correct |
12 |
Correct |
61 ms |
21524 KB |
Output is correct |
13 |
Correct |
64 ms |
21488 KB |
Output is correct |
14 |
Correct |
65 ms |
21488 KB |
Output is correct |
15 |
Correct |
66 ms |
18672 KB |
Output is correct |
16 |
Correct |
66 ms |
18640 KB |
Output is correct |
17 |
Correct |
61 ms |
17912 KB |
Output is correct |
18 |
Correct |
61 ms |
20720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7516 KB |
Output is correct |
2 |
Correct |
3 ms |
7516 KB |
Output is correct |
3 |
Correct |
3 ms |
7516 KB |
Output is correct |
4 |
Correct |
4 ms |
7512 KB |
Output is correct |
5 |
Correct |
3 ms |
7516 KB |
Output is correct |
6 |
Correct |
3 ms |
7516 KB |
Output is correct |
7 |
Correct |
3 ms |
7516 KB |
Output is correct |
8 |
Correct |
3 ms |
7424 KB |
Output is correct |
9 |
Correct |
3 ms |
7516 KB |
Output is correct |
10 |
Correct |
3 ms |
7516 KB |
Output is correct |
11 |
Correct |
3 ms |
7516 KB |
Output is correct |
12 |
Correct |
3 ms |
7516 KB |
Output is correct |
13 |
Correct |
3 ms |
7516 KB |
Output is correct |
14 |
Correct |
3 ms |
7516 KB |
Output is correct |
15 |
Correct |
3 ms |
7404 KB |
Output is correct |
16 |
Correct |
4 ms |
7516 KB |
Output is correct |
17 |
Correct |
3 ms |
7516 KB |
Output is correct |
18 |
Correct |
3 ms |
7516 KB |
Output is correct |
19 |
Correct |
3 ms |
7516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7516 KB |
Output is correct |
2 |
Correct |
3 ms |
7516 KB |
Output is correct |
3 |
Correct |
3 ms |
7516 KB |
Output is correct |
4 |
Correct |
4 ms |
7512 KB |
Output is correct |
5 |
Correct |
3 ms |
7516 KB |
Output is correct |
6 |
Correct |
3 ms |
7516 KB |
Output is correct |
7 |
Correct |
3 ms |
7516 KB |
Output is correct |
8 |
Correct |
3 ms |
7424 KB |
Output is correct |
9 |
Correct |
3 ms |
7516 KB |
Output is correct |
10 |
Correct |
3 ms |
7516 KB |
Output is correct |
11 |
Correct |
3 ms |
7516 KB |
Output is correct |
12 |
Correct |
3 ms |
7516 KB |
Output is correct |
13 |
Correct |
3 ms |
7516 KB |
Output is correct |
14 |
Correct |
3 ms |
7516 KB |
Output is correct |
15 |
Correct |
3 ms |
7404 KB |
Output is correct |
16 |
Correct |
4 ms |
7516 KB |
Output is correct |
17 |
Correct |
3 ms |
7516 KB |
Output is correct |
18 |
Correct |
3 ms |
7516 KB |
Output is correct |
19 |
Correct |
3 ms |
7516 KB |
Output is correct |
20 |
Correct |
10 ms |
8796 KB |
Output is correct |
21 |
Correct |
10 ms |
8972 KB |
Output is correct |
22 |
Correct |
9 ms |
9052 KB |
Output is correct |
23 |
Correct |
7 ms |
9016 KB |
Output is correct |
24 |
Correct |
10 ms |
9308 KB |
Output is correct |
25 |
Correct |
10 ms |
9052 KB |
Output is correct |
26 |
Correct |
11 ms |
9160 KB |
Output is correct |
27 |
Correct |
10 ms |
9308 KB |
Output is correct |
28 |
Correct |
10 ms |
9308 KB |
Output is correct |
29 |
Correct |
10 ms |
9052 KB |
Output is correct |
30 |
Correct |
7 ms |
9052 KB |
Output is correct |
31 |
Correct |
8 ms |
9308 KB |
Output is correct |
32 |
Correct |
9 ms |
9052 KB |
Output is correct |
33 |
Correct |
9 ms |
9308 KB |
Output is correct |
34 |
Correct |
11 ms |
9076 KB |
Output is correct |
35 |
Correct |
8 ms |
9052 KB |
Output is correct |
36 |
Correct |
9 ms |
9424 KB |
Output is correct |
37 |
Correct |
11 ms |
9048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
18664 KB |
Output is correct |
2 |
Correct |
46 ms |
18656 KB |
Output is correct |
3 |
Correct |
71 ms |
19444 KB |
Output is correct |
4 |
Correct |
65 ms |
19452 KB |
Output is correct |
5 |
Correct |
60 ms |
21596 KB |
Output is correct |
6 |
Correct |
65 ms |
18664 KB |
Output is correct |
7 |
Correct |
61 ms |
18708 KB |
Output is correct |
8 |
Correct |
36 ms |
18412 KB |
Output is correct |
9 |
Correct |
34 ms |
18676 KB |
Output is correct |
10 |
Correct |
40 ms |
18416 KB |
Output is correct |
11 |
Correct |
57 ms |
21488 KB |
Output is correct |
12 |
Correct |
61 ms |
21524 KB |
Output is correct |
13 |
Correct |
64 ms |
21488 KB |
Output is correct |
14 |
Correct |
65 ms |
21488 KB |
Output is correct |
15 |
Correct |
66 ms |
18672 KB |
Output is correct |
16 |
Correct |
66 ms |
18640 KB |
Output is correct |
17 |
Correct |
61 ms |
17912 KB |
Output is correct |
18 |
Correct |
61 ms |
20720 KB |
Output is correct |
19 |
Correct |
3 ms |
7516 KB |
Output is correct |
20 |
Correct |
3 ms |
7516 KB |
Output is correct |
21 |
Correct |
3 ms |
7516 KB |
Output is correct |
22 |
Correct |
4 ms |
7512 KB |
Output is correct |
23 |
Correct |
3 ms |
7516 KB |
Output is correct |
24 |
Correct |
3 ms |
7516 KB |
Output is correct |
25 |
Correct |
3 ms |
7516 KB |
Output is correct |
26 |
Correct |
3 ms |
7424 KB |
Output is correct |
27 |
Correct |
3 ms |
7516 KB |
Output is correct |
28 |
Correct |
3 ms |
7516 KB |
Output is correct |
29 |
Correct |
3 ms |
7516 KB |
Output is correct |
30 |
Correct |
3 ms |
7516 KB |
Output is correct |
31 |
Correct |
3 ms |
7516 KB |
Output is correct |
32 |
Correct |
3 ms |
7516 KB |
Output is correct |
33 |
Correct |
3 ms |
7404 KB |
Output is correct |
34 |
Correct |
4 ms |
7516 KB |
Output is correct |
35 |
Correct |
3 ms |
7516 KB |
Output is correct |
36 |
Correct |
3 ms |
7516 KB |
Output is correct |
37 |
Correct |
3 ms |
7516 KB |
Output is correct |
38 |
Correct |
10 ms |
8796 KB |
Output is correct |
39 |
Correct |
10 ms |
8972 KB |
Output is correct |
40 |
Correct |
9 ms |
9052 KB |
Output is correct |
41 |
Correct |
7 ms |
9016 KB |
Output is correct |
42 |
Correct |
10 ms |
9308 KB |
Output is correct |
43 |
Correct |
10 ms |
9052 KB |
Output is correct |
44 |
Correct |
11 ms |
9160 KB |
Output is correct |
45 |
Correct |
10 ms |
9308 KB |
Output is correct |
46 |
Correct |
10 ms |
9308 KB |
Output is correct |
47 |
Correct |
10 ms |
9052 KB |
Output is correct |
48 |
Correct |
7 ms |
9052 KB |
Output is correct |
49 |
Correct |
8 ms |
9308 KB |
Output is correct |
50 |
Correct |
9 ms |
9052 KB |
Output is correct |
51 |
Correct |
9 ms |
9308 KB |
Output is correct |
52 |
Correct |
11 ms |
9076 KB |
Output is correct |
53 |
Correct |
8 ms |
9052 KB |
Output is correct |
54 |
Correct |
9 ms |
9424 KB |
Output is correct |
55 |
Correct |
11 ms |
9048 KB |
Output is correct |
56 |
Correct |
214 ms |
43728 KB |
Output is correct |
57 |
Correct |
227 ms |
43976 KB |
Output is correct |
58 |
Correct |
242 ms |
41736 KB |
Output is correct |
59 |
Correct |
223 ms |
42192 KB |
Output is correct |
60 |
Correct |
171 ms |
46536 KB |
Output is correct |
61 |
Correct |
226 ms |
41820 KB |
Output is correct |
62 |
Correct |
221 ms |
43496 KB |
Output is correct |
63 |
Correct |
216 ms |
45384 KB |
Output is correct |
64 |
Correct |
196 ms |
45260 KB |
Output is correct |
65 |
Correct |
69 ms |
20424 KB |
Output is correct |
66 |
Correct |
129 ms |
46024 KB |
Output is correct |
67 |
Correct |
245 ms |
49344 KB |
Output is correct |
68 |
Correct |
212 ms |
52156 KB |
Output is correct |
69 |
Correct |
204 ms |
54472 KB |
Output is correct |
70 |
Correct |
300 ms |
46792 KB |
Output is correct |
71 |
Correct |
229 ms |
48328 KB |
Output is correct |
72 |
Correct |
203 ms |
51372 KB |
Output is correct |
73 |
Correct |
266 ms |
47656 KB |
Output is correct |
74 |
Correct |
103 ms |
48692 KB |
Output is correct |
75 |
Correct |
100 ms |
51112 KB |
Output is correct |
76 |
Correct |
164 ms |
50168 KB |
Output is correct |
77 |
Correct |
159 ms |
49608 KB |
Output is correct |
78 |
Correct |
253 ms |
49996 KB |
Output is correct |
79 |
Correct |
312 ms |
47816 KB |
Output is correct |
80 |
Correct |
311 ms |
47764 KB |
Output is correct |
81 |
Correct |
146 ms |
48460 KB |
Output is correct |
82 |
Correct |
290 ms |
47112 KB |
Output is correct |
83 |
Correct |
314 ms |
52932 KB |
Output is correct |
84 |
Correct |
310 ms |
47816 KB |
Output is correct |
85 |
Correct |
233 ms |
48440 KB |
Output is correct |
86 |
Correct |
240 ms |
49100 KB |
Output is correct |
87 |
Correct |
266 ms |
49608 KB |
Output is correct |
88 |
Correct |
282 ms |
53448 KB |
Output is correct |
89 |
Correct |
255 ms |
49860 KB |
Output is correct |
90 |
Correct |
187 ms |
52424 KB |
Output is correct |
91 |
Correct |
139 ms |
47656 KB |
Output is correct |
92 |
Correct |
235 ms |
48688 KB |
Output is correct |
93 |
Correct |
275 ms |
48224 KB |
Output is correct |
94 |
Correct |
319 ms |
52400 KB |
Output is correct |
95 |
Correct |
262 ms |
47884 KB |
Output is correct |
96 |
Correct |
154 ms |
48580 KB |
Output is correct |
97 |
Correct |
181 ms |
50632 KB |
Output is correct |
98 |
Correct |
190 ms |
50380 KB |
Output is correct |
99 |
Correct |
168 ms |
51144 KB |
Output is correct |
100 |
Correct |
212 ms |
51832 KB |
Output is correct |
101 |
Correct |
229 ms |
50120 KB |
Output is correct |
102 |
Correct |
175 ms |
50116 KB |
Output is correct |
103 |
Correct |
196 ms |
48404 KB |
Output is correct |