#include<bits/stdc++.h>
#define ld long double
#define pii pair<int,int>
#define pb push_back
using namespace std;
const int N=2e5+10;
const int K=23;
const ld inf=1e18+1e17;
int T,n=2,m=1,q,x[N],y[N],s[N],t[N],c[N],d[N],f[K][N],de[N],lg[N];
map <pii,int> ma;
vector <int> e[N];
ld dis(int i,int j) {return sqrt(1ll*(x[i]-x[j])*(x[i]-x[j])+1ll*(y[i]-y[j])*(y[i]-y[j]));}
struct mat
{
ld w[2][2];
void cle() {for(int i=0;i<2;i++) for(int j=0;j<2;j++) w[i][j]=inf;}
void ini() {cle(); for(int i=0;i<2;i++) w[i][i]=0;}
mat operator *(const mat &b) const
{
mat r; r.cle();
for(int i=0;i<2;i++) for(int k=0;k<2;k++) if(w[i][k]<inf)
for(int j=0;j<2;j++) r.w[i][j]=min(r.w[i][j],w[i][k]+b.w[k][j]);
return r;
}
void mk(int ax,int ay,int bx,int by) {for(int i=0;i<2;i++) for(int j=0;j<2;j++) w[i][j]=dis(i?ay:ax,j?by:bx);}
}g[K][N];
mat cg(mat A) {swap(A.w[0][1],A.w[1][0]); return A;}
void dfs(int u)
{
if(u!=1) g[0][u].mk(c[u],d[u],c[f[0][u]],d[f[0][u]]);
for(int i=1;i<=20;i++)
{
int to=f[i-1][u];
if(!to||!f[i-1][to]) break;
f[i][u]=f[i-1][to],g[i][u]=g[i-1][u]*g[i-1][to];
}
for(int v:e[u]) f[0][v]=u,de[v]=de[u]+1,dfs(v);
}
ld qry(int x,int y,bool o)
{
mat A,B; A.ini(),B.ini();
while(de[x]>de[y])
{
int i=lg[de[x]-de[y]];
A=A*g[i][x],x=f[i][x];
}
while(de[x]<de[y])
{
int i=lg[de[y]-de[x]];
B=cg(g[i][y])*B,y=f[i][y];
}
ld res=inf;
if(x!=y)
{
for(int i=lg[de[x]]+1;i>=0;i--) if(f[i][x]!=f[i][y])
A=A*g[i][x],B=cg(g[i][y])*B,x=f[i][x],y=f[i][y];
if(x/2==y/2) res=A.w[o][1]+B.w[1][1];
A=A*g[0][x]*cg(g[0][y])*B;
}
else A=A*B;
return min(res,A.w[o][1]);
}
int main()
{
freopen("kap.in","r",stdin);
freopen("kap.out","w",stdout);
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>x[1]>>y[1]>>x[2]>>y[2]>>T,c[1]=1,d[1]=2,ma[{1,2}]=1;
while(T--)
{
char ch; cin>>ch;
if(ch=='d')
{
int A,B,t;
n++,cin>>x[n]>>y[n]>>A>>B;
if(A>B) swap(A,B);
t=ma[{A,B}],ma[{A,n}]=++m,c[m]=A,d[m]=n,e[t].pb(m);
ma[{B,n}]=++m,c[m]=B,d[m]=n,e[t].pb(m);
}
else q++,cin>>s[q]>>t[q];
}
dfs(1),lg[0]=-1;
for(int i=1;i<=m;i++) lg[i]=lg[i>>1]+1;
for(int i=1;i<=q;i++)
{
ld res=inf; int a=s[i],b=t[i];
if(a>b) swap(a,b);
if(a==b) res=0;
else if(a==1)
{
if(b==2) res=dis(1,2);
else for(int o=0;o<2;o++) res=min(res,qry(1,(b-2)*2+o,0));
}
else if(a==2) for(int o=0;o<2;o++) res=min(res,qry(1,(b-2)*2+o,1));
else for(int _=0;_<2;_++) for(int o=0;o<2;o++) res=min(res,qry((a-2)*2+_,(b-2)*2+o,1));
cout<<fixed<<setprecision(6)<<res<<'\n';
}
return 0;
}剪贴板
剪贴板 wo6zozxc
剪贴板信息
剪贴板信息
- 收录
- 更新