#include<iostream>
#include<vector>
#include<algorithm>
#define ep emplace
#define eb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef pair<int,int>pi;
typedef pair<ll,ll>pl;
const int inf=1e9+7;
const ll INF=1e18;
struct dsu
{
int pa[100010];
inline void init()
{
fill(pa,pa+100010,-1);
return;
}
dsu(){init();}
int par(int x)
{
if(pa[x]==-1)
return x;
return pa[x]=par(pa[x]);
}
inline void uni(int x,int y)
{
x=par(x);
y=par(y);
if(x==y)
return;
pa[x]=y;
return;
}
}uf1,uf2;
vector<int>adj[200010];
int sz[200010];
void sdfs(int x,int p)
{
sz[x]=1;
for(int&t:adj[x])
if(t!=p)
sdfs(t,x),sz[x]+=sz[t];
return;
}
bool chk[200010];
int dis[200010][18];
void dfs(int x,int p,int d,int cd)
{
dis[x][cd]=d;
sz[x]=1;
for(int&t:adj[x])
if(!chk[t]&&t!=p)
dfs(t,x,d+1,cd),sz[x]+=sz[t];
return;
}
int get(int x,int p,int z)
{
int ms=-1,mi=-1;
for(int&t:adj[x])
if(!chk[t]&&t!=p&&sz[t]>ms)
ms=sz[t],mi=t;
if(ms<=sz[z]/2)
return x;
return get(mi,x,z);
}
int cdep[200010];
int cpa[200010];
void dnc(int x)
{
chk[x]=1;
for(int&t:adj[x])
{
if(chk[t])
continue;
dfs(t,x,1,cdep[x]);
int nc=get(t,x,t);
cpa[nc]=x;
cdep[nc]=cdep[x]+1;
dnc(nc);
}
return;
}
vector<pi>sv1[100010];
vector<pi>sv2[100010];
int ind[100010][17][17];
int arr[10000010];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin>>n;
vector<pi>p(n);
for(pi&t:p)
cin>>t.fi>>t.se;
vector<pi>sxv=p,syv=p;
auto cmp1=[](const pi&x,const pi&y){return x<y;};
auto cmp2=[](const pi&x,const pi&y){return pi(x.se,x.fi)<pi(y.se,y.fi);};
sort(all(sxv),cmp1);
sort(all(syv),cmp2);
for(int i=1;i<n;i++)
if(sxv[i].fi==sxv[i-1].fi&&sxv[i].se==sxv[i-1].se+1)
uf1.uni(i,i-1);
for(int i=1;i<n;i++)
if(syv[i].fi==syv[i-1].fi+1&&syv[i].se==syv[i-1].se)
uf2.uni(i,i-1);
for(int i=1;i<n;i++)
{
if(sxv[i].fi==sxv[i-1].fi&&sxv[i].se==sxv[i-1].se+1)
{
int p1=uf2.par(lower_bound(all(syv),sxv[i-1],cmp2)-syv.begin())+n;
int p2=uf2.par(lower_bound(all(syv),sxv[i],cmp2)-syv.begin())+n;
adj[p1].eb(p2);
adj[p2].eb(p1);
}
}
for(int i=1;i<n;i++)
{
if(syv[i].fi==syv[i-1].fi+1&&syv[i].se==syv[i-1].se)
{
int p1=uf1.par(lower_bound(all(sxv),syv[i-1],cmp1)-sxv.begin());
int p2=uf1.par(lower_bound(all(sxv),syv[i],cmp1)-sxv.begin());
adj[p1].eb(p2);
adj[p2].eb(p1);
}
}
for(int i=0;i<n*2;i++)
{
sort(all(adj[i]));
adj[i].erase(unique(all(adj[i])),adj[i].end());
}
sdfs(uf1.par(0),-1);
int c1=get(uf1.par(0),-1,uf1.par(0));
cpa[c1]=-1;
dnc(c1);
sdfs(uf2.par(0)+n,-1);
int c2=get(uf2.par(0)+n,-1,uf2.par(0)+n);
cpa[c2]=-1;
dnc(c2);
for(int i=0;i<n;i++)
{
auto&t=sxv[i];
int p1=uf1.par(i);
int p2=uf2.par(lower_bound(all(syv),t,cmp2)-syv.begin())+n;
vector<int>cp1,cp2;
while(p1!=-1)
cp1.eb(p1),p1=cpa[p1];
while(p2!=-1)
cp2.eb(p2),p2=cpa[p2];
for(int&u:cp1)
for(int&v:cp2)
sv1[u].eb(v-n,i);
}
for(int i=0;i<n;i++)
for(pi&t:sv1[i])
sv2[t.fi].eb(i,t.se);
int ct=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<(int)sv2[i].size();j++)
{
ind[sv2[i][j].se][cdep[sv2[i][j].fi]][cdep[i+n]]=ct;
if(j==(int)sv2[i].size()-1||sv2[i][j].fi!=sv2[i][j+1].fi)
ct++;
}
}
int q;
cin>>q;
for(int qi=0;qi<q;qi++)
{
int t,x,y;
cin>>t>>x>>y;
int id=lower_bound(all(sxv),pi(x,y),cmp1)-sxv.begin();
int p1=uf1.par(id);
int p2=uf2.par(lower_bound(all(syv),pi(x,y),cmp2)-syv.begin())+n;
if(t==1)
for(int i=cdep[p1];i>=0;i--)
for(int j=cdep[p2];j>=0;j--)
arr[ind[id][i][j]]=min(arr[ind[id][i][j]],dis[p1][i]+dis[p2][j]-inf);
else
{
int ans=0;
for(int i=cdep[p1];i>=0;i--)
for(int j=cdep[p2];j>=0;j--)
ans=min(ans,arr[ind[id][i][j]]+dis[p1][i]+dis[p2][j]);
if(ans==0)
ans=-1;
else
ans+=inf;
cout<<ans<<'\n';
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
10496 KB |
Output is correct |
2 |
Correct |
6 ms |
10624 KB |
Output is correct |
3 |
Correct |
8 ms |
10752 KB |
Output is correct |
4 |
Correct |
27 ms |
19708 KB |
Output is correct |
5 |
Correct |
30 ms |
19836 KB |
Output is correct |
6 |
Correct |
34 ms |
20860 KB |
Output is correct |
7 |
Correct |
37 ms |
22520 KB |
Output is correct |
8 |
Correct |
38 ms |
23408 KB |
Output is correct |
9 |
Correct |
39 ms |
23532 KB |
Output is correct |
10 |
Correct |
35 ms |
22128 KB |
Output is correct |
11 |
Correct |
37 ms |
23024 KB |
Output is correct |
12 |
Correct |
38 ms |
23408 KB |
Output is correct |
13 |
Correct |
39 ms |
23276 KB |
Output is correct |
14 |
Correct |
36 ms |
23024 KB |
Output is correct |
15 |
Correct |
37 ms |
23532 KB |
Output is correct |
16 |
Correct |
36 ms |
23032 KB |
Output is correct |
17 |
Correct |
37 ms |
23148 KB |
Output is correct |
18 |
Correct |
35 ms |
22768 KB |
Output is correct |
19 |
Correct |
35 ms |
22640 KB |
Output is correct |
20 |
Correct |
39 ms |
24052 KB |
Output is correct |
21 |
Correct |
37 ms |
23280 KB |
Output is correct |
22 |
Correct |
44 ms |
26316 KB |
Output is correct |
23 |
Correct |
42 ms |
26092 KB |
Output is correct |
24 |
Correct |
46 ms |
27396 KB |
Output is correct |
25 |
Correct |
24 ms |
18736 KB |
Output is correct |
26 |
Correct |
27 ms |
19696 KB |
Output is correct |
27 |
Correct |
22 ms |
18428 KB |
Output is correct |
28 |
Correct |
45 ms |
27376 KB |
Output is correct |
29 |
Correct |
45 ms |
27480 KB |
Output is correct |
30 |
Correct |
46 ms |
27884 KB |
Output is correct |
31 |
Correct |
46 ms |
27760 KB |
Output is correct |
32 |
Correct |
46 ms |
27500 KB |
Output is correct |
33 |
Correct |
46 ms |
27504 KB |
Output is correct |
34 |
Correct |
45 ms |
27116 KB |
Output is correct |
35 |
Correct |
47 ms |
27884 KB |
Output is correct |
36 |
Correct |
22 ms |
18552 KB |
Output is correct |
37 |
Correct |
45 ms |
27376 KB |
Output is correct |
38 |
Correct |
51 ms |
27428 KB |
Output is correct |
39 |
Correct |
46 ms |
27760 KB |
Output is correct |
40 |
Correct |
47 ms |
27888 KB |
Output is correct |
41 |
Correct |
44 ms |
26476 KB |
Output is correct |
42 |
Correct |
44 ms |
26480 KB |
Output is correct |
43 |
Correct |
27 ms |
18352 KB |
Output is correct |
44 |
Correct |
43 ms |
26352 KB |
Output is correct |
45 |
Correct |
44 ms |
26608 KB |
Output is correct |
46 |
Correct |
23 ms |
18620 KB |
Output is correct |
47 |
Correct |
36 ms |
23152 KB |
Output is correct |
48 |
Correct |
38 ms |
22508 KB |
Output is correct |
49 |
Correct |
34 ms |
22516 KB |
Output is correct |
50 |
Correct |
34 ms |
22136 KB |
Output is correct |
51 |
Correct |
31 ms |
21872 KB |
Output is correct |
52 |
Correct |
37 ms |
23916 KB |
Output is correct |
53 |
Correct |
37 ms |
23792 KB |
Output is correct |
54 |
Correct |
38 ms |
24688 KB |
Output is correct |
55 |
Correct |
39 ms |
24816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
10496 KB |
Output is correct |
2 |
Correct |
6 ms |
10624 KB |
Output is correct |
3 |
Correct |
8 ms |
10752 KB |
Output is correct |
4 |
Correct |
27 ms |
19708 KB |
Output is correct |
5 |
Correct |
30 ms |
19836 KB |
Output is correct |
6 |
Correct |
34 ms |
20860 KB |
Output is correct |
7 |
Correct |
37 ms |
22520 KB |
Output is correct |
8 |
Correct |
38 ms |
23408 KB |
Output is correct |
9 |
Correct |
39 ms |
23532 KB |
Output is correct |
10 |
Correct |
35 ms |
22128 KB |
Output is correct |
11 |
Correct |
37 ms |
23024 KB |
Output is correct |
12 |
Correct |
38 ms |
23408 KB |
Output is correct |
13 |
Correct |
39 ms |
23276 KB |
Output is correct |
14 |
Correct |
36 ms |
23024 KB |
Output is correct |
15 |
Correct |
37 ms |
23532 KB |
Output is correct |
16 |
Correct |
36 ms |
23032 KB |
Output is correct |
17 |
Correct |
37 ms |
23148 KB |
Output is correct |
18 |
Correct |
35 ms |
22768 KB |
Output is correct |
19 |
Correct |
35 ms |
22640 KB |
Output is correct |
20 |
Correct |
39 ms |
24052 KB |
Output is correct |
21 |
Correct |
37 ms |
23280 KB |
Output is correct |
22 |
Correct |
44 ms |
26316 KB |
Output is correct |
23 |
Correct |
42 ms |
26092 KB |
Output is correct |
24 |
Correct |
46 ms |
27396 KB |
Output is correct |
25 |
Correct |
24 ms |
18736 KB |
Output is correct |
26 |
Correct |
27 ms |
19696 KB |
Output is correct |
27 |
Correct |
22 ms |
18428 KB |
Output is correct |
28 |
Correct |
45 ms |
27376 KB |
Output is correct |
29 |
Correct |
45 ms |
27480 KB |
Output is correct |
30 |
Correct |
46 ms |
27884 KB |
Output is correct |
31 |
Correct |
46 ms |
27760 KB |
Output is correct |
32 |
Correct |
46 ms |
27500 KB |
Output is correct |
33 |
Correct |
46 ms |
27504 KB |
Output is correct |
34 |
Correct |
45 ms |
27116 KB |
Output is correct |
35 |
Correct |
47 ms |
27884 KB |
Output is correct |
36 |
Correct |
22 ms |
18552 KB |
Output is correct |
37 |
Correct |
45 ms |
27376 KB |
Output is correct |
38 |
Correct |
51 ms |
27428 KB |
Output is correct |
39 |
Correct |
46 ms |
27760 KB |
Output is correct |
40 |
Correct |
47 ms |
27888 KB |
Output is correct |
41 |
Correct |
44 ms |
26476 KB |
Output is correct |
42 |
Correct |
44 ms |
26480 KB |
Output is correct |
43 |
Correct |
27 ms |
18352 KB |
Output is correct |
44 |
Correct |
43 ms |
26352 KB |
Output is correct |
45 |
Correct |
44 ms |
26608 KB |
Output is correct |
46 |
Correct |
23 ms |
18620 KB |
Output is correct |
47 |
Correct |
36 ms |
23152 KB |
Output is correct |
48 |
Correct |
38 ms |
22508 KB |
Output is correct |
49 |
Correct |
34 ms |
22516 KB |
Output is correct |
50 |
Correct |
34 ms |
22136 KB |
Output is correct |
51 |
Correct |
31 ms |
21872 KB |
Output is correct |
52 |
Correct |
37 ms |
23916 KB |
Output is correct |
53 |
Correct |
37 ms |
23792 KB |
Output is correct |
54 |
Correct |
38 ms |
24688 KB |
Output is correct |
55 |
Correct |
39 ms |
24816 KB |
Output is correct |
56 |
Correct |
106 ms |
21616 KB |
Output is correct |
57 |
Correct |
93 ms |
22004 KB |
Output is correct |
58 |
Correct |
95 ms |
21620 KB |
Output is correct |
59 |
Correct |
116 ms |
24284 KB |
Output is correct |
60 |
Correct |
119 ms |
25072 KB |
Output is correct |
61 |
Correct |
119 ms |
25064 KB |
Output is correct |
62 |
Correct |
118 ms |
24424 KB |
Output is correct |
63 |
Correct |
120 ms |
24304 KB |
Output is correct |
64 |
Correct |
121 ms |
25068 KB |
Output is correct |
65 |
Correct |
121 ms |
25036 KB |
Output is correct |
66 |
Correct |
120 ms |
25064 KB |
Output is correct |
67 |
Correct |
116 ms |
24684 KB |
Output is correct |
68 |
Correct |
117 ms |
24684 KB |
Output is correct |
69 |
Correct |
113 ms |
24048 KB |
Output is correct |
70 |
Correct |
118 ms |
24556 KB |
Output is correct |
71 |
Correct |
110 ms |
22508 KB |
Output is correct |
72 |
Correct |
119 ms |
24936 KB |
Output is correct |
73 |
Correct |
116 ms |
24688 KB |
Output is correct |
74 |
Correct |
93 ms |
20016 KB |
Output is correct |
75 |
Correct |
93 ms |
20844 KB |
Output is correct |
76 |
Correct |
88 ms |
19828 KB |
Output is correct |
77 |
Correct |
139 ms |
28780 KB |
Output is correct |
78 |
Correct |
131 ms |
29040 KB |
Output is correct |
79 |
Correct |
90 ms |
19952 KB |
Output is correct |
80 |
Correct |
133 ms |
28780 KB |
Output is correct |
81 |
Correct |
140 ms |
28656 KB |
Output is correct |
82 |
Correct |
130 ms |
27632 KB |
Output is correct |
83 |
Correct |
136 ms |
27892 KB |
Output is correct |
84 |
Correct |
93 ms |
19780 KB |
Output is correct |
85 |
Correct |
133 ms |
27888 KB |
Output is correct |
86 |
Correct |
128 ms |
27756 KB |
Output is correct |
87 |
Correct |
88 ms |
19760 KB |
Output is correct |
88 |
Correct |
120 ms |
24556 KB |
Output is correct |
89 |
Correct |
114 ms |
24208 KB |
Output is correct |
90 |
Correct |
111 ms |
24304 KB |
Output is correct |
91 |
Correct |
108 ms |
23560 KB |
Output is correct |
92 |
Correct |
102 ms |
22004 KB |
Output is correct |
93 |
Correct |
117 ms |
25456 KB |
Output is correct |
94 |
Correct |
116 ms |
24944 KB |
Output is correct |
95 |
Correct |
121 ms |
25708 KB |
Output is correct |
96 |
Correct |
120 ms |
25840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
459 ms |
200692 KB |
Output is correct |
2 |
Correct |
471 ms |
200192 KB |
Output is correct |
3 |
Runtime error |
567 ms |
262148 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
673 ms |
262148 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
10496 KB |
Output is correct |
2 |
Correct |
6 ms |
10624 KB |
Output is correct |
3 |
Correct |
8 ms |
10752 KB |
Output is correct |
4 |
Correct |
27 ms |
19708 KB |
Output is correct |
5 |
Correct |
30 ms |
19836 KB |
Output is correct |
6 |
Correct |
34 ms |
20860 KB |
Output is correct |
7 |
Correct |
37 ms |
22520 KB |
Output is correct |
8 |
Correct |
38 ms |
23408 KB |
Output is correct |
9 |
Correct |
39 ms |
23532 KB |
Output is correct |
10 |
Correct |
35 ms |
22128 KB |
Output is correct |
11 |
Correct |
37 ms |
23024 KB |
Output is correct |
12 |
Correct |
38 ms |
23408 KB |
Output is correct |
13 |
Correct |
39 ms |
23276 KB |
Output is correct |
14 |
Correct |
36 ms |
23024 KB |
Output is correct |
15 |
Correct |
37 ms |
23532 KB |
Output is correct |
16 |
Correct |
36 ms |
23032 KB |
Output is correct |
17 |
Correct |
37 ms |
23148 KB |
Output is correct |
18 |
Correct |
35 ms |
22768 KB |
Output is correct |
19 |
Correct |
35 ms |
22640 KB |
Output is correct |
20 |
Correct |
39 ms |
24052 KB |
Output is correct |
21 |
Correct |
37 ms |
23280 KB |
Output is correct |
22 |
Correct |
44 ms |
26316 KB |
Output is correct |
23 |
Correct |
42 ms |
26092 KB |
Output is correct |
24 |
Correct |
46 ms |
27396 KB |
Output is correct |
25 |
Correct |
24 ms |
18736 KB |
Output is correct |
26 |
Correct |
27 ms |
19696 KB |
Output is correct |
27 |
Correct |
22 ms |
18428 KB |
Output is correct |
28 |
Correct |
45 ms |
27376 KB |
Output is correct |
29 |
Correct |
45 ms |
27480 KB |
Output is correct |
30 |
Correct |
46 ms |
27884 KB |
Output is correct |
31 |
Correct |
46 ms |
27760 KB |
Output is correct |
32 |
Correct |
46 ms |
27500 KB |
Output is correct |
33 |
Correct |
46 ms |
27504 KB |
Output is correct |
34 |
Correct |
45 ms |
27116 KB |
Output is correct |
35 |
Correct |
47 ms |
27884 KB |
Output is correct |
36 |
Correct |
22 ms |
18552 KB |
Output is correct |
37 |
Correct |
45 ms |
27376 KB |
Output is correct |
38 |
Correct |
51 ms |
27428 KB |
Output is correct |
39 |
Correct |
46 ms |
27760 KB |
Output is correct |
40 |
Correct |
47 ms |
27888 KB |
Output is correct |
41 |
Correct |
44 ms |
26476 KB |
Output is correct |
42 |
Correct |
44 ms |
26480 KB |
Output is correct |
43 |
Correct |
27 ms |
18352 KB |
Output is correct |
44 |
Correct |
43 ms |
26352 KB |
Output is correct |
45 |
Correct |
44 ms |
26608 KB |
Output is correct |
46 |
Correct |
23 ms |
18620 KB |
Output is correct |
47 |
Correct |
36 ms |
23152 KB |
Output is correct |
48 |
Correct |
38 ms |
22508 KB |
Output is correct |
49 |
Correct |
34 ms |
22516 KB |
Output is correct |
50 |
Correct |
34 ms |
22136 KB |
Output is correct |
51 |
Correct |
31 ms |
21872 KB |
Output is correct |
52 |
Correct |
37 ms |
23916 KB |
Output is correct |
53 |
Correct |
37 ms |
23792 KB |
Output is correct |
54 |
Correct |
38 ms |
24688 KB |
Output is correct |
55 |
Correct |
39 ms |
24816 KB |
Output is correct |
56 |
Correct |
106 ms |
21616 KB |
Output is correct |
57 |
Correct |
93 ms |
22004 KB |
Output is correct |
58 |
Correct |
95 ms |
21620 KB |
Output is correct |
59 |
Correct |
116 ms |
24284 KB |
Output is correct |
60 |
Correct |
119 ms |
25072 KB |
Output is correct |
61 |
Correct |
119 ms |
25064 KB |
Output is correct |
62 |
Correct |
118 ms |
24424 KB |
Output is correct |
63 |
Correct |
120 ms |
24304 KB |
Output is correct |
64 |
Correct |
121 ms |
25068 KB |
Output is correct |
65 |
Correct |
121 ms |
25036 KB |
Output is correct |
66 |
Correct |
120 ms |
25064 KB |
Output is correct |
67 |
Correct |
116 ms |
24684 KB |
Output is correct |
68 |
Correct |
117 ms |
24684 KB |
Output is correct |
69 |
Correct |
113 ms |
24048 KB |
Output is correct |
70 |
Correct |
118 ms |
24556 KB |
Output is correct |
71 |
Correct |
110 ms |
22508 KB |
Output is correct |
72 |
Correct |
119 ms |
24936 KB |
Output is correct |
73 |
Correct |
116 ms |
24688 KB |
Output is correct |
74 |
Correct |
93 ms |
20016 KB |
Output is correct |
75 |
Correct |
93 ms |
20844 KB |
Output is correct |
76 |
Correct |
88 ms |
19828 KB |
Output is correct |
77 |
Correct |
139 ms |
28780 KB |
Output is correct |
78 |
Correct |
131 ms |
29040 KB |
Output is correct |
79 |
Correct |
90 ms |
19952 KB |
Output is correct |
80 |
Correct |
133 ms |
28780 KB |
Output is correct |
81 |
Correct |
140 ms |
28656 KB |
Output is correct |
82 |
Correct |
130 ms |
27632 KB |
Output is correct |
83 |
Correct |
136 ms |
27892 KB |
Output is correct |
84 |
Correct |
93 ms |
19780 KB |
Output is correct |
85 |
Correct |
133 ms |
27888 KB |
Output is correct |
86 |
Correct |
128 ms |
27756 KB |
Output is correct |
87 |
Correct |
88 ms |
19760 KB |
Output is correct |
88 |
Correct |
120 ms |
24556 KB |
Output is correct |
89 |
Correct |
114 ms |
24208 KB |
Output is correct |
90 |
Correct |
111 ms |
24304 KB |
Output is correct |
91 |
Correct |
108 ms |
23560 KB |
Output is correct |
92 |
Correct |
102 ms |
22004 KB |
Output is correct |
93 |
Correct |
117 ms |
25456 KB |
Output is correct |
94 |
Correct |
116 ms |
24944 KB |
Output is correct |
95 |
Correct |
121 ms |
25708 KB |
Output is correct |
96 |
Correct |
120 ms |
25840 KB |
Output is correct |
97 |
Correct |
459 ms |
200692 KB |
Output is correct |
98 |
Correct |
471 ms |
200192 KB |
Output is correct |
99 |
Runtime error |
567 ms |
262148 KB |
Execution killed with signal 9 |
100 |
Halted |
0 ms |
0 KB |
- |