#include <bits/stdc++.h>
#include "walk.h"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const ll INFLL = 1e18;
long long min_distance(std::vector<int> x, std::vector<int> h, std::vector<int> l, std::vector<int> r, std::vector<int> y, int start, int end) {
int n = x.size(),
m = y.size();
vector<vector<int>> rmq(h.size(), vector<int> (18));
for(int i = 0 ; i < h.size() ; i++)
rmq[i][0] = h[i];
for(int i = 1 ; (1<<i) < n ; i++)
{
for(int j = 0 ; j + (1<<i) - 1 < n ; j++)
{
rmq[j][i] = max(rmq[j][i-1], rmq[j+(1<<(i-1))][i-1]);
}
};
function<int(int, int)> query = [&](int l, int r)
{
int lg = __lg(r - l + 1);
return max(rmq[l][lg], rmq[r-(1<<lg)+1][lg]);
};
vector<pii> cc;
vector<vector<int>> d(n);
d[start].push_back(0);
d[end].push_back(0);
cc.push_back({x[start], 0});
cc.push_back({x[end], 0});
for(int i = 0 ; i < m ; i++)
{
int L = l[i];
cc.push_back({x[l[i]], y[i]});
d[l[i]].push_back(y[i]);
while(l[i] < r[i])
{
for(int j = 18 ; j >= 0 ; j--)
{
int target = l[i] + (1<<j);
if(target <= r[i] and query(l[i]+1, target) < y[i])
l[i] = target;
}
l[i]++;
cc.push_back({x[l[i]], y[i]});
d[l[i]].push_back(y[i]);
}
l[i] = L;
}
sort(cc.begin(), cc.end());
cc.resize(unique(cc.begin(), cc.end()) - cc.begin());
function<int(int, int)> f = [&](int a, int b)
{
return lower_bound(cc.begin(), cc.end(), (pii){a, b}) - cc.begin();
};
vector<vector<pair<int, ll>>> g(cc.size());
for(int i = 0 ; i < m ; i++)
{
int last = l[i];
while(l[i] < r[i])
{
for(int j = 18 ; j >= 0 ; j--)
{
int target = l[i] + (1<<j);
if(target <= r[i] and query(l[i]+1, target) < y[i])
l[i] = target;
}
l[i]++;
int a = f(x[last], y[i]),
b = f(x[l[i]], y[i]),
w = x[l[i]] - x[last];
g[a].push_back({b, w});
g[b].push_back({a, w});
last = l[i];
}
}
for(int i = 0 ; i < n ; i++)
{
sort(d[i].begin(), d[i].end());
for(int j = 0 ; j + 1 < d[i].size() ; j++)
{
int a = f(x[i], d[i][j]),
b = f(x[i], d[i][j+1]),
w = d[i][j+1] - d[i][j];
g[a].push_back({b, w});
g[b].push_back({a, w});
}
}
priority_queue<pair<ll, int>> q;
vector<ll> dp(cc.size(), INFLL);
vector<bool> flag(cc.size());
int s = f(x[start], 0);
dp[s] = 0;
q.push({0, s});
while(q.size())
{
int nod = q.top().second;
q.pop();
if(flag[nod])
continue;
flag[nod] = true;
for(auto i: g[nod])
{
if(dp[i.first] > dp[nod] + i.second)
{
dp[i.first] = dp[nod] + i.second;
q.push({-dp[i.first], i.first});
}
}
}
int e = f(x[end], 0);
return (dp[e] == INFLL ? -1 : dp[e]);
}
Compilation message
walk.cpp: In function 'long long int min_distance(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, int, int)':
walk.cpp:17:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | for(int i = 0 ; i < h.size() ; i++)
| ~~^~~~~~~~~~
walk.cpp:104:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
104 | for(int j = 0 ; j + 1 < d[i].size() ; j++)
| ~~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
292 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
2 ms |
424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1155 ms |
95788 KB |
Output is correct |
4 |
Correct |
1154 ms |
110128 KB |
Output is correct |
5 |
Correct |
804 ms |
96104 KB |
Output is correct |
6 |
Correct |
729 ms |
85804 KB |
Output is correct |
7 |
Correct |
904 ms |
96136 KB |
Output is correct |
8 |
Correct |
1471 ms |
121160 KB |
Output is correct |
9 |
Correct |
889 ms |
94152 KB |
Output is correct |
10 |
Correct |
1618 ms |
148104 KB |
Output is correct |
11 |
Correct |
652 ms |
55824 KB |
Output is correct |
12 |
Correct |
337 ms |
42676 KB |
Output is correct |
13 |
Correct |
1352 ms |
131852 KB |
Output is correct |
14 |
Correct |
430 ms |
41428 KB |
Output is correct |
15 |
Correct |
431 ms |
41984 KB |
Output is correct |
16 |
Correct |
373 ms |
42340 KB |
Output is correct |
17 |
Correct |
357 ms |
41036 KB |
Output is correct |
18 |
Correct |
344 ms |
48024 KB |
Output is correct |
19 |
Correct |
10 ms |
2120 KB |
Output is correct |
20 |
Correct |
96 ms |
19840 KB |
Output is correct |
21 |
Correct |
323 ms |
38712 KB |
Output is correct |
22 |
Correct |
344 ms |
41776 KB |
Output is correct |
23 |
Correct |
468 ms |
51000 KB |
Output is correct |
24 |
Correct |
377 ms |
41752 KB |
Output is correct |
25 |
Correct |
347 ms |
41076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
143 ms |
15540 KB |
Output is correct |
2 |
Execution timed out |
4057 ms |
313036 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
143 ms |
15540 KB |
Output is correct |
2 |
Execution timed out |
4057 ms |
313036 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
292 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
2 ms |
424 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1155 ms |
95788 KB |
Output is correct |
21 |
Correct |
1154 ms |
110128 KB |
Output is correct |
22 |
Correct |
804 ms |
96104 KB |
Output is correct |
23 |
Correct |
729 ms |
85804 KB |
Output is correct |
24 |
Correct |
904 ms |
96136 KB |
Output is correct |
25 |
Correct |
1471 ms |
121160 KB |
Output is correct |
26 |
Correct |
889 ms |
94152 KB |
Output is correct |
27 |
Correct |
1618 ms |
148104 KB |
Output is correct |
28 |
Correct |
652 ms |
55824 KB |
Output is correct |
29 |
Correct |
337 ms |
42676 KB |
Output is correct |
30 |
Correct |
1352 ms |
131852 KB |
Output is correct |
31 |
Correct |
430 ms |
41428 KB |
Output is correct |
32 |
Correct |
431 ms |
41984 KB |
Output is correct |
33 |
Correct |
373 ms |
42340 KB |
Output is correct |
34 |
Correct |
357 ms |
41036 KB |
Output is correct |
35 |
Correct |
344 ms |
48024 KB |
Output is correct |
36 |
Correct |
10 ms |
2120 KB |
Output is correct |
37 |
Correct |
96 ms |
19840 KB |
Output is correct |
38 |
Correct |
323 ms |
38712 KB |
Output is correct |
39 |
Correct |
344 ms |
41776 KB |
Output is correct |
40 |
Correct |
468 ms |
51000 KB |
Output is correct |
41 |
Correct |
377 ms |
41752 KB |
Output is correct |
42 |
Correct |
347 ms |
41076 KB |
Output is correct |
43 |
Correct |
143 ms |
15540 KB |
Output is correct |
44 |
Execution timed out |
4057 ms |
313036 KB |
Time limit exceeded |
45 |
Halted |
0 ms |
0 KB |
- |