#include <bits/stdc++.h>
using namespace std;
const int oo = 2e9;
struct Tree {
typedef pair<int, int> T;
static constexpr T unit = {-oo, -oo};
T f(T a, T b) { return max(a, b); }
vector<T> s;
int n;
Tree(int nn) : s(2 * nn, unit), n(nn) {}
void update(int pos, T val)
{
for(s[pos += n] = val; pos /= 2;) {
s[pos] = f(s[pos * 2], s[pos * 2 + 1]);
}
}
T query(int b, int e)
{
T ra = unit, rb = unit;
for(b += n, e += n + 1; b < e; b /= 2, e /= 2) {
if(b % 2) ra = f(ra, s[b++]);
if(e % 2) rb = f(s[--e], rb);
}
return f(ra, rb);
}
};
struct treatment {
int t, l, r, c;
pair<int, int> pocz, kon;
void read()
{
cin >> t >> l >> r >> c;
r++;
pocz.first = l + t;
pocz.second = t - l;
kon.first = r + t;
kon.second = t - r;
}
bool operator<(const treatment &he) const
{
return pocz.first < he.pocz.first;
}
};
int main()
{
cin.tie(nullptr)->sync_with_stdio(0);
int n, m;
cin >> n >> m;
vector<treatment> they(m + 1);
for(int i = 1; i <= m; i++) {
they[i].read();
}
sort(they.begin() + 1, they.end());
Tree tree(m + 123);
#define T pair<long long, int>
priority_queue<T, vector<T>, greater<T>> q;
const long long ool = 1e18 + 5;
vector<long long> d(m + 1);
for(int i = 1; i <= m; i++) {
if(they[i].l == 1) {
d[i] = they[i].c;
q.push({d[i], i});
}
else {
d[i] = ool;
tree.update(i, {they[i].pocz.second, i});
}
}
while(!q.empty()) {
pair<long long, int> t = q.top();
q.pop();
assert(t.first == d[t.second]);
while(true) {
int lo = 1, hi = m, ans = 0;
while(lo <= hi) {
int mid = (lo + hi) / 2;
if(they[t.second].kon.first < they[mid].pocz.first) {
hi = mid - 1;
}
else {
lo = mid + 1;
ans = mid;
}
}
pair<int, int> maybe = tree.query(1, ans);
if(maybe.first < they[t.second].kon.second) {
break;
}
assert(d[maybe.second] > they[maybe.second].c + t.first);
q.emplace(d[maybe.second] = they[maybe.second].c + t.first, maybe.second);
tree.update(maybe.second, {-oo, -oo});
}
}
long long ans = ool;
for(int i = 1; i <= m; i++) {
if(they[i].r == n + 1) {
ans = min(ans, d[i]);
}
}
cout << (ans == ool ? -1 : ans) << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
99 ms |
5748 KB |
Output is correct |
2 |
Correct |
78 ms |
8944 KB |
Output is correct |
3 |
Correct |
94 ms |
8564 KB |
Output is correct |
4 |
Correct |
90 ms |
8604 KB |
Output is correct |
5 |
Correct |
114 ms |
11076 KB |
Output is correct |
6 |
Correct |
105 ms |
9160 KB |
Output is correct |
7 |
Correct |
99 ms |
8780 KB |
Output is correct |
8 |
Correct |
63 ms |
8896 KB |
Output is correct |
9 |
Correct |
57 ms |
8812 KB |
Output is correct |
10 |
Correct |
62 ms |
8812 KB |
Output is correct |
11 |
Correct |
127 ms |
11088 KB |
Output is correct |
12 |
Correct |
128 ms |
11148 KB |
Output is correct |
13 |
Correct |
132 ms |
10988 KB |
Output is correct |
14 |
Correct |
134 ms |
11068 KB |
Output is correct |
15 |
Correct |
106 ms |
8964 KB |
Output is correct |
16 |
Correct |
107 ms |
8948 KB |
Output is correct |
17 |
Correct |
101 ms |
8148 KB |
Output is correct |
18 |
Correct |
113 ms |
10356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
252 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
252 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
4 ms |
596 KB |
Output is correct |
21 |
Correct |
4 ms |
596 KB |
Output is correct |
22 |
Correct |
5 ms |
596 KB |
Output is correct |
23 |
Correct |
5 ms |
596 KB |
Output is correct |
24 |
Correct |
6 ms |
724 KB |
Output is correct |
25 |
Correct |
6 ms |
596 KB |
Output is correct |
26 |
Correct |
6 ms |
596 KB |
Output is correct |
27 |
Correct |
5 ms |
596 KB |
Output is correct |
28 |
Correct |
6 ms |
724 KB |
Output is correct |
29 |
Correct |
6 ms |
596 KB |
Output is correct |
30 |
Correct |
3 ms |
596 KB |
Output is correct |
31 |
Correct |
4 ms |
596 KB |
Output is correct |
32 |
Correct |
6 ms |
724 KB |
Output is correct |
33 |
Correct |
5 ms |
724 KB |
Output is correct |
34 |
Correct |
6 ms |
596 KB |
Output is correct |
35 |
Correct |
6 ms |
724 KB |
Output is correct |
36 |
Correct |
5 ms |
724 KB |
Output is correct |
37 |
Correct |
5 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
99 ms |
5748 KB |
Output is correct |
2 |
Correct |
78 ms |
8944 KB |
Output is correct |
3 |
Correct |
94 ms |
8564 KB |
Output is correct |
4 |
Correct |
90 ms |
8604 KB |
Output is correct |
5 |
Correct |
114 ms |
11076 KB |
Output is correct |
6 |
Correct |
105 ms |
9160 KB |
Output is correct |
7 |
Correct |
99 ms |
8780 KB |
Output is correct |
8 |
Correct |
63 ms |
8896 KB |
Output is correct |
9 |
Correct |
57 ms |
8812 KB |
Output is correct |
10 |
Correct |
62 ms |
8812 KB |
Output is correct |
11 |
Correct |
127 ms |
11088 KB |
Output is correct |
12 |
Correct |
128 ms |
11148 KB |
Output is correct |
13 |
Correct |
132 ms |
10988 KB |
Output is correct |
14 |
Correct |
134 ms |
11068 KB |
Output is correct |
15 |
Correct |
106 ms |
8964 KB |
Output is correct |
16 |
Correct |
107 ms |
8948 KB |
Output is correct |
17 |
Correct |
101 ms |
8148 KB |
Output is correct |
18 |
Correct |
113 ms |
10356 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
252 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
4 ms |
596 KB |
Output is correct |
39 |
Correct |
4 ms |
596 KB |
Output is correct |
40 |
Correct |
5 ms |
596 KB |
Output is correct |
41 |
Correct |
5 ms |
596 KB |
Output is correct |
42 |
Correct |
6 ms |
724 KB |
Output is correct |
43 |
Correct |
6 ms |
596 KB |
Output is correct |
44 |
Correct |
6 ms |
596 KB |
Output is correct |
45 |
Correct |
5 ms |
596 KB |
Output is correct |
46 |
Correct |
6 ms |
724 KB |
Output is correct |
47 |
Correct |
6 ms |
596 KB |
Output is correct |
48 |
Correct |
3 ms |
596 KB |
Output is correct |
49 |
Correct |
4 ms |
596 KB |
Output is correct |
50 |
Correct |
6 ms |
724 KB |
Output is correct |
51 |
Correct |
5 ms |
724 KB |
Output is correct |
52 |
Correct |
6 ms |
596 KB |
Output is correct |
53 |
Correct |
6 ms |
724 KB |
Output is correct |
54 |
Correct |
5 ms |
724 KB |
Output is correct |
55 |
Correct |
5 ms |
596 KB |
Output is correct |
56 |
Correct |
101 ms |
8948 KB |
Output is correct |
57 |
Correct |
112 ms |
9880 KB |
Output is correct |
58 |
Correct |
108 ms |
8052 KB |
Output is correct |
59 |
Correct |
114 ms |
8524 KB |
Output is correct |
60 |
Correct |
114 ms |
9156 KB |
Output is correct |
61 |
Correct |
110 ms |
8012 KB |
Output is correct |
62 |
Correct |
98 ms |
8944 KB |
Output is correct |
63 |
Correct |
110 ms |
8940 KB |
Output is correct |
64 |
Correct |
119 ms |
8948 KB |
Output is correct |
65 |
Correct |
106 ms |
9200 KB |
Output is correct |
66 |
Correct |
113 ms |
9204 KB |
Output is correct |
67 |
Correct |
147 ms |
9708 KB |
Output is correct |
68 |
Correct |
146 ms |
9584 KB |
Output is correct |
69 |
Correct |
138 ms |
9632 KB |
Output is correct |
70 |
Correct |
180 ms |
9820 KB |
Output is correct |
71 |
Correct |
154 ms |
9680 KB |
Output is correct |
72 |
Correct |
128 ms |
9580 KB |
Output is correct |
73 |
Correct |
151 ms |
9876 KB |
Output is correct |
74 |
Correct |
75 ms |
9628 KB |
Output is correct |
75 |
Correct |
65 ms |
9584 KB |
Output is correct |
76 |
Correct |
145 ms |
11512 KB |
Output is correct |
77 |
Correct |
152 ms |
11880 KB |
Output is correct |
78 |
Correct |
156 ms |
11404 KB |
Output is correct |
79 |
Correct |
164 ms |
9708 KB |
Output is correct |
80 |
Correct |
126 ms |
9320 KB |
Output is correct |
81 |
Correct |
76 ms |
9712 KB |
Output is correct |
82 |
Correct |
114 ms |
8740 KB |
Output is correct |
83 |
Correct |
125 ms |
8920 KB |
Output is correct |
84 |
Correct |
144 ms |
8908 KB |
Output is correct |
85 |
Correct |
103 ms |
9580 KB |
Output is correct |
86 |
Correct |
117 ms |
9580 KB |
Output is correct |
87 |
Correct |
105 ms |
9584 KB |
Output is correct |
88 |
Correct |
108 ms |
9692 KB |
Output is correct |
89 |
Correct |
106 ms |
9580 KB |
Output is correct |
90 |
Correct |
151 ms |
11784 KB |
Output is correct |
91 |
Correct |
118 ms |
10740 KB |
Output is correct |
92 |
Correct |
121 ms |
9580 KB |
Output is correct |
93 |
Correct |
135 ms |
9580 KB |
Output is correct |
94 |
Correct |
124 ms |
9844 KB |
Output is correct |
95 |
Correct |
125 ms |
9584 KB |
Output is correct |
96 |
Correct |
149 ms |
11760 KB |
Output is correct |
97 |
Correct |
154 ms |
11848 KB |
Output is correct |
98 |
Correct |
154 ms |
11884 KB |
Output is correct |
99 |
Correct |
144 ms |
11796 KB |
Output is correct |
100 |
Correct |
132 ms |
11768 KB |
Output is correct |
101 |
Correct |
143 ms |
11764 KB |
Output is correct |
102 |
Correct |
140 ms |
11824 KB |
Output is correct |
103 |
Correct |
104 ms |
9972 KB |
Output is correct |