#include "walk.h"
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const ll INF=1e18+7;
const int LIM=1e5+7;
vector<pair<ll,ll>>V[10*LIM];
set<pair<ll,ll>>S;
map<pair<ll,ll>,ll>mp;
ll tr[4*LIM], odl[10*LIM], n, N=1, akt;
void upd(int l, int r, ll x) {
l+=N; r+=N;
tr[l]=max(tr[l], x);
tr[r]=max(tr[r], x);
while(l/2!=r/2) {
if(l%2==0) tr[l+1]=max(tr[l+1], x);
if(r%2==1) tr[r-1]=max(tr[r-1], x);
l/=2; r/=2;
}
}
ll cnt(int v) {
v+=N;
ll ans=tr[v];
while(v) {
ans=max(ans, tr[v]);
v/=2;
}
return ans;
}
void kraw(pair<ll,ll>a, pair<ll,ll>b) {
if(mp.find(a)==mp.end()) {
mp[a]=akt++;
S.insert({a.nd, a.st});
}
if(mp.find(b)==mp.end()) {
mp[b]=akt++;
if(S.find({b.nd, b.st})==S.end() && b.nd!=0) {
auto it=S.lower_bound({b.nd, b.st});
if(it!=S.end()) {
auto x=*it;
if(x.st==b.nd) kraw(b, {x.nd, x.st});
}
if(it!=S.begin()) {
--it;
auto x=*it;
if(x.st==b.nd) kraw(b, {x.nd, x.st});
}
}
S.insert({b.nd, b.st});
}
ll o=abs(a.st-b.st)+abs(a.nd-b.nd);
V[mp[a]].pb({mp[b], o});
V[mp[b]].pb({mp[a], o});
}
ll min_distance(vector<int>x, vector<int>h, vector<int>l, vector<int>r, vector<int>y, int s, int g) {
n=x.size();
while(N<n) N*=2;
vector<pair<ll,pair<ll,ll>>>P;
rep(i, l.size()) P.pb({y[i], {l[i], r[i]}});
mp[{x[s], 0}]=0;
mp[{x[g], 0}]=1;
akt=2;
sort(all(P));
for(auto i : P) {
kraw({x[i.nd.st], i.st}, {x[i.nd.nd], i.st});
kraw({x[i.nd.st], i.st}, {x[i.nd.st], cnt(i.nd.st)});
kraw({x[i.nd.nd], i.st}, {x[i.nd.nd], cnt(i.nd.nd)});
upd(i.nd.st, i.nd.nd, i.st);
}
rep(i, akt+1) odl[i]=INF;
priority_queue<pair<ll,ll>>q;
q.push({0, 0});
while(!q.empty()) {
ll o=-q.top().st, p=q.top().nd; q.pop();
if(odl[p]<=o) continue;
odl[p]=o;
for(auto i : V[p]) q.push({-o-i.nd, i.st});
}
if(odl[1]==INF) return -1;
return odl[1];
}
Compilation message
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:5:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
5 | #define rep(a, b) for(int a = 0; a < (b); ++a)
| ^
walk.cpp:64:3: note: in expansion of macro 'rep'
64 | rep(i, l.size()) P.pb({y[i], {l[i], r[i]}});
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
15 ms |
23900 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
15 ms |
23896 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
180 ms |
44628 KB |
Output is correct |
2 |
Correct |
1280 ms |
120480 KB |
Output is correct |
3 |
Correct |
1415 ms |
122948 KB |
Output is correct |
4 |
Correct |
1569 ms |
127844 KB |
Output is correct |
5 |
Correct |
1574 ms |
134132 KB |
Output is correct |
6 |
Correct |
1538 ms |
127092 KB |
Output is correct |
7 |
Correct |
634 ms |
79484 KB |
Output is correct |
8 |
Correct |
704 ms |
93668 KB |
Output is correct |
9 |
Correct |
1234 ms |
124496 KB |
Output is correct |
10 |
Correct |
548 ms |
86392 KB |
Output is correct |
11 |
Correct |
24 ms |
25936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
180 ms |
44628 KB |
Output is correct |
2 |
Correct |
1280 ms |
120480 KB |
Output is correct |
3 |
Correct |
1415 ms |
122948 KB |
Output is correct |
4 |
Correct |
1569 ms |
127844 KB |
Output is correct |
5 |
Correct |
1574 ms |
134132 KB |
Output is correct |
6 |
Correct |
1538 ms |
127092 KB |
Output is correct |
7 |
Correct |
634 ms |
79484 KB |
Output is correct |
8 |
Correct |
704 ms |
93668 KB |
Output is correct |
9 |
Correct |
1234 ms |
124496 KB |
Output is correct |
10 |
Correct |
548 ms |
86392 KB |
Output is correct |
11 |
Correct |
24 ms |
25936 KB |
Output is correct |
12 |
Correct |
1528 ms |
122616 KB |
Output is correct |
13 |
Correct |
1130 ms |
126208 KB |
Output is correct |
14 |
Correct |
1458 ms |
133792 KB |
Output is correct |
15 |
Correct |
1211 ms |
123776 KB |
Output is correct |
16 |
Correct |
1072 ms |
115300 KB |
Output is correct |
17 |
Correct |
1319 ms |
130096 KB |
Output is correct |
18 |
Correct |
1194 ms |
123996 KB |
Output is correct |
19 |
Correct |
990 ms |
112956 KB |
Output is correct |
20 |
Correct |
650 ms |
78104 KB |
Output is correct |
21 |
Correct |
38 ms |
29780 KB |
Output is correct |
22 |
Correct |
862 ms |
105520 KB |
Output is correct |
23 |
Correct |
795 ms |
103872 KB |
Output is correct |
24 |
Correct |
629 ms |
93076 KB |
Output is correct |
25 |
Correct |
755 ms |
99652 KB |
Output is correct |
26 |
Correct |
534 ms |
89456 KB |
Output is correct |
27 |
Correct |
1591 ms |
134916 KB |
Output is correct |
28 |
Correct |
1232 ms |
125180 KB |
Output is correct |
29 |
Correct |
1457 ms |
126640 KB |
Output is correct |
30 |
Correct |
666 ms |
79164 KB |
Output is correct |
31 |
Correct |
1247 ms |
120528 KB |
Output is correct |
32 |
Correct |
491 ms |
86968 KB |
Output is correct |
33 |
Correct |
466 ms |
89560 KB |
Output is correct |
34 |
Correct |
607 ms |
94912 KB |
Output is correct |
35 |
Correct |
565 ms |
97852 KB |
Output is correct |
36 |
Correct |
636 ms |
92828 KB |
Output is correct |
37 |
Correct |
527 ms |
87736 KB |
Output is correct |
38 |
Correct |
542 ms |
92868 KB |
Output is correct |
39 |
Correct |
646 ms |
104888 KB |
Output is correct |
40 |
Correct |
455 ms |
92312 KB |
Output is correct |
41 |
Correct |
526 ms |
89352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
15 ms |
23900 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |