#include "walk.h"
#include <bits/stdc++.h>
using namespace std;
#define sp " "
#define endl "\n"
#define pb push_back
#define pii pair<ll, ll>
#define st first
#define nd second
#define fileio() freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define fastio() cin.tie(0), ios_base::sync_with_stdio(0)
#define LL node * 2
#define RR node * 2 + 1
#define ll long long
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 s, int g) {
ll n = x.size(), m = l.size();
vector<map<ll, long long>> dist(n);
vector<vector<pii>> adj(n);
vector<vector<ll>> polls(n);
vector<array<ll, 3>> events;
vector<ll> last(m, -1);
set<pii> open;
for (ll i = 0; i < m; i++)
events.pb({l[i], i, -1}), events.pb({r[i] + 1, i, 1});
sort(events.begin(), events.end());
ll it = 0;
for (ll i = 0; i < n; i++){
while(it < events.size() && events[it][0] == i){
ll t = events[it][2], ind = events[it][1];
if (t == -1) open.insert({-y[ind], ind});
else open.erase({-y[ind], ind});
it++;
}
auto it2 = open.lower_bound({-h[i], 0});
while(it2 != open.end()){
polls[i].pb(-it2->st);
ll ind = it2->nd;
if (last[ind] != -1){
adj[i].pb({y[ind], last[ind]});
adj[last[ind]].pb({y[ind], i});
}
last[ind] = i;
it2++;
}
polls[i].pb(0);
}
for (ll i = 0; i < n; i++){
sort(polls[i].begin(), polls[i].end());
sort(adj[i].begin(), adj[i].end());
}
priority_queue<array<long long, 3>> q;
q.push({0, s, 0});
dist[s][0] = 0;
/*
for (ll i = 0; i < n; i++){
cout<<i<<" : ";
for (auto j : polls[i]) cout<<j<<sp;
cout<<endl;
}*/
while(!q.empty()){
array<ll, 3> tmp = q.top();
q.pop();
long long X = tmp[1], y = tmp[2], d = -tmp[0];
if (dist[X][y] < d) continue;
//cout<<X<<sp<<y<<sp<<d<<endl;
ll pos = lower_bound(polls[X].begin(), polls[X].end(), y) - polls[X].begin();
if (pos > 0){
ll to = polls[X][pos - 1];
ll tmp = d + abs(to - y);
if (dist[X].count(to) == 0 || dist[X][to] > tmp){
dist[X][to] = tmp;
q.push({-tmp, X, to});
}
}
if (pos < polls[X].size()){
ll to = polls[X][pos + 1];
ll tmp = d + abs(to - y);
if (dist[X].count(to) == 0 || dist[X][to] > tmp){
dist[X][to] = tmp;
q.push({-tmp, X, to});
}
}
pos = lower_bound(adj[X].begin(), adj[X].end(), make_pair(y, (ll)0)) - adj[X].begin();
while(pos < adj[X].size() && adj[X][pos].st == y){
ll to = adj[X][pos].nd;
ll tmp = d + abs(x[X] - x[to]);
if (dist[to].count(y) == 0 || dist[to][y] > tmp){
dist[to][y] = tmp;
q.push({-tmp, to, y});
}
pos++;
}
}
if (dist[g].count(0)) return dist[g][0];
return -1;
}
/*
int main() {
fileio();
int n, m;
assert(2 == scanf("%d%d", &n, &m));
vector<int> x(n), h(n);
for (int i = 0; i < n; i++)
assert(2 == scanf("%d%d", &x[i], &h[i]));
vector<int> l(m), r(m), y(m);
for (int i = 0; i < m; i++)
assert(3 == scanf("%d%d%d", &l[i], &r[i], &y[i]));
int s, g;
assert(2 == scanf("%d%d", &s, &g));
fclose(stdin);
long long result = min_distance(x, h, l, r, y, s, g);
printf("%lld\n", result);
fclose(stdout);
return 0;
}
*/
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:37:12: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | while(it < events.size() && events[it][0] == i){
| ~~~^~~~~~~~~~~~~~~
walk.cpp:87:11: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | if (pos < polls[X].size()){
| ~~~~^~~~~~~~~~~~~~~~~
walk.cpp:97:13: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
97 | while(pos < adj[X].size() && adj[X][pos].st == y){
| ~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
496 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
554 ms |
91548 KB |
Output is correct |
4 |
Correct |
324 ms |
106936 KB |
Output is correct |
5 |
Correct |
101 ms |
50732 KB |
Output is correct |
6 |
Correct |
106 ms |
48252 KB |
Output is correct |
7 |
Correct |
100 ms |
50716 KB |
Output is correct |
8 |
Correct |
833 ms |
113408 KB |
Output is correct |
9 |
Correct |
146 ms |
56256 KB |
Output is correct |
10 |
Correct |
537 ms |
144320 KB |
Output is correct |
11 |
Correct |
200 ms |
53952 KB |
Output is correct |
12 |
Correct |
94 ms |
48316 KB |
Output is correct |
13 |
Correct |
398 ms |
133308 KB |
Output is correct |
14 |
Correct |
78 ms |
29116 KB |
Output is correct |
15 |
Correct |
111 ms |
49592 KB |
Output is correct |
16 |
Incorrect |
108 ms |
48064 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
13764 KB |
Output is correct |
2 |
Execution timed out |
4089 ms |
363680 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
13764 KB |
Output is correct |
2 |
Execution timed out |
4089 ms |
363680 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
496 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
554 ms |
91548 KB |
Output is correct |
21 |
Correct |
324 ms |
106936 KB |
Output is correct |
22 |
Correct |
101 ms |
50732 KB |
Output is correct |
23 |
Correct |
106 ms |
48252 KB |
Output is correct |
24 |
Correct |
100 ms |
50716 KB |
Output is correct |
25 |
Correct |
833 ms |
113408 KB |
Output is correct |
26 |
Correct |
146 ms |
56256 KB |
Output is correct |
27 |
Correct |
537 ms |
144320 KB |
Output is correct |
28 |
Correct |
200 ms |
53952 KB |
Output is correct |
29 |
Correct |
94 ms |
48316 KB |
Output is correct |
30 |
Correct |
398 ms |
133308 KB |
Output is correct |
31 |
Correct |
78 ms |
29116 KB |
Output is correct |
32 |
Correct |
111 ms |
49592 KB |
Output is correct |
33 |
Incorrect |
108 ms |
48064 KB |
Output isn't correct |
34 |
Halted |
0 ms |
0 KB |
- |