#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct bheap {
vector<int> heap, mp;
vector<ll> val;
bheap() {}
bheap(int n) {
val.assign(n, 1e9);
mp.assign(n, -1);
}
void sink(int pos) {
while ((pos << 1) + 1 < heap.size()) {
int l = (pos << 1) + 1, r = (pos << 1) + 2;
int smallest = l;
if (r < heap.size() && val[heap[r]] < val[heap[l]]) smallest = r;
if (val[heap[smallest]] < val[heap[pos]]){
mp[heap[smallest]] = pos;
mp[heap[pos]] = smallest;
swap(heap[pos], heap[smallest]);
} else {
break;
}
pos = smallest;
}
}
bool swim(int pos) {
int p = (pos - 1) >> 1;
if (!(pos && val[heap[p]] > val[heap[pos]])) return 0;
while (pos&&val[heap[p]]>val[heap[pos]]) {
mp[heap[p]] = pos;
mp[heap[pos]] = p;
swap(heap[p], heap[pos]);
pos = p;
p = (pos - 1) >> 1;
}
return 1;
}
void push(int key, ll value) {
int pos = heap.size();
heap.push_back(key);
mp[key] = pos;
val[key] = value;
swim(pos);
}
int pop() {
int n = heap.size(), ret = heap[0];
mp[heap[0]] = n - 1;
mp[heap[n - 1]] = 0;
swap(heap[0], heap[n-1]);
heap.pop_back();
sink(0);
return ret;
}
void update(int key, ll value) {
int pos = mp[key];
val[key] = value;
if (!swim(pos)) {
sink(pos);
}
}
int top() {
return heap[0];
}
bool empty() {
return heap.empty();
}
};
vector<int> intersect[100'005];
vector<pair<int, int>> adj[100'005];
vector<ll> dist[100'005];
vector<int> idx[100'005], mp[100'005];
pair<int, int> rev[2'000'005];
int sparse[17][200'005];
const ll INF = 1e18;
ll min_distance(vector<int> x, vector<int> h, vector<int> l, vector<int> r, vector<int> y, int s, int g) {
int n = x.size(), m = l.size();
for (int i = 0; i < n; i++) {
sparse[0][i] = h[i];
}
for (int j = 1; j < 17; j++) {
for (int i = 0; i < n; i++) {
sparse[j][i] = max(sparse[j - 1][i], sparse[j - 1][i + (1 << (j - 1))]);
}
}
y.push_back(0);
idx[s].push_back(m);
dist[s].push_back(INF);
for (int i = 0; i < m; i++) {
int j = l[i];
while (j <= r[i]) {
if (y[i] <= h[j]) {
intersect[i].push_back(j);
j++;
continue;
}
int mx = 0;
for (int k = 16; k >= 0; k--) {
if (y[i] > max(mx, sparse[k][j])) {
mx = max(mx, sparse[k][j]);
j += (1 << k);
}
}
if (j <= r[i]) {
intersect[i].push_back(j);
j++;
}
}
for (int k = 0; k < intersect[i].size(); k++) {
dist[intersect[i][k]].push_back(INF);
idx[intersect[i][k]].push_back(i);
}
for (int k = 0; k < intersect[i].size(); k++) {
if (k + 1 < intersect[i].size()) {
adj[intersect[i][k]].push_back({intersect[i][k + 1], idx[intersect[i][k + 1]].size() - 1});
}
if (k - 1 >= 0) {
adj[intersect[i][k]].push_back({intersect[i][k - 1], idx[intersect[i][k - 1]].size() - 1});
}
}
}
int cur = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < idx[i].size(); j++) {
rev[cur] = {i, j};
mp[i].push_back(cur++);
}
}
priority_queue<pair<ll, int>> pq;
pq.push({0, mp[s][0]});
while (pq.size()) {
ll d = -pq.top().first;
int node = pq.top().second;
int i = rev[node].first;
int j = rev[node].second;
pq.pop();
if (d > dist[i][j]) continue;
j = y[idx[i][j]];
for (auto [nxt, nxth] : adj[i]) {
ll newd = d + abs(x[nxt] - x[i]) + abs(j - y[idx[nxt][nxth]]);
if (newd < dist[nxt][nxth]) {
dist[nxt][nxth] = newd;
pq.push({-newd, mp[nxt][nxth]});
}
}
}
ll ans = INF;
for (int i = 0; i < dist[g].size(); i++) {
ans = min(ans, dist[g][i] + y[idx[g][i]]);
}
// for (auto i : dist[g]) {
// ans = min(ans, i.first + i.second);
// }
if (ans >= INF) return -1;
return ans;
}
// int main() {
// ios_base::sync_with_stdio(0), cin.tie(0);
// int N, M;
// cin >> N >> M;
// vector<int> X(N), H(N);
// for (int i = 0; i < N; i++) {
// cin >> X[i] >> H[i];
// }
// vector<int> L(M), R(M), Y(M);
// for (int i = 0; i < M; i++) {
// cin >> L[i] >> R[i] >> Y[i];
// }
// int S, G;
// cin >> S >> G;
// cout << min_distance(X, H, L, R, Y, S, G) << '\n';
// return 0;
// }
Compilation message
walk.cpp: In member function 'void bheap::sink(int)':
walk.cpp:14:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
14 | while ((pos << 1) + 1 < heap.size()) {
| ~~~~~~~~~~~~~~~^~~~~~~~~~~~~
walk.cpp:17:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | if (r < heap.size() && val[heap[r]] < val[heap[l]]) smallest = r;
| ~~^~~~~~~~~~~~~
walk.cpp: In function 'll min_distance(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, int, int)':
walk.cpp:110:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
110 | for (int k = 0; k < intersect[i].size(); k++) {
| ~~^~~~~~~~~~~~~~~~~~~~~
walk.cpp:114:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
114 | for (int k = 0; k < intersect[i].size(); k++) {
| ~~^~~~~~~~~~~~~~~~~~~~~
walk.cpp:115:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
115 | if (k + 1 < intersect[i].size()) {
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~
walk.cpp:125:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
125 | for (int j = 0; j < idx[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~
walk.cpp:149:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
149 | for (int i = 0; i < dist[g].size(); i++) {
| ~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12116 KB |
Output is correct |
2 |
Correct |
6 ms |
12116 KB |
Output is correct |
3 |
Correct |
6 ms |
12116 KB |
Output is correct |
4 |
Correct |
6 ms |
12032 KB |
Output is correct |
5 |
Correct |
7 ms |
12116 KB |
Output is correct |
6 |
Correct |
6 ms |
12116 KB |
Output is correct |
7 |
Correct |
6 ms |
12116 KB |
Output is correct |
8 |
Correct |
6 ms |
12116 KB |
Output is correct |
9 |
Correct |
6 ms |
12080 KB |
Output is correct |
10 |
Correct |
8 ms |
12116 KB |
Output is correct |
11 |
Correct |
7 ms |
12116 KB |
Output is correct |
12 |
Correct |
6 ms |
12116 KB |
Output is correct |
13 |
Correct |
6 ms |
12116 KB |
Output is correct |
14 |
Correct |
6 ms |
12116 KB |
Output is correct |
15 |
Correct |
6 ms |
12116 KB |
Output is correct |
16 |
Correct |
6 ms |
12116 KB |
Output is correct |
17 |
Correct |
6 ms |
12116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12116 KB |
Output is correct |
2 |
Correct |
6 ms |
12104 KB |
Output is correct |
3 |
Correct |
1841 ms |
66648 KB |
Output is correct |
4 |
Correct |
480 ms |
66540 KB |
Output is correct |
5 |
Correct |
242 ms |
61396 KB |
Output is correct |
6 |
Correct |
220 ms |
57204 KB |
Output is correct |
7 |
Correct |
243 ms |
61424 KB |
Output is correct |
8 |
Correct |
3640 ms |
101192 KB |
Output is correct |
9 |
Correct |
326 ms |
59668 KB |
Output is correct |
10 |
Correct |
812 ms |
87336 KB |
Output is correct |
11 |
Correct |
232 ms |
39608 KB |
Output is correct |
12 |
Correct |
137 ms |
39876 KB |
Output is correct |
13 |
Correct |
652 ms |
83228 KB |
Output is correct |
14 |
Correct |
153 ms |
39532 KB |
Output is correct |
15 |
Correct |
204 ms |
39896 KB |
Output is correct |
16 |
Correct |
703 ms |
39292 KB |
Output is correct |
17 |
Correct |
167 ms |
38392 KB |
Output is correct |
18 |
Execution timed out |
4069 ms |
37276 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
21044 KB |
Output is correct |
2 |
Runtime error |
591 ms |
502284 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
21044 KB |
Output is correct |
2 |
Runtime error |
591 ms |
502284 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12116 KB |
Output is correct |
2 |
Correct |
6 ms |
12116 KB |
Output is correct |
3 |
Correct |
6 ms |
12116 KB |
Output is correct |
4 |
Correct |
6 ms |
12032 KB |
Output is correct |
5 |
Correct |
7 ms |
12116 KB |
Output is correct |
6 |
Correct |
6 ms |
12116 KB |
Output is correct |
7 |
Correct |
6 ms |
12116 KB |
Output is correct |
8 |
Correct |
6 ms |
12116 KB |
Output is correct |
9 |
Correct |
6 ms |
12080 KB |
Output is correct |
10 |
Correct |
8 ms |
12116 KB |
Output is correct |
11 |
Correct |
7 ms |
12116 KB |
Output is correct |
12 |
Correct |
6 ms |
12116 KB |
Output is correct |
13 |
Correct |
6 ms |
12116 KB |
Output is correct |
14 |
Correct |
6 ms |
12116 KB |
Output is correct |
15 |
Correct |
6 ms |
12116 KB |
Output is correct |
16 |
Correct |
6 ms |
12116 KB |
Output is correct |
17 |
Correct |
6 ms |
12116 KB |
Output is correct |
18 |
Correct |
7 ms |
12116 KB |
Output is correct |
19 |
Correct |
6 ms |
12104 KB |
Output is correct |
20 |
Correct |
1841 ms |
66648 KB |
Output is correct |
21 |
Correct |
480 ms |
66540 KB |
Output is correct |
22 |
Correct |
242 ms |
61396 KB |
Output is correct |
23 |
Correct |
220 ms |
57204 KB |
Output is correct |
24 |
Correct |
243 ms |
61424 KB |
Output is correct |
25 |
Correct |
3640 ms |
101192 KB |
Output is correct |
26 |
Correct |
326 ms |
59668 KB |
Output is correct |
27 |
Correct |
812 ms |
87336 KB |
Output is correct |
28 |
Correct |
232 ms |
39608 KB |
Output is correct |
29 |
Correct |
137 ms |
39876 KB |
Output is correct |
30 |
Correct |
652 ms |
83228 KB |
Output is correct |
31 |
Correct |
153 ms |
39532 KB |
Output is correct |
32 |
Correct |
204 ms |
39896 KB |
Output is correct |
33 |
Correct |
703 ms |
39292 KB |
Output is correct |
34 |
Correct |
167 ms |
38392 KB |
Output is correct |
35 |
Execution timed out |
4069 ms |
37276 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |