#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <chrono>
#include <vector>
#include <map>
#include <random>
#include <set>
#include <algorithm>
#include <math.h>
#include <cstdio>
#include <stdio.h>
#include <queue>
#include <bitset>
#include <cstdlib>
#include <deque>
#include <cassert>
#include <stack>
using namespace std;
#define mp make_pair
#define f first
#define se second
#define pb push_back
#define ppb pop_back
#define ll long long
#define ull unsigned long long
#define cntbit(x) __builtin_popcount(x)
#define endl '\n'
#define uset unordered_set
#define umap unordered_map
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define ld long double
#define pll pair<long long, long long>
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T> inline T range(T l, T r) {
return uniform_int_distribution <T>(l, r)(rng);
}
template <typename T> void Min(T &a, T b) {
a = min(a, b);
}
template <typename T> void Max(T &a, T b) {
a = max(a, b);
}
const int mod = 1e9 + 7;
const ll inf = 2e18;
const int N = 1e5 + 15;
int n, m;
ll ans = -1;
vector <int> compress;
int sz;
ll d[N];
set <pair <ll, int>> q;
inline int gt(int x) {
return lower_bound(compress.begin(), compress.end(), x) - compress.begin() + 1;
}
struct pt {
int t, l, r, c;
} a[N];
struct segment_tree {
set <pii> t[N << 2];
void get(int v, int tl, int tr, int l, int r, int val, vector <int> &res) {
if(tl > r || tr < l)
return;
if(tl >= l && tr <= r) {
while(!t[v].empty() && t[v].begin()->f <= val) {
res.pb(t[v].begin()->se);
t[v].erase(t[v].begin());
}
return;
}
int mid = tl + tr >> 1;
get(v << 1, tl, mid, l, r, val, res);
get(v << 1 | 1, mid + 1, tr, l, r, val, res);
}
void update(int v, int tl, int tr, int pos, pii val, bool insert) {
if(insert)
t[v].insert(val);
else if(t[v].find(val) != t[v].end())
t[v].erase(val);
if(tl == tr)
return;
int mid = tl + tr >> 1;
if(pos <= mid)
update(v << 1, tl, mid, pos, val, insert);
else
update(v << 1 | 1, mid + 1, tr, pos, val, insert);
}
} t1, t2;
inline int nxt() {
int x;
scanf("%d", &x);
return x;
}
main() {
fill(d, d + N, inf);
n = nxt(); m = nxt();
for(int i = 1; i <= m; ++i) {
a[i].t = nxt(); a[i].l = nxt(); a[i].r = nxt(); a[i].c = nxt();
compress.pb(a[i].t);
}
sort(compress.begin(), compress.end());
compress.erase(unique(compress.begin(), compress.end()), compress.end());
sz = compress.size();
for(int i = 1; i <= m; ++i) {
--a[i].l;
if(a[i].l == 0) {
d[i] = a[i].c;
q.insert({d[i], i});
}
else {
t1.update(1, 1, sz, gt(a[i].t), mp(a[i].l + a[i].t, i), true);
t2.update(1, 1, sz, gt(a[i].t), mp(a[i].l - a[i].t, i), true);
}
}
while(!q.empty()) {
int i = q.begin()->se;
q.erase(q.begin());
vector <int> res[2] = {{}, {}};
t1.get(1, 1, sz, gt(a[i].t), sz, a[i].r + a[i].t, res[0]);
t2.get(1, 1, sz, 1, gt(a[i].t), a[i].r - a[i].t, res[1]);
for(int t = 0; t < 2; ++t) {
for(int j : res[t]) {
d[j] = d[i] + a[j].c;
q.insert(mp(d[j], j));
t1.update(1, 1, sz, gt(a[j].t), mp(a[j].l + a[j].t, j), false);
t2.update(1, 1, sz, gt(a[j].t), mp(a[j].l - a[j].t, j), false);
}
}
}
for(int i = 1; i <= m; ++i) {
if(a[i].r != n || d[i] == inf)
continue;
if(ans == -1 || d[i] < ans)
ans = d[i];
}
cout << ans << endl;
return 0;
}
Compilation message
treatment.cpp: In member function 'void segment_tree::get(int, int, int, int, int, int, std::vector<int>&)':
treatment.cpp:86:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = tl + tr >> 1;
~~~^~~~
treatment.cpp: In member function 'void segment_tree::update(int, int, int, int, std::pair<int, int>, bool)':
treatment.cpp:97:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = tl + tr >> 1;
~~~^~~~
treatment.cpp: At global scope:
treatment.cpp:111:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main() {
^
treatment.cpp: In function 'int nxt()':
treatment.cpp:107:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &x);
~~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
192 ms |
50032 KB |
Output is correct |
2 |
Correct |
176 ms |
50032 KB |
Output is correct |
3 |
Correct |
223 ms |
49648 KB |
Output is correct |
4 |
Correct |
214 ms |
49520 KB |
Output is correct |
5 |
Correct |
247 ms |
50032 KB |
Output is correct |
6 |
Correct |
181 ms |
50120 KB |
Output is correct |
7 |
Correct |
158 ms |
50028 KB |
Output is correct |
8 |
Correct |
130 ms |
50028 KB |
Output is correct |
9 |
Correct |
129 ms |
50032 KB |
Output is correct |
10 |
Correct |
116 ms |
50032 KB |
Output is correct |
11 |
Correct |
319 ms |
51056 KB |
Output is correct |
12 |
Correct |
334 ms |
50928 KB |
Output is correct |
13 |
Correct |
293 ms |
50032 KB |
Output is correct |
14 |
Correct |
293 ms |
50036 KB |
Output is correct |
15 |
Correct |
219 ms |
50032 KB |
Output is correct |
16 |
Correct |
229 ms |
50032 KB |
Output is correct |
17 |
Correct |
231 ms |
50032 KB |
Output is correct |
18 |
Correct |
324 ms |
51056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
38656 KB |
Output is correct |
2 |
Correct |
27 ms |
38656 KB |
Output is correct |
3 |
Correct |
26 ms |
38656 KB |
Output is correct |
4 |
Correct |
26 ms |
38656 KB |
Output is correct |
5 |
Correct |
26 ms |
38656 KB |
Output is correct |
6 |
Correct |
28 ms |
38656 KB |
Output is correct |
7 |
Correct |
26 ms |
38708 KB |
Output is correct |
8 |
Correct |
26 ms |
38656 KB |
Output is correct |
9 |
Correct |
26 ms |
38656 KB |
Output is correct |
10 |
Correct |
27 ms |
38656 KB |
Output is correct |
11 |
Correct |
27 ms |
38656 KB |
Output is correct |
12 |
Correct |
30 ms |
38784 KB |
Output is correct |
13 |
Correct |
27 ms |
38656 KB |
Output is correct |
14 |
Correct |
26 ms |
38648 KB |
Output is correct |
15 |
Correct |
26 ms |
38656 KB |
Output is correct |
16 |
Correct |
28 ms |
38656 KB |
Output is correct |
17 |
Correct |
27 ms |
38656 KB |
Output is correct |
18 |
Correct |
26 ms |
38656 KB |
Output is correct |
19 |
Correct |
26 ms |
38656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
38656 KB |
Output is correct |
2 |
Correct |
27 ms |
38656 KB |
Output is correct |
3 |
Correct |
26 ms |
38656 KB |
Output is correct |
4 |
Correct |
26 ms |
38656 KB |
Output is correct |
5 |
Correct |
26 ms |
38656 KB |
Output is correct |
6 |
Correct |
28 ms |
38656 KB |
Output is correct |
7 |
Correct |
26 ms |
38708 KB |
Output is correct |
8 |
Correct |
26 ms |
38656 KB |
Output is correct |
9 |
Correct |
26 ms |
38656 KB |
Output is correct |
10 |
Correct |
27 ms |
38656 KB |
Output is correct |
11 |
Correct |
27 ms |
38656 KB |
Output is correct |
12 |
Correct |
30 ms |
38784 KB |
Output is correct |
13 |
Correct |
27 ms |
38656 KB |
Output is correct |
14 |
Correct |
26 ms |
38648 KB |
Output is correct |
15 |
Correct |
26 ms |
38656 KB |
Output is correct |
16 |
Correct |
28 ms |
38656 KB |
Output is correct |
17 |
Correct |
27 ms |
38656 KB |
Output is correct |
18 |
Correct |
26 ms |
38656 KB |
Output is correct |
19 |
Correct |
26 ms |
38656 KB |
Output is correct |
20 |
Correct |
61 ms |
43264 KB |
Output is correct |
21 |
Correct |
62 ms |
43256 KB |
Output is correct |
22 |
Correct |
68 ms |
43512 KB |
Output is correct |
23 |
Correct |
68 ms |
43512 KB |
Output is correct |
24 |
Correct |
71 ms |
44024 KB |
Output is correct |
25 |
Correct |
92 ms |
45048 KB |
Output is correct |
26 |
Correct |
74 ms |
45048 KB |
Output is correct |
27 |
Correct |
68 ms |
45048 KB |
Output is correct |
28 |
Correct |
71 ms |
44028 KB |
Output is correct |
29 |
Correct |
74 ms |
44920 KB |
Output is correct |
30 |
Correct |
56 ms |
45048 KB |
Output is correct |
31 |
Correct |
58 ms |
45048 KB |
Output is correct |
32 |
Correct |
85 ms |
45176 KB |
Output is correct |
33 |
Correct |
82 ms |
45176 KB |
Output is correct |
34 |
Correct |
78 ms |
44792 KB |
Output is correct |
35 |
Correct |
84 ms |
45176 KB |
Output is correct |
36 |
Correct |
88 ms |
45304 KB |
Output is correct |
37 |
Correct |
79 ms |
44792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
192 ms |
50032 KB |
Output is correct |
2 |
Correct |
176 ms |
50032 KB |
Output is correct |
3 |
Correct |
223 ms |
49648 KB |
Output is correct |
4 |
Correct |
214 ms |
49520 KB |
Output is correct |
5 |
Correct |
247 ms |
50032 KB |
Output is correct |
6 |
Correct |
181 ms |
50120 KB |
Output is correct |
7 |
Correct |
158 ms |
50028 KB |
Output is correct |
8 |
Correct |
130 ms |
50028 KB |
Output is correct |
9 |
Correct |
129 ms |
50032 KB |
Output is correct |
10 |
Correct |
116 ms |
50032 KB |
Output is correct |
11 |
Correct |
319 ms |
51056 KB |
Output is correct |
12 |
Correct |
334 ms |
50928 KB |
Output is correct |
13 |
Correct |
293 ms |
50032 KB |
Output is correct |
14 |
Correct |
293 ms |
50036 KB |
Output is correct |
15 |
Correct |
219 ms |
50032 KB |
Output is correct |
16 |
Correct |
229 ms |
50032 KB |
Output is correct |
17 |
Correct |
231 ms |
50032 KB |
Output is correct |
18 |
Correct |
324 ms |
51056 KB |
Output is correct |
19 |
Correct |
26 ms |
38656 KB |
Output is correct |
20 |
Correct |
27 ms |
38656 KB |
Output is correct |
21 |
Correct |
26 ms |
38656 KB |
Output is correct |
22 |
Correct |
26 ms |
38656 KB |
Output is correct |
23 |
Correct |
26 ms |
38656 KB |
Output is correct |
24 |
Correct |
28 ms |
38656 KB |
Output is correct |
25 |
Correct |
26 ms |
38708 KB |
Output is correct |
26 |
Correct |
26 ms |
38656 KB |
Output is correct |
27 |
Correct |
26 ms |
38656 KB |
Output is correct |
28 |
Correct |
27 ms |
38656 KB |
Output is correct |
29 |
Correct |
27 ms |
38656 KB |
Output is correct |
30 |
Correct |
30 ms |
38784 KB |
Output is correct |
31 |
Correct |
27 ms |
38656 KB |
Output is correct |
32 |
Correct |
26 ms |
38648 KB |
Output is correct |
33 |
Correct |
26 ms |
38656 KB |
Output is correct |
34 |
Correct |
28 ms |
38656 KB |
Output is correct |
35 |
Correct |
27 ms |
38656 KB |
Output is correct |
36 |
Correct |
26 ms |
38656 KB |
Output is correct |
37 |
Correct |
26 ms |
38656 KB |
Output is correct |
38 |
Correct |
61 ms |
43264 KB |
Output is correct |
39 |
Correct |
62 ms |
43256 KB |
Output is correct |
40 |
Correct |
68 ms |
43512 KB |
Output is correct |
41 |
Correct |
68 ms |
43512 KB |
Output is correct |
42 |
Correct |
71 ms |
44024 KB |
Output is correct |
43 |
Correct |
92 ms |
45048 KB |
Output is correct |
44 |
Correct |
74 ms |
45048 KB |
Output is correct |
45 |
Correct |
68 ms |
45048 KB |
Output is correct |
46 |
Correct |
71 ms |
44028 KB |
Output is correct |
47 |
Correct |
74 ms |
44920 KB |
Output is correct |
48 |
Correct |
56 ms |
45048 KB |
Output is correct |
49 |
Correct |
58 ms |
45048 KB |
Output is correct |
50 |
Correct |
85 ms |
45176 KB |
Output is correct |
51 |
Correct |
82 ms |
45176 KB |
Output is correct |
52 |
Correct |
78 ms |
44792 KB |
Output is correct |
53 |
Correct |
84 ms |
45176 KB |
Output is correct |
54 |
Correct |
88 ms |
45304 KB |
Output is correct |
55 |
Correct |
79 ms |
44792 KB |
Output is correct |
56 |
Correct |
1741 ms |
160112 KB |
Output is correct |
57 |
Correct |
1719 ms |
159980 KB |
Output is correct |
58 |
Correct |
1899 ms |
196788 KB |
Output is correct |
59 |
Correct |
1914 ms |
196800 KB |
Output is correct |
60 |
Correct |
2043 ms |
175828 KB |
Output is correct |
61 |
Correct |
1900 ms |
196844 KB |
Output is correct |
62 |
Correct |
1745 ms |
160112 KB |
Output is correct |
63 |
Correct |
1740 ms |
206992 KB |
Output is correct |
64 |
Correct |
1759 ms |
206884 KB |
Output is correct |
65 |
Correct |
324 ms |
59504 KB |
Output is correct |
66 |
Correct |
1950 ms |
175600 KB |
Output is correct |
67 |
Correct |
2658 ms |
205420 KB |
Output is correct |
68 |
Correct |
2290 ms |
206700 KB |
Output is correct |
69 |
Correct |
1866 ms |
205680 KB |
Output is correct |
70 |
Correct |
2919 ms |
205808 KB |
Output is correct |
71 |
Correct |
2351 ms |
206708 KB |
Output is correct |
72 |
Correct |
1845 ms |
206828 KB |
Output is correct |
73 |
Correct |
2981 ms |
205748 KB |
Output is correct |
74 |
Correct |
1395 ms |
206704 KB |
Output is correct |
75 |
Correct |
1244 ms |
206700 KB |
Output is correct |
76 |
Execution timed out |
3096 ms |
201056 KB |
Time limit exceeded |
77 |
Halted |
0 ms |
0 KB |
- |