#include<iostream>
#include<vector>
#include<unordered_map>
#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;
}
unordered_map<int,int>dmp[100010];
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);
int q;
cin>>q;
while(q-->0)
{
int t,x,y;
cin>>t>>x>>y;
int p1=uf1.par(lower_bound(all(sxv),pi(x,y),cmp1)-sxv.begin());
int p2=uf2.par(lower_bound(all(syv),pi(x,y),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];
p1=cp1[0],p2=cp2[0];
if(t==1)
for(int&u:cp1)
for(int&v:cp2)
dmp[u][v]=min(dmp[u][v],dis[p1][cdep[u]]+dis[p2][cdep[v]]-inf);
else
{
int ans=0;
for(int&u:cp1)
for(int&v:cp2)
ans=min(ans,dmp[u][v]+dis[p1][cdep[u]]+dis[p2][cdep[v]]);
if(ans==0)
ans=-1;
else
ans+=inf;
cout<<ans<<'\n';
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
11264 KB |
Output is correct |
2 |
Correct |
7 ms |
11264 KB |
Output is correct |
3 |
Correct |
8 ms |
11392 KB |
Output is correct |
4 |
Correct |
23 ms |
12544 KB |
Output is correct |
5 |
Correct |
27 ms |
12544 KB |
Output is correct |
6 |
Correct |
23 ms |
12672 KB |
Output is correct |
7 |
Correct |
32 ms |
13568 KB |
Output is correct |
8 |
Correct |
33 ms |
13600 KB |
Output is correct |
9 |
Correct |
32 ms |
13696 KB |
Output is correct |
10 |
Correct |
32 ms |
13560 KB |
Output is correct |
11 |
Correct |
32 ms |
13560 KB |
Output is correct |
12 |
Correct |
35 ms |
13688 KB |
Output is correct |
13 |
Correct |
32 ms |
13688 KB |
Output is correct |
14 |
Correct |
33 ms |
13696 KB |
Output is correct |
15 |
Correct |
33 ms |
13696 KB |
Output is correct |
16 |
Correct |
34 ms |
13688 KB |
Output is correct |
17 |
Correct |
32 ms |
13560 KB |
Output is correct |
18 |
Correct |
31 ms |
13560 KB |
Output is correct |
19 |
Correct |
31 ms |
13440 KB |
Output is correct |
20 |
Correct |
34 ms |
13816 KB |
Output is correct |
21 |
Correct |
41 ms |
13688 KB |
Output is correct |
22 |
Correct |
38 ms |
14136 KB |
Output is correct |
23 |
Correct |
45 ms |
14072 KB |
Output is correct |
24 |
Correct |
43 ms |
14360 KB |
Output is correct |
25 |
Correct |
21 ms |
12792 KB |
Output is correct |
26 |
Correct |
23 ms |
13304 KB |
Output is correct |
27 |
Correct |
17 ms |
12416 KB |
Output is correct |
28 |
Correct |
40 ms |
14328 KB |
Output is correct |
29 |
Correct |
40 ms |
14328 KB |
Output is correct |
30 |
Correct |
40 ms |
14456 KB |
Output is correct |
31 |
Correct |
41 ms |
14328 KB |
Output is correct |
32 |
Correct |
45 ms |
14360 KB |
Output is correct |
33 |
Correct |
43 ms |
14328 KB |
Output is correct |
34 |
Correct |
43 ms |
14304 KB |
Output is correct |
35 |
Correct |
50 ms |
14456 KB |
Output is correct |
36 |
Correct |
17 ms |
12544 KB |
Output is correct |
37 |
Correct |
40 ms |
14464 KB |
Output is correct |
38 |
Correct |
48 ms |
14328 KB |
Output is correct |
39 |
Correct |
40 ms |
14464 KB |
Output is correct |
40 |
Correct |
41 ms |
14456 KB |
Output is correct |
41 |
Correct |
38 ms |
14072 KB |
Output is correct |
42 |
Correct |
37 ms |
14080 KB |
Output is correct |
43 |
Correct |
25 ms |
12672 KB |
Output is correct |
44 |
Correct |
39 ms |
14072 KB |
Output is correct |
45 |
Correct |
37 ms |
14072 KB |
Output is correct |
46 |
Correct |
19 ms |
12672 KB |
Output is correct |
47 |
Correct |
33 ms |
13312 KB |
Output is correct |
48 |
Correct |
29 ms |
13056 KB |
Output is correct |
49 |
Correct |
28 ms |
13056 KB |
Output is correct |
50 |
Correct |
28 ms |
12928 KB |
Output is correct |
51 |
Correct |
29 ms |
12792 KB |
Output is correct |
52 |
Correct |
31 ms |
13304 KB |
Output is correct |
53 |
Correct |
32 ms |
13440 KB |
Output is correct |
54 |
Correct |
33 ms |
13440 KB |
Output is correct |
55 |
Correct |
36 ms |
13432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
11264 KB |
Output is correct |
2 |
Correct |
7 ms |
11264 KB |
Output is correct |
3 |
Correct |
8 ms |
11392 KB |
Output is correct |
4 |
Correct |
23 ms |
12544 KB |
Output is correct |
5 |
Correct |
27 ms |
12544 KB |
Output is correct |
6 |
Correct |
23 ms |
12672 KB |
Output is correct |
7 |
Correct |
32 ms |
13568 KB |
Output is correct |
8 |
Correct |
33 ms |
13600 KB |
Output is correct |
9 |
Correct |
32 ms |
13696 KB |
Output is correct |
10 |
Correct |
32 ms |
13560 KB |
Output is correct |
11 |
Correct |
32 ms |
13560 KB |
Output is correct |
12 |
Correct |
35 ms |
13688 KB |
Output is correct |
13 |
Correct |
32 ms |
13688 KB |
Output is correct |
14 |
Correct |
33 ms |
13696 KB |
Output is correct |
15 |
Correct |
33 ms |
13696 KB |
Output is correct |
16 |
Correct |
34 ms |
13688 KB |
Output is correct |
17 |
Correct |
32 ms |
13560 KB |
Output is correct |
18 |
Correct |
31 ms |
13560 KB |
Output is correct |
19 |
Correct |
31 ms |
13440 KB |
Output is correct |
20 |
Correct |
34 ms |
13816 KB |
Output is correct |
21 |
Correct |
41 ms |
13688 KB |
Output is correct |
22 |
Correct |
38 ms |
14136 KB |
Output is correct |
23 |
Correct |
45 ms |
14072 KB |
Output is correct |
24 |
Correct |
43 ms |
14360 KB |
Output is correct |
25 |
Correct |
21 ms |
12792 KB |
Output is correct |
26 |
Correct |
23 ms |
13304 KB |
Output is correct |
27 |
Correct |
17 ms |
12416 KB |
Output is correct |
28 |
Correct |
40 ms |
14328 KB |
Output is correct |
29 |
Correct |
40 ms |
14328 KB |
Output is correct |
30 |
Correct |
40 ms |
14456 KB |
Output is correct |
31 |
Correct |
41 ms |
14328 KB |
Output is correct |
32 |
Correct |
45 ms |
14360 KB |
Output is correct |
33 |
Correct |
43 ms |
14328 KB |
Output is correct |
34 |
Correct |
43 ms |
14304 KB |
Output is correct |
35 |
Correct |
50 ms |
14456 KB |
Output is correct |
36 |
Correct |
17 ms |
12544 KB |
Output is correct |
37 |
Correct |
40 ms |
14464 KB |
Output is correct |
38 |
Correct |
48 ms |
14328 KB |
Output is correct |
39 |
Correct |
40 ms |
14464 KB |
Output is correct |
40 |
Correct |
41 ms |
14456 KB |
Output is correct |
41 |
Correct |
38 ms |
14072 KB |
Output is correct |
42 |
Correct |
37 ms |
14080 KB |
Output is correct |
43 |
Correct |
25 ms |
12672 KB |
Output is correct |
44 |
Correct |
39 ms |
14072 KB |
Output is correct |
45 |
Correct |
37 ms |
14072 KB |
Output is correct |
46 |
Correct |
19 ms |
12672 KB |
Output is correct |
47 |
Correct |
33 ms |
13312 KB |
Output is correct |
48 |
Correct |
29 ms |
13056 KB |
Output is correct |
49 |
Correct |
28 ms |
13056 KB |
Output is correct |
50 |
Correct |
28 ms |
12928 KB |
Output is correct |
51 |
Correct |
29 ms |
12792 KB |
Output is correct |
52 |
Correct |
31 ms |
13304 KB |
Output is correct |
53 |
Correct |
32 ms |
13440 KB |
Output is correct |
54 |
Correct |
33 ms |
13440 KB |
Output is correct |
55 |
Correct |
36 ms |
13432 KB |
Output is correct |
56 |
Correct |
225 ms |
12880 KB |
Output is correct |
57 |
Correct |
223 ms |
12920 KB |
Output is correct |
58 |
Correct |
215 ms |
12920 KB |
Output is correct |
59 |
Correct |
323 ms |
14172 KB |
Output is correct |
60 |
Correct |
342 ms |
14456 KB |
Output is correct |
61 |
Correct |
342 ms |
14584 KB |
Output is correct |
62 |
Correct |
325 ms |
14328 KB |
Output is correct |
63 |
Correct |
313 ms |
14200 KB |
Output is correct |
64 |
Correct |
334 ms |
14456 KB |
Output is correct |
65 |
Correct |
316 ms |
14456 KB |
Output is correct |
66 |
Correct |
330 ms |
14336 KB |
Output is correct |
67 |
Correct |
314 ms |
14328 KB |
Output is correct |
68 |
Correct |
327 ms |
14184 KB |
Output is correct |
69 |
Correct |
306 ms |
14072 KB |
Output is correct |
70 |
Correct |
317 ms |
14072 KB |
Output is correct |
71 |
Correct |
303 ms |
13976 KB |
Output is correct |
72 |
Correct |
313 ms |
14200 KB |
Output is correct |
73 |
Correct |
313 ms |
14200 KB |
Output is correct |
74 |
Correct |
150 ms |
13304 KB |
Output is correct |
75 |
Correct |
168 ms |
13944 KB |
Output is correct |
76 |
Correct |
129 ms |
12876 KB |
Output is correct |
77 |
Correct |
440 ms |
15096 KB |
Output is correct |
78 |
Correct |
443 ms |
15224 KB |
Output is correct |
79 |
Correct |
131 ms |
12924 KB |
Output is correct |
80 |
Correct |
434 ms |
14956 KB |
Output is correct |
81 |
Correct |
502 ms |
14968 KB |
Output is correct |
82 |
Correct |
405 ms |
14968 KB |
Output is correct |
83 |
Correct |
399 ms |
14692 KB |
Output is correct |
84 |
Correct |
149 ms |
13304 KB |
Output is correct |
85 |
Correct |
401 ms |
14840 KB |
Output is correct |
86 |
Correct |
416 ms |
14840 KB |
Output is correct |
87 |
Correct |
134 ms |
13148 KB |
Output is correct |
88 |
Correct |
308 ms |
13816 KB |
Output is correct |
89 |
Correct |
328 ms |
13660 KB |
Output is correct |
90 |
Correct |
284 ms |
13432 KB |
Output is correct |
91 |
Correct |
274 ms |
13304 KB |
Output is correct |
92 |
Correct |
218 ms |
13176 KB |
Output is correct |
93 |
Correct |
311 ms |
14012 KB |
Output is correct |
94 |
Correct |
327 ms |
13816 KB |
Output is correct |
95 |
Correct |
362 ms |
13896 KB |
Output is correct |
96 |
Correct |
348 ms |
13944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
436 ms |
37620 KB |
Output is correct |
2 |
Correct |
412 ms |
38264 KB |
Output is correct |
3 |
Correct |
1922 ms |
89300 KB |
Output is correct |
4 |
Execution timed out |
2029 ms |
93136 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1353 ms |
68984 KB |
Output is correct |
2 |
Correct |
1422 ms |
71852 KB |
Output is correct |
3 |
Correct |
1461 ms |
71800 KB |
Output is correct |
4 |
Correct |
1709 ms |
73972 KB |
Output is correct |
5 |
Correct |
1659 ms |
72640 KB |
Output is correct |
6 |
Correct |
1815 ms |
72896 KB |
Output is correct |
7 |
Correct |
1569 ms |
71916 KB |
Output is correct |
8 |
Correct |
1572 ms |
71676 KB |
Output is correct |
9 |
Correct |
1556 ms |
72836 KB |
Output is correct |
10 |
Correct |
1775 ms |
86092 KB |
Output is correct |
11 |
Correct |
1813 ms |
85720 KB |
Output is correct |
12 |
Execution timed out |
2043 ms |
90388 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
11264 KB |
Output is correct |
2 |
Correct |
7 ms |
11264 KB |
Output is correct |
3 |
Correct |
8 ms |
11392 KB |
Output is correct |
4 |
Correct |
23 ms |
12544 KB |
Output is correct |
5 |
Correct |
27 ms |
12544 KB |
Output is correct |
6 |
Correct |
23 ms |
12672 KB |
Output is correct |
7 |
Correct |
32 ms |
13568 KB |
Output is correct |
8 |
Correct |
33 ms |
13600 KB |
Output is correct |
9 |
Correct |
32 ms |
13696 KB |
Output is correct |
10 |
Correct |
32 ms |
13560 KB |
Output is correct |
11 |
Correct |
32 ms |
13560 KB |
Output is correct |
12 |
Correct |
35 ms |
13688 KB |
Output is correct |
13 |
Correct |
32 ms |
13688 KB |
Output is correct |
14 |
Correct |
33 ms |
13696 KB |
Output is correct |
15 |
Correct |
33 ms |
13696 KB |
Output is correct |
16 |
Correct |
34 ms |
13688 KB |
Output is correct |
17 |
Correct |
32 ms |
13560 KB |
Output is correct |
18 |
Correct |
31 ms |
13560 KB |
Output is correct |
19 |
Correct |
31 ms |
13440 KB |
Output is correct |
20 |
Correct |
34 ms |
13816 KB |
Output is correct |
21 |
Correct |
41 ms |
13688 KB |
Output is correct |
22 |
Correct |
38 ms |
14136 KB |
Output is correct |
23 |
Correct |
45 ms |
14072 KB |
Output is correct |
24 |
Correct |
43 ms |
14360 KB |
Output is correct |
25 |
Correct |
21 ms |
12792 KB |
Output is correct |
26 |
Correct |
23 ms |
13304 KB |
Output is correct |
27 |
Correct |
17 ms |
12416 KB |
Output is correct |
28 |
Correct |
40 ms |
14328 KB |
Output is correct |
29 |
Correct |
40 ms |
14328 KB |
Output is correct |
30 |
Correct |
40 ms |
14456 KB |
Output is correct |
31 |
Correct |
41 ms |
14328 KB |
Output is correct |
32 |
Correct |
45 ms |
14360 KB |
Output is correct |
33 |
Correct |
43 ms |
14328 KB |
Output is correct |
34 |
Correct |
43 ms |
14304 KB |
Output is correct |
35 |
Correct |
50 ms |
14456 KB |
Output is correct |
36 |
Correct |
17 ms |
12544 KB |
Output is correct |
37 |
Correct |
40 ms |
14464 KB |
Output is correct |
38 |
Correct |
48 ms |
14328 KB |
Output is correct |
39 |
Correct |
40 ms |
14464 KB |
Output is correct |
40 |
Correct |
41 ms |
14456 KB |
Output is correct |
41 |
Correct |
38 ms |
14072 KB |
Output is correct |
42 |
Correct |
37 ms |
14080 KB |
Output is correct |
43 |
Correct |
25 ms |
12672 KB |
Output is correct |
44 |
Correct |
39 ms |
14072 KB |
Output is correct |
45 |
Correct |
37 ms |
14072 KB |
Output is correct |
46 |
Correct |
19 ms |
12672 KB |
Output is correct |
47 |
Correct |
33 ms |
13312 KB |
Output is correct |
48 |
Correct |
29 ms |
13056 KB |
Output is correct |
49 |
Correct |
28 ms |
13056 KB |
Output is correct |
50 |
Correct |
28 ms |
12928 KB |
Output is correct |
51 |
Correct |
29 ms |
12792 KB |
Output is correct |
52 |
Correct |
31 ms |
13304 KB |
Output is correct |
53 |
Correct |
32 ms |
13440 KB |
Output is correct |
54 |
Correct |
33 ms |
13440 KB |
Output is correct |
55 |
Correct |
36 ms |
13432 KB |
Output is correct |
56 |
Correct |
225 ms |
12880 KB |
Output is correct |
57 |
Correct |
223 ms |
12920 KB |
Output is correct |
58 |
Correct |
215 ms |
12920 KB |
Output is correct |
59 |
Correct |
323 ms |
14172 KB |
Output is correct |
60 |
Correct |
342 ms |
14456 KB |
Output is correct |
61 |
Correct |
342 ms |
14584 KB |
Output is correct |
62 |
Correct |
325 ms |
14328 KB |
Output is correct |
63 |
Correct |
313 ms |
14200 KB |
Output is correct |
64 |
Correct |
334 ms |
14456 KB |
Output is correct |
65 |
Correct |
316 ms |
14456 KB |
Output is correct |
66 |
Correct |
330 ms |
14336 KB |
Output is correct |
67 |
Correct |
314 ms |
14328 KB |
Output is correct |
68 |
Correct |
327 ms |
14184 KB |
Output is correct |
69 |
Correct |
306 ms |
14072 KB |
Output is correct |
70 |
Correct |
317 ms |
14072 KB |
Output is correct |
71 |
Correct |
303 ms |
13976 KB |
Output is correct |
72 |
Correct |
313 ms |
14200 KB |
Output is correct |
73 |
Correct |
313 ms |
14200 KB |
Output is correct |
74 |
Correct |
150 ms |
13304 KB |
Output is correct |
75 |
Correct |
168 ms |
13944 KB |
Output is correct |
76 |
Correct |
129 ms |
12876 KB |
Output is correct |
77 |
Correct |
440 ms |
15096 KB |
Output is correct |
78 |
Correct |
443 ms |
15224 KB |
Output is correct |
79 |
Correct |
131 ms |
12924 KB |
Output is correct |
80 |
Correct |
434 ms |
14956 KB |
Output is correct |
81 |
Correct |
502 ms |
14968 KB |
Output is correct |
82 |
Correct |
405 ms |
14968 KB |
Output is correct |
83 |
Correct |
399 ms |
14692 KB |
Output is correct |
84 |
Correct |
149 ms |
13304 KB |
Output is correct |
85 |
Correct |
401 ms |
14840 KB |
Output is correct |
86 |
Correct |
416 ms |
14840 KB |
Output is correct |
87 |
Correct |
134 ms |
13148 KB |
Output is correct |
88 |
Correct |
308 ms |
13816 KB |
Output is correct |
89 |
Correct |
328 ms |
13660 KB |
Output is correct |
90 |
Correct |
284 ms |
13432 KB |
Output is correct |
91 |
Correct |
274 ms |
13304 KB |
Output is correct |
92 |
Correct |
218 ms |
13176 KB |
Output is correct |
93 |
Correct |
311 ms |
14012 KB |
Output is correct |
94 |
Correct |
327 ms |
13816 KB |
Output is correct |
95 |
Correct |
362 ms |
13896 KB |
Output is correct |
96 |
Correct |
348 ms |
13944 KB |
Output is correct |
97 |
Correct |
436 ms |
37620 KB |
Output is correct |
98 |
Correct |
412 ms |
38264 KB |
Output is correct |
99 |
Correct |
1922 ms |
89300 KB |
Output is correct |
100 |
Execution timed out |
2029 ms |
93136 KB |
Time limit exceeded |
101 |
Halted |
0 ms |
0 KB |
- |