#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];
priority_queue <pair <ll, int>, vector <pair <ll, int>>, greater <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 {
priority_queue <pii, vector <pii>, greater <pii>> t[N << 2];
void get(int v, int tl, int tr, int l, int r, int val, ll nw) {
if(tl > r || tr < l)
return;
if(tl >= l && tr <= r) {
while(!t[v].empty() && t[v].top().f <= val) {
int i = t[v].top().se;
if(d[i] > nw + a[i].c)
q.push(mp(d[i] = nw + a[i].c, i));
t[v].pop();
}
return;
}
int mid = tl + tr >> 1;
get(v << 1, tl, mid, l, r, val, nw);
get(v << 1 | 1, mid + 1, tr, l, r, val, nw);
}
inline void update(int v, int tl, int tr, int pos, pii val) {
while(1) {
t[v].push(val);
if(tl == tr)
break;
int mid = tl + tr >> 1;
if(pos <= mid)
v <<= 1, tr = mid;
else
v = v << 1 | 1, tl = mid + 1;
}
}
} 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.push({d[i], i});
}
else {
t1.update(1, 1, sz, gt(a[i].t), mp(a[i].l + a[i].t, i));
t2.update(1, 1, sz, gt(a[i].t), mp(a[i].l - a[i].t, i));
}
}
while(!q.empty()) {
int dist = q.top().f, i = q.top().se;
q.pop();
if(dist > d[i])
continue;
t1.get(1, 1, sz, gt(a[i].t), sz, a[i].r + a[i].t, d[i]);
t2.get(1, 1, sz, 1, gt(a[i].t), a[i].r - a[i].t, d[i]);
}
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, long long int)':
treatment.cpp:88: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>)':
treatment.cpp:99:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = tl + tr >> 1;
~~~^~~~
treatment.cpp: At global scope:
treatment.cpp:114:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main() {
^
treatment.cpp: In function 'int nxt()':
treatment.cpp:110: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 |
116 ms |
30572 KB |
Output is correct |
2 |
Correct |
112 ms |
30572 KB |
Output is correct |
3 |
Correct |
131 ms |
30568 KB |
Output is correct |
4 |
Correct |
130 ms |
30364 KB |
Output is correct |
5 |
Correct |
134 ms |
31884 KB |
Output is correct |
6 |
Correct |
128 ms |
30572 KB |
Output is correct |
7 |
Correct |
123 ms |
30568 KB |
Output is correct |
8 |
Correct |
88 ms |
30568 KB |
Output is correct |
9 |
Correct |
88 ms |
30572 KB |
Output is correct |
10 |
Correct |
88 ms |
30572 KB |
Output is correct |
11 |
Correct |
144 ms |
31852 KB |
Output is correct |
12 |
Correct |
142 ms |
31856 KB |
Output is correct |
13 |
Correct |
154 ms |
31956 KB |
Output is correct |
14 |
Correct |
153 ms |
31856 KB |
Output is correct |
15 |
Correct |
136 ms |
29980 KB |
Output is correct |
16 |
Correct |
134 ms |
30572 KB |
Output is correct |
17 |
Correct |
140 ms |
30580 KB |
Output is correct |
18 |
Correct |
139 ms |
31856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
26240 KB |
Output is correct |
2 |
Correct |
19 ms |
26240 KB |
Output is correct |
3 |
Correct |
19 ms |
26112 KB |
Output is correct |
4 |
Correct |
19 ms |
26112 KB |
Output is correct |
5 |
Correct |
19 ms |
26112 KB |
Output is correct |
6 |
Correct |
19 ms |
26112 KB |
Output is correct |
7 |
Correct |
19 ms |
26112 KB |
Output is correct |
8 |
Correct |
19 ms |
26112 KB |
Output is correct |
9 |
Correct |
19 ms |
26112 KB |
Output is correct |
10 |
Correct |
19 ms |
26112 KB |
Output is correct |
11 |
Correct |
19 ms |
26112 KB |
Output is correct |
12 |
Correct |
21 ms |
26112 KB |
Output is correct |
13 |
Correct |
19 ms |
26112 KB |
Output is correct |
14 |
Correct |
19 ms |
26240 KB |
Output is correct |
15 |
Correct |
19 ms |
26240 KB |
Output is correct |
16 |
Correct |
19 ms |
26112 KB |
Output is correct |
17 |
Correct |
19 ms |
26240 KB |
Output is correct |
18 |
Correct |
20 ms |
26112 KB |
Output is correct |
19 |
Correct |
19 ms |
26240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
26240 KB |
Output is correct |
2 |
Correct |
19 ms |
26240 KB |
Output is correct |
3 |
Correct |
19 ms |
26112 KB |
Output is correct |
4 |
Correct |
19 ms |
26112 KB |
Output is correct |
5 |
Correct |
19 ms |
26112 KB |
Output is correct |
6 |
Correct |
19 ms |
26112 KB |
Output is correct |
7 |
Correct |
19 ms |
26112 KB |
Output is correct |
8 |
Correct |
19 ms |
26112 KB |
Output is correct |
9 |
Correct |
19 ms |
26112 KB |
Output is correct |
10 |
Correct |
19 ms |
26112 KB |
Output is correct |
11 |
Correct |
19 ms |
26112 KB |
Output is correct |
12 |
Correct |
21 ms |
26112 KB |
Output is correct |
13 |
Correct |
19 ms |
26112 KB |
Output is correct |
14 |
Correct |
19 ms |
26240 KB |
Output is correct |
15 |
Correct |
19 ms |
26240 KB |
Output is correct |
16 |
Correct |
19 ms |
26112 KB |
Output is correct |
17 |
Correct |
19 ms |
26240 KB |
Output is correct |
18 |
Correct |
20 ms |
26112 KB |
Output is correct |
19 |
Correct |
19 ms |
26240 KB |
Output is correct |
20 |
Correct |
37 ms |
27520 KB |
Output is correct |
21 |
Correct |
37 ms |
27520 KB |
Output is correct |
22 |
Correct |
32 ms |
27512 KB |
Output is correct |
23 |
Correct |
30 ms |
27520 KB |
Output is correct |
24 |
Correct |
38 ms |
28024 KB |
Output is correct |
25 |
Correct |
38 ms |
28152 KB |
Output is correct |
26 |
Correct |
37 ms |
28160 KB |
Output is correct |
27 |
Correct |
36 ms |
28152 KB |
Output is correct |
28 |
Correct |
37 ms |
28032 KB |
Output is correct |
29 |
Correct |
38 ms |
28160 KB |
Output is correct |
30 |
Correct |
31 ms |
28160 KB |
Output is correct |
31 |
Correct |
31 ms |
28160 KB |
Output is correct |
32 |
Correct |
40 ms |
28408 KB |
Output is correct |
33 |
Correct |
40 ms |
28408 KB |
Output is correct |
34 |
Correct |
39 ms |
27904 KB |
Output is correct |
35 |
Correct |
42 ms |
28408 KB |
Output is correct |
36 |
Correct |
39 ms |
28416 KB |
Output is correct |
37 |
Correct |
39 ms |
27896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
116 ms |
30572 KB |
Output is correct |
2 |
Correct |
112 ms |
30572 KB |
Output is correct |
3 |
Correct |
131 ms |
30568 KB |
Output is correct |
4 |
Correct |
130 ms |
30364 KB |
Output is correct |
5 |
Correct |
134 ms |
31884 KB |
Output is correct |
6 |
Correct |
128 ms |
30572 KB |
Output is correct |
7 |
Correct |
123 ms |
30568 KB |
Output is correct |
8 |
Correct |
88 ms |
30568 KB |
Output is correct |
9 |
Correct |
88 ms |
30572 KB |
Output is correct |
10 |
Correct |
88 ms |
30572 KB |
Output is correct |
11 |
Correct |
144 ms |
31852 KB |
Output is correct |
12 |
Correct |
142 ms |
31856 KB |
Output is correct |
13 |
Correct |
154 ms |
31956 KB |
Output is correct |
14 |
Correct |
153 ms |
31856 KB |
Output is correct |
15 |
Correct |
136 ms |
29980 KB |
Output is correct |
16 |
Correct |
134 ms |
30572 KB |
Output is correct |
17 |
Correct |
140 ms |
30580 KB |
Output is correct |
18 |
Correct |
139 ms |
31856 KB |
Output is correct |
19 |
Correct |
19 ms |
26240 KB |
Output is correct |
20 |
Correct |
19 ms |
26240 KB |
Output is correct |
21 |
Correct |
19 ms |
26112 KB |
Output is correct |
22 |
Correct |
19 ms |
26112 KB |
Output is correct |
23 |
Correct |
19 ms |
26112 KB |
Output is correct |
24 |
Correct |
19 ms |
26112 KB |
Output is correct |
25 |
Correct |
19 ms |
26112 KB |
Output is correct |
26 |
Correct |
19 ms |
26112 KB |
Output is correct |
27 |
Correct |
19 ms |
26112 KB |
Output is correct |
28 |
Correct |
19 ms |
26112 KB |
Output is correct |
29 |
Correct |
19 ms |
26112 KB |
Output is correct |
30 |
Correct |
21 ms |
26112 KB |
Output is correct |
31 |
Correct |
19 ms |
26112 KB |
Output is correct |
32 |
Correct |
19 ms |
26240 KB |
Output is correct |
33 |
Correct |
19 ms |
26240 KB |
Output is correct |
34 |
Correct |
19 ms |
26112 KB |
Output is correct |
35 |
Correct |
19 ms |
26240 KB |
Output is correct |
36 |
Correct |
20 ms |
26112 KB |
Output is correct |
37 |
Correct |
19 ms |
26240 KB |
Output is correct |
38 |
Correct |
37 ms |
27520 KB |
Output is correct |
39 |
Correct |
37 ms |
27520 KB |
Output is correct |
40 |
Correct |
32 ms |
27512 KB |
Output is correct |
41 |
Correct |
30 ms |
27520 KB |
Output is correct |
42 |
Correct |
38 ms |
28024 KB |
Output is correct |
43 |
Correct |
38 ms |
28152 KB |
Output is correct |
44 |
Correct |
37 ms |
28160 KB |
Output is correct |
45 |
Correct |
36 ms |
28152 KB |
Output is correct |
46 |
Correct |
37 ms |
28032 KB |
Output is correct |
47 |
Correct |
38 ms |
28160 KB |
Output is correct |
48 |
Correct |
31 ms |
28160 KB |
Output is correct |
49 |
Correct |
31 ms |
28160 KB |
Output is correct |
50 |
Correct |
40 ms |
28408 KB |
Output is correct |
51 |
Correct |
40 ms |
28408 KB |
Output is correct |
52 |
Correct |
39 ms |
27904 KB |
Output is correct |
53 |
Correct |
42 ms |
28408 KB |
Output is correct |
54 |
Correct |
39 ms |
28416 KB |
Output is correct |
55 |
Correct |
39 ms |
27896 KB |
Output is correct |
56 |
Correct |
541 ms |
56424 KB |
Output is correct |
57 |
Correct |
533 ms |
56424 KB |
Output is correct |
58 |
Correct |
589 ms |
62952 KB |
Output is correct |
59 |
Correct |
617 ms |
63208 KB |
Output is correct |
60 |
Correct |
418 ms |
57576 KB |
Output is correct |
61 |
Correct |
593 ms |
63208 KB |
Output is correct |
62 |
Correct |
554 ms |
56424 KB |
Output is correct |
63 |
Correct |
628 ms |
69484 KB |
Output is correct |
64 |
Correct |
661 ms |
69644 KB |
Output is correct |
65 |
Correct |
147 ms |
32104 KB |
Output is correct |
66 |
Correct |
415 ms |
57584 KB |
Output is correct |
67 |
Correct |
751 ms |
69340 KB |
Output is correct |
68 |
Correct |
676 ms |
69348 KB |
Output is correct |
69 |
Correct |
601 ms |
68232 KB |
Output is correct |
70 |
Correct |
821 ms |
69456 KB |
Output is correct |
71 |
Correct |
647 ms |
69200 KB |
Output is correct |
72 |
Correct |
545 ms |
69352 KB |
Output is correct |
73 |
Correct |
767 ms |
69484 KB |
Output is correct |
74 |
Correct |
319 ms |
69224 KB |
Output is correct |
75 |
Correct |
296 ms |
69140 KB |
Output is correct |
76 |
Correct |
717 ms |
67560 KB |
Output is correct |
77 |
Correct |
802 ms |
71912 KB |
Output is correct |
78 |
Correct |
805 ms |
67724 KB |
Output is correct |
79 |
Correct |
921 ms |
69736 KB |
Output is correct |
80 |
Correct |
861 ms |
65508 KB |
Output is correct |
81 |
Correct |
522 ms |
69992 KB |
Output is correct |
82 |
Correct |
839 ms |
65640 KB |
Output is correct |
83 |
Correct |
930 ms |
69864 KB |
Output is correct |
84 |
Correct |
895 ms |
69864 KB |
Output is correct |
85 |
Correct |
707 ms |
69608 KB |
Output is correct |
86 |
Correct |
705 ms |
69584 KB |
Output is correct |
87 |
Correct |
780 ms |
69860 KB |
Output is correct |
88 |
Correct |
769 ms |
69608 KB |
Output is correct |
89 |
Correct |
763 ms |
69608 KB |
Output is correct |
90 |
Correct |
840 ms |
71788 KB |
Output is correct |
91 |
Correct |
794 ms |
71144 KB |
Output is correct |
92 |
Correct |
732 ms |
69860 KB |
Output is correct |
93 |
Correct |
881 ms |
69736 KB |
Output is correct |
94 |
Correct |
862 ms |
69736 KB |
Output is correct |
95 |
Correct |
844 ms |
69608 KB |
Output is correct |
96 |
Correct |
803 ms |
71784 KB |
Output is correct |
97 |
Correct |
788 ms |
71908 KB |
Output is correct |
98 |
Correct |
812 ms |
71872 KB |
Output is correct |
99 |
Correct |
805 ms |
71784 KB |
Output is correct |
100 |
Correct |
765 ms |
70504 KB |
Output is correct |
101 |
Correct |
818 ms |
71820 KB |
Output is correct |
102 |
Correct |
791 ms |
72016 KB |
Output is correct |
103 |
Correct |
683 ms |
70120 KB |
Output is correct |