#include "walk.h"
#include<bits/stdc++.h>
using namespace std;
const int nmax=1e5+42;
set<int> there[nmax];
map<int,int> id[nmax];
vector<long long> dist;
vector< vector< pair<int,int> > > adj;
vector<int> inp;
void add_edge(int x1,int y1,int x2,int y2)
{
int p=id[x1][y1];
int q=id[x2][y2];
int d=abs(inp[x1]-inp[x2])+abs(y1-y2);
//cout<<"x1= "<<x1<<" y1= "<<y1<<" x2= "<<x2<<" y2= "<<y2<<" d= "<<d<<" p= "<<p<<" q= "<<q<<endl;
while(adj.size()<=p)adj.push_back({});
while(adj.size()<=q)adj.push_back({});
adj[p].push_back({q,d});
adj[q].push_back({p,d});
}
priority_queue< pair<long long,int> > pq;
int tree[4*nmax];
vector<int> mem_h,mem_l,mem_r,mem_y;
void build(int node,int l,int r)
{
if(l==r)
{
tree[node]=mem_h[l];
return;
}
int av=(l+r)/2;
build(node*2,l,av);
build(node*2+1,av+1,r);
tree[node]=max(tree[node*2],tree[node*2+1]);
}
void push(int i,int j)
{
//cout<<"push "<<i<<" "<<j<<endl;
if(mem_l[i]<=j&&j<=mem_r[i])
{
if(mem_h[j]>=mem_y[i])
{
//cout<<"in"<<endl;
there[j].insert(mem_y[i]);
}
}
}
int query(int node,int l,int r,int lq,int rq)
{
if(l==lq&&r==rq)return tree[node];
int av=(l+r)/2;
int ret=0;
if(lq<=av)ret=max(ret,query(node*2,l,av,lq,min(av,rq)));
if(av<rq)ret=max(ret,query(node*2+1,av+1,r,max(av+1,lq),rq));
return ret;
}
int get_first(int l,int r,int h)
{
if(l>r)return -1;
if(query(1,0,mem_h.size()-1,l,r)<h)return -1;
int ok=r,not_ok=l-1;
while(ok-not_ok>1)
{
int av=(ok+not_ok)/2;
if(query(1,0,mem_h.size()-1,l,av)<h)not_ok=av;
else ok=av;
}
return ok;
}
int get_last(int l,int r,int h)
{
if(l>r)return -1;
if(query(1,0,mem_h.size()-1,l,r)<h)return -1;
int ok=l,not_ok=r+1;
while(not_ok-ok>1)
{
int av=(ok+not_ok)/2;
if(query(1,0,mem_h.size()-1,av,r)<h)not_ok=av;
else ok=av;
}
return ok;
}
vector< pair<int,int> > updates[nmax];
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) {
mem_l=l;
mem_r=r;
mem_y=y;
if(s>g)swap(s,g);
inp=x;
mem_h=h;
build(1,0,h.size()-1);
for(int i=0;i<x.size();i++)
there[i].insert(0);
for(int i=0;i<y.size();i++)
{
updates[l[i]].push_back({y[i],1});
updates[r[i]+1].push_back({y[i],-1});
//cout<<"line: "<<l[i]<<" "<<r[i]<<" "<<y[i]<<endl;
/*
for(int j=l[i];j<=r[i];j++)
if(h[j]>=y[i])
{
//cout<<"push "<<j<<" h= "<<y[i]<<endl;
there[j].insert(y[i]);
noted[i].insert(j);
}
*/
push(i,l[i]);
push(i,get_last(0,l[i]-1,y[i]));
push(i,s);
push(i,get_first(s+1,g-1,y[i]));
push(i,get_last(s+1,g-1,y[i]));
push(i,g);
push(i,get_first(g+1,r[i],y[i]));
push(i,r[i]);
}
set<int> active={};
map<int,int> cnt_active={};
for(int i=0;i<x.size();i++)
{
for(auto w:updates[i])
{
cnt_active[w.first]+=w.second;
active.erase(w.first);
if(cnt_active[w.first])active.insert(w.first);
}
set<int> help=there[i];
for(auto w:help)
{
set<int>::iterator it=active.lower_bound(w);
if(it!=active.begin())
{
it--;
int val=*it;
if(0<=val&&val<=h[i])there[i].insert(val);
//cout<<"low "<<*it<<endl;
}
it=active.upper_bound(w);
if(it!=active.end())
{
int val=*it;
if(0<=val&&val<=h[i])there[i].insert(val);
//cout<<"high "<<*it<<endl;
}
}
}
int pointer=0;
for(int i=0;i<x.size();i++)
{
for(auto w:there[i])
{
id[i][w]=pointer;
//cout<<"id "<<i<<" "<<w<<" -> "<<pointer<<endl;
pointer++;
}
}
/*
cout<<"towers: "<<endl;
for(int i=0;i<x.size();i++)
{
cout<<"i= "<<i<<" : ";
for(auto w:there[i])
cout<<w<<" ";
cout<<endl;
}
*/
for(int i=0;i<x.size();i++)
{
int prv=0;
for(auto w:there[i])
{
if(w)
{
add_edge(i,prv,i,w);
}
prv=w;
}
}
//cout<<"-------------"<<endl;
map<int, vector<int> > seen_by_y={};
for(int i=0;i<x.size();i++)
{
for(auto w:there[i])
seen_by_y[w].push_back(i);
}
map<int, vector< pair<int,int> > > seen_updates_y={};
for(int i=0;i<y.size();i++)
seen_updates_y[y[i]].push_back({l[i],r[i]});
for(auto w:seen_by_y)
{
//cout<<l[i]<<" "<<r[i]<<" "<<y[i]<<" :"<<endl;
vector<int> me=w.second;
vector<int> pref=me;
for(auto &t:pref)
t=0;
int prv=-1;
for(auto u:seen_updates_y[w.first])
{
int p=lower_bound(me.begin(),me.end(),u.first)-me.begin();
int q=lower_bound(me.begin(),me.end(),u.second)-me.begin();
pref[p]++;
pref[q]--;
}
for(int i=1;i<pref.size();i++)
pref[i]+=pref[i-1];
for(int i=0;i+1<pref.size();i++)
{
if(pref[i])
add_edge(me[i],w.first,me[i+1],w.first);
}
}
/*
cout<<"there: "<<endl;
for(int i=0;i<x.size();i++)
{
cout<<i<<" -> ";for(auto j:there[i])cout<<j<<" ";cout<<endl;
}
*/
for(int i=0;i<=pointer;i++)
dist.push_back(-1);
pq.push({0,id[s][0]});
while(pq.size())
{
pair<long long,int> tp=pq.top();
pq.pop();
tp.first=-tp.first;
if(dist[tp.second]!=-1)continue;
dist[tp.second]=tp.first;
//cout<<"dist: "<<tp.second<<" -> "<<tp.first<<endl;
for(auto w:adj[tp.second])
pq.push({-(tp.first+w.second),w.first});
}
return dist[id[g][0]];
}
/*
int main() {
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 'void add_edge(int, int, int, int)':
walk.cpp:26:21: warning: comparison of integer expressions of different signedness: 'std::vector<std::vector<std::pair<int, int> > >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
26 | while(adj.size()<=p)adj.push_back({});
| ~~~~~~~~~~^~~
walk.cpp:27:21: warning: comparison of integer expressions of different signedness: 'std::vector<std::vector<std::pair<int, int> > >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
27 | while(adj.size()<=q)adj.push_back({});
| ~~~~~~~~~~^~~
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:133:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
133 | for(int i=0;i<x.size();i++)
| ~^~~~~~~~~
walk.cpp:136:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
136 | for(int i=0;i<y.size();i++)
| ~^~~~~~~~~
walk.cpp:170:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
170 | for(int i=0;i<x.size();i++)
| ~^~~~~~~~~
walk.cpp:213:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
213 | for(int i=0;i<x.size();i++)
| ~^~~~~~~~~
walk.cpp:239:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
239 | for(int i=0;i<x.size();i++)
| ~^~~~~~~~~
walk.cpp:258:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
258 | for(int i=0;i<x.size();i++)
| ~^~~~~~~~~
walk.cpp:266:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
266 | for(int i=0;i<y.size();i++)
| ~^~~~~~~~~
walk.cpp:292:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
292 | for(int i=1;i<pref.size();i++)
| ~^~~~~~~~~~~~
walk.cpp:295:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
295 | for(int i=0;i+1<pref.size();i++)
| ~~~^~~~~~~~~~~~
walk.cpp:281:13: warning: unused variable 'prv' [-Wunused-variable]
281 | int prv=-1;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
11980 KB |
Output is correct |
2 |
Correct |
7 ms |
11980 KB |
Output is correct |
3 |
Correct |
8 ms |
11980 KB |
Output is correct |
4 |
Correct |
7 ms |
11980 KB |
Output is correct |
5 |
Correct |
8 ms |
12044 KB |
Output is correct |
6 |
Correct |
9 ms |
12108 KB |
Output is correct |
7 |
Correct |
7 ms |
12108 KB |
Output is correct |
8 |
Correct |
7 ms |
12048 KB |
Output is correct |
9 |
Correct |
7 ms |
12068 KB |
Output is correct |
10 |
Correct |
8 ms |
12108 KB |
Output is correct |
11 |
Correct |
7 ms |
12048 KB |
Output is correct |
12 |
Correct |
7 ms |
12108 KB |
Output is correct |
13 |
Correct |
7 ms |
12044 KB |
Output is correct |
14 |
Correct |
7 ms |
12048 KB |
Output is correct |
15 |
Correct |
7 ms |
11980 KB |
Output is correct |
16 |
Correct |
7 ms |
11980 KB |
Output is correct |
17 |
Correct |
8 ms |
12108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11980 KB |
Output is correct |
2 |
Correct |
7 ms |
12048 KB |
Output is correct |
3 |
Correct |
1895 ms |
128324 KB |
Output is correct |
4 |
Correct |
2651 ms |
164364 KB |
Output is correct |
5 |
Correct |
1601 ms |
132540 KB |
Output is correct |
6 |
Correct |
1585 ms |
125244 KB |
Output is correct |
7 |
Correct |
1608 ms |
132984 KB |
Output is correct |
8 |
Correct |
2155 ms |
135320 KB |
Output is correct |
9 |
Correct |
2404 ms |
144620 KB |
Output is correct |
10 |
Correct |
2673 ms |
169056 KB |
Output is correct |
11 |
Correct |
2060 ms |
114976 KB |
Output is correct |
12 |
Correct |
1762 ms |
104220 KB |
Output is correct |
13 |
Correct |
2477 ms |
172516 KB |
Output is correct |
14 |
Correct |
1300 ms |
98540 KB |
Output is correct |
15 |
Correct |
1825 ms |
86556 KB |
Output is correct |
16 |
Correct |
1630 ms |
84752 KB |
Output is correct |
17 |
Correct |
1557 ms |
81356 KB |
Output is correct |
18 |
Correct |
869 ms |
105868 KB |
Output is correct |
19 |
Correct |
47 ms |
16512 KB |
Output is correct |
20 |
Correct |
588 ms |
57448 KB |
Output is correct |
21 |
Correct |
1409 ms |
77236 KB |
Output is correct |
22 |
Correct |
1421 ms |
83320 KB |
Output is correct |
23 |
Correct |
2019 ms |
109176 KB |
Output is correct |
24 |
Correct |
1484 ms |
83900 KB |
Output is correct |
25 |
Correct |
1447 ms |
80556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
443 ms |
30228 KB |
Output is correct |
2 |
Correct |
2005 ms |
153964 KB |
Output is correct |
3 |
Correct |
2575 ms |
162224 KB |
Output is correct |
4 |
Correct |
3374 ms |
200024 KB |
Output is correct |
5 |
Correct |
3417 ms |
199164 KB |
Output is correct |
6 |
Correct |
3371 ms |
195916 KB |
Output is correct |
7 |
Correct |
1624 ms |
129256 KB |
Output is correct |
8 |
Correct |
1812 ms |
104368 KB |
Output is correct |
9 |
Correct |
3342 ms |
192652 KB |
Output is correct |
10 |
Correct |
1718 ms |
117200 KB |
Output is correct |
11 |
Correct |
37 ms |
22608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
443 ms |
30228 KB |
Output is correct |
2 |
Correct |
2005 ms |
153964 KB |
Output is correct |
3 |
Correct |
2575 ms |
162224 KB |
Output is correct |
4 |
Correct |
3374 ms |
200024 KB |
Output is correct |
5 |
Correct |
3417 ms |
199164 KB |
Output is correct |
6 |
Correct |
3371 ms |
195916 KB |
Output is correct |
7 |
Correct |
1624 ms |
129256 KB |
Output is correct |
8 |
Correct |
1812 ms |
104368 KB |
Output is correct |
9 |
Correct |
3342 ms |
192652 KB |
Output is correct |
10 |
Correct |
1718 ms |
117200 KB |
Output is correct |
11 |
Correct |
37 ms |
22608 KB |
Output is correct |
12 |
Correct |
2649 ms |
163812 KB |
Output is correct |
13 |
Correct |
2755 ms |
204152 KB |
Output is correct |
14 |
Correct |
3379 ms |
203028 KB |
Output is correct |
15 |
Correct |
2298 ms |
150740 KB |
Output is correct |
16 |
Correct |
2407 ms |
164920 KB |
Output is correct |
17 |
Correct |
2680 ms |
189392 KB |
Output is correct |
18 |
Correct |
2354 ms |
150896 KB |
Output is correct |
19 |
Correct |
2425 ms |
165244 KB |
Output is correct |
20 |
Correct |
1596 ms |
129440 KB |
Output is correct |
21 |
Correct |
172 ms |
57584 KB |
Output is correct |
22 |
Correct |
2054 ms |
169192 KB |
Output is correct |
23 |
Correct |
1993 ms |
161096 KB |
Output is correct |
24 |
Correct |
1755 ms |
119992 KB |
Output is correct |
25 |
Correct |
1954 ms |
154844 KB |
Output is correct |
26 |
Correct |
1645 ms |
102360 KB |
Output is correct |
27 |
Correct |
3460 ms |
203244 KB |
Output is correct |
28 |
Correct |
2686 ms |
205228 KB |
Output is correct |
29 |
Correct |
3317 ms |
200280 KB |
Output is correct |
30 |
Correct |
1625 ms |
132100 KB |
Output is correct |
31 |
Correct |
3222 ms |
196796 KB |
Output is correct |
32 |
Correct |
1557 ms |
98064 KB |
Output is correct |
33 |
Correct |
1582 ms |
99264 KB |
Output is correct |
34 |
Correct |
1859 ms |
109536 KB |
Output is correct |
35 |
Correct |
1874 ms |
112620 KB |
Output is correct |
36 |
Correct |
1594 ms |
95156 KB |
Output is correct |
37 |
Correct |
1395 ms |
77300 KB |
Output is correct |
38 |
Correct |
1447 ms |
83272 KB |
Output is correct |
39 |
Correct |
2029 ms |
109092 KB |
Output is correct |
40 |
Correct |
1479 ms |
83860 KB |
Output is correct |
41 |
Correct |
1448 ms |
80452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
11980 KB |
Output is correct |
2 |
Correct |
7 ms |
11980 KB |
Output is correct |
3 |
Correct |
8 ms |
11980 KB |
Output is correct |
4 |
Correct |
7 ms |
11980 KB |
Output is correct |
5 |
Correct |
8 ms |
12044 KB |
Output is correct |
6 |
Correct |
9 ms |
12108 KB |
Output is correct |
7 |
Correct |
7 ms |
12108 KB |
Output is correct |
8 |
Correct |
7 ms |
12048 KB |
Output is correct |
9 |
Correct |
7 ms |
12068 KB |
Output is correct |
10 |
Correct |
8 ms |
12108 KB |
Output is correct |
11 |
Correct |
7 ms |
12048 KB |
Output is correct |
12 |
Correct |
7 ms |
12108 KB |
Output is correct |
13 |
Correct |
7 ms |
12044 KB |
Output is correct |
14 |
Correct |
7 ms |
12048 KB |
Output is correct |
15 |
Correct |
7 ms |
11980 KB |
Output is correct |
16 |
Correct |
7 ms |
11980 KB |
Output is correct |
17 |
Correct |
8 ms |
12108 KB |
Output is correct |
18 |
Correct |
7 ms |
11980 KB |
Output is correct |
19 |
Correct |
7 ms |
12048 KB |
Output is correct |
20 |
Correct |
1895 ms |
128324 KB |
Output is correct |
21 |
Correct |
2651 ms |
164364 KB |
Output is correct |
22 |
Correct |
1601 ms |
132540 KB |
Output is correct |
23 |
Correct |
1585 ms |
125244 KB |
Output is correct |
24 |
Correct |
1608 ms |
132984 KB |
Output is correct |
25 |
Correct |
2155 ms |
135320 KB |
Output is correct |
26 |
Correct |
2404 ms |
144620 KB |
Output is correct |
27 |
Correct |
2673 ms |
169056 KB |
Output is correct |
28 |
Correct |
2060 ms |
114976 KB |
Output is correct |
29 |
Correct |
1762 ms |
104220 KB |
Output is correct |
30 |
Correct |
2477 ms |
172516 KB |
Output is correct |
31 |
Correct |
1300 ms |
98540 KB |
Output is correct |
32 |
Correct |
1825 ms |
86556 KB |
Output is correct |
33 |
Correct |
1630 ms |
84752 KB |
Output is correct |
34 |
Correct |
1557 ms |
81356 KB |
Output is correct |
35 |
Correct |
869 ms |
105868 KB |
Output is correct |
36 |
Correct |
47 ms |
16512 KB |
Output is correct |
37 |
Correct |
588 ms |
57448 KB |
Output is correct |
38 |
Correct |
1409 ms |
77236 KB |
Output is correct |
39 |
Correct |
1421 ms |
83320 KB |
Output is correct |
40 |
Correct |
2019 ms |
109176 KB |
Output is correct |
41 |
Correct |
1484 ms |
83900 KB |
Output is correct |
42 |
Correct |
1447 ms |
80556 KB |
Output is correct |
43 |
Correct |
443 ms |
30228 KB |
Output is correct |
44 |
Correct |
2005 ms |
153964 KB |
Output is correct |
45 |
Correct |
2575 ms |
162224 KB |
Output is correct |
46 |
Correct |
3374 ms |
200024 KB |
Output is correct |
47 |
Correct |
3417 ms |
199164 KB |
Output is correct |
48 |
Correct |
3371 ms |
195916 KB |
Output is correct |
49 |
Correct |
1624 ms |
129256 KB |
Output is correct |
50 |
Correct |
1812 ms |
104368 KB |
Output is correct |
51 |
Correct |
3342 ms |
192652 KB |
Output is correct |
52 |
Correct |
1718 ms |
117200 KB |
Output is correct |
53 |
Correct |
37 ms |
22608 KB |
Output is correct |
54 |
Correct |
2649 ms |
163812 KB |
Output is correct |
55 |
Correct |
2755 ms |
204152 KB |
Output is correct |
56 |
Correct |
3379 ms |
203028 KB |
Output is correct |
57 |
Correct |
2298 ms |
150740 KB |
Output is correct |
58 |
Correct |
2407 ms |
164920 KB |
Output is correct |
59 |
Correct |
2680 ms |
189392 KB |
Output is correct |
60 |
Correct |
2354 ms |
150896 KB |
Output is correct |
61 |
Correct |
2425 ms |
165244 KB |
Output is correct |
62 |
Correct |
1596 ms |
129440 KB |
Output is correct |
63 |
Correct |
172 ms |
57584 KB |
Output is correct |
64 |
Correct |
2054 ms |
169192 KB |
Output is correct |
65 |
Correct |
1993 ms |
161096 KB |
Output is correct |
66 |
Correct |
1755 ms |
119992 KB |
Output is correct |
67 |
Correct |
1954 ms |
154844 KB |
Output is correct |
68 |
Correct |
1645 ms |
102360 KB |
Output is correct |
69 |
Correct |
3460 ms |
203244 KB |
Output is correct |
70 |
Correct |
2686 ms |
205228 KB |
Output is correct |
71 |
Correct |
3317 ms |
200280 KB |
Output is correct |
72 |
Correct |
1625 ms |
132100 KB |
Output is correct |
73 |
Correct |
3222 ms |
196796 KB |
Output is correct |
74 |
Correct |
1557 ms |
98064 KB |
Output is correct |
75 |
Correct |
1582 ms |
99264 KB |
Output is correct |
76 |
Correct |
1859 ms |
109536 KB |
Output is correct |
77 |
Correct |
1874 ms |
112620 KB |
Output is correct |
78 |
Correct |
1594 ms |
95156 KB |
Output is correct |
79 |
Correct |
1395 ms |
77300 KB |
Output is correct |
80 |
Correct |
1447 ms |
83272 KB |
Output is correct |
81 |
Correct |
2029 ms |
109092 KB |
Output is correct |
82 |
Correct |
1479 ms |
83860 KB |
Output is correct |
83 |
Correct |
1448 ms |
80452 KB |
Output is correct |
84 |
Correct |
391 ms |
27564 KB |
Output is correct |
85 |
Correct |
2779 ms |
168300 KB |
Output is correct |
86 |
Execution timed out |
4030 ms |
240920 KB |
Time limit exceeded |
87 |
Halted |
0 ms |
0 KB |
- |