Untitled
Anonymous
Plain Text
10/04/2025 2:57 PM
9.3 KB
25
Indexable
Never
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <functional>
#include <iterator>
#include <tuple>
#include <chrono>
#include <cassert>
#include <unordered_map>
#include <cstdint>
#include <limits>
#include <iomanip>
#include <fstream> // File stream objects
#include <sstream> // String stream objects
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
#define PI 3.141592653589793238462643383279502884L
#define make_unique(vec) vec.resize(distance(vec.begin(),unique(vec.begin(),vec.end())))
#define endl "\n"
#define all(st) st.begin(),st.end()
#define rall(st) st.rbegin(),st.end()
#define tup tuple<int,int,int>
#define pi pair<int,int>
#define pl pair<ll,ll>
#define vi vector<int>
#define vd vector<double>
#define vl vector<ll>
#define rep(i,l,r) for(int i=l;i<r;i++)
#define cs(ans); cout<<setprecision(10)<<fixed<<ans<<endl;
#define pb push_back
#define ll long long int
#define f first
#define se second
ll mod=1e9+7,inf=1e16;//LLONG_MAX;
using namespace std;
//using namespace __gnu_pbds;
ll mi(ll n,ll m){ll pw=n%m;ll ex=m-2;ll ans=1;while(ex){if(ex&1) ans = ans*pw%m;pw = pw*pw%m;ex>>=1;}return ans%mod;}
ll pw(ll a,ll n){ll pw=a;ll ex=n;ll ans=1;while(ex){if(ex&1) ans = ans*pw;pw = pw*pw;ex>>=1;}return ans;}
ll pwm(ll a,ll n){ll pw=a%mod;ll ex=n;ll ans=1;while(ex){if(ex&1) ans = ans*pw%mod;pw = pw*pw%mod;ex>>=1;}return ans%mod;}
template<typename T>istream &operator>>(istream &is, vector<T> &v) { for (T &t : v) is >> t; return is; }
template<typename T>ostream &operator<<(ostream &os, const vector<T> &v) { for (const T &t : v) {os << t <<" ";} os << '\n'; return os; }
// template<typename T> using ordered_set=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
// mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());
//st.order_of_key(num)->number of elements < num
//st.find_by_order(i)->iterator of ith element in sorted order starting from zero
ll inverse(ll a,ll m){return a==1?1:(1-inverse(m%a,a)*m)/a+m;}
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll max(ll a,ll b){return a>b?a:b;}
ll min(ll a,ll b){return a<b?a:b;}
ll Abs(ll x){return x>0?x:(-x);}
ll Ceil(ll a,ll b){return a/b+(a%b!=0);}
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
int dx[]={0,1};
int dy[]={1,0};
template<int MOD=998244353>
struct Modular{
int value;
static const int MOD_value = MOD;
Modular(long long int v = 0) { value = v % MOD; if (value < 0) value += MOD;}
Modular(long long a, long long b) : value(0){ *this += a; *this /= b;}
Modular& operator+=(Modular const& b) {value += b.value; if (value >= MOD) value -= MOD; return *this;}
Modular& operator-=(Modular const& b) {value -= b.value; if (value < 0) value += MOD;return *this;}
Modular& operator*=(Modular const& b) {value = (long long)value * b.value % MOD;return *this;}
friend Modular mexp(Modular a, long long e) {
Modular res = 1; while (e) { if (e&1) res *= a; a *= a; e >>= 1; }
return res;
}
friend Modular inverse(Modular a) { return mexp(a, MOD - 2); }
Modular& operator/=(Modular const& b) { return *this *= inverse(b); }
friend Modular operator+(Modular a, Modular const b) { return a += b; }
friend Modular operator-(Modular a, Modular const b) { return a -= b; }
friend Modular operator-(Modular const a) { return 0 - a; }
friend Modular operator*(Modular a, Modular const b) { return a *= b; }
friend Modular operator/(Modular a, Modular const b) { return a /= b; }
friend std::ostream& operator<<(std::ostream& os, Modular const& a) {return os << a.value;}
friend bool operator==(Modular const& a, Modular const& b) {return a.value == b.value;}
friend bool operator!=(Modular const& a, Modular const& b) {return a.value != b.value;}
};
typedef Modular<998244353> mint;
//typedef Modular<1000000007> mint;
/************************DEBUG************************/
void __print(mint x) {cerr << x;}
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
/*************************END*************************/
struct custom_hash{
static uint64_t splitmix64(uint64_t x){
x+=0x9e3779b97f4a7c15;
x=(x^(x>>30))*0xbf58476d1ce4e5b9;
x=(x^(x>>27))*0x94d049bb133111eb;
return x^(x>>31);
}
size_t operator()(uint64_t x) const{
static const uint64_t FIXED_RANDOM=chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x+FIXED_RANDOM);
}
//unodered_map<int,int,custom_hash> mp;
};
//#define int long long int
//Binary_Search->dp->greedy
//Once check reverse also of your approach
/*
Check if you can solve the problem itself
before input queries to save computations.
---
In Lazy propagation you can't only add in the
given range but also update in the given range
Adding in the range can also be done without Lazy
by creating Segment/Fenwick tree of difference array.
---
In bitmask dp, go through element wise i.e consider
each element one by one to avoid duplicate calculations.
---
In dp instead of thinking directly about requirement,
think about how you will achieve to that requirement,
i.e what dp states you must choose to reach your goal.
Think like what can be done to update other dp value
using that state, if you know the dp value of currently.
---
Check with contribution of element to solve questions.
---
------------------------Sorting Set------------------------
struct comp{
bool operator()(const pair<int,int>&a,const pair<int,int>&b) const {
if(a.first>b.first) return true;
if(a.first<b.first) return false;
if(a.second<b.second) return true;
return false;
}
};
----------------------Lambda Function----------------------
auto update = [&](int i){
cnt[i]=(s[i]!='0'?cnt[2*i+1]:0)+(s[i]!='1'?cnt[2*i+2]:0);
return cnt[i];
};
function<int(int, int)> dfs = [&](int i,int par){
};
auto dfs = [&](const auto &self,int i,int par) -> void{
for(auto [nbs,w]:adj[i]){
if(nbs!=par) self(self,nbs,i);
}
};
dfs(dfs,0,-1);
*/
struct P { double x, y; };
P operator-(const P&a,const P&b){return {a.x-b.x,a.y-b.y};}
P operator+(const P&a,const P&b){return {a.x+b.x,a.y+b.y};}
P operator*(const P&a,double s){return {a.x*s,a.y*s};}
double dot(const P&a,const P&b){return a.x*b.x + a.y*b.y;}
double norm2(const P&a){return dot(a,a);}
void solve(){
P TS, TG, AS, AG;
cin >> TS.x >> TS.y >> TG.x >> TG.y;
cin >> AS.x >> AS.y >> AG.x >> AG.y;
double Ta=sqrt(norm2(TS-TG)),Tb=sqrt(norm2(AS-AG));
P v1 = {0,0}, v2 = {0,0};
if (Ta > 0) v1 = (P{TG.x-TS.x, TG.y-TS.y}) * (1.0 / Ta);
if (Tb > 0) v2 = (P{AG.x-AS.x, AG.y-AS.y}) * (1.0 / Tb);
double t1 = min(Ta, Tb);
double t2 = max(Ta, Tb);
vd candidates;
candidates.pb(0.0);
candidates.pb(t1);
candidates.pb(t2);
const double exp=1e-15;
P d0 = TS - AS;
P rv = P{v1.x - v2.x, v1.y - v2.y};
double a = norm2(rv);
double b = 2.0 * dot(d0, rv);
if (a > exp){
double tstar = - b / (2.0 * a);
if (tstar >= 0.0 && tstar <= t1) candidates.pb(tstar);
}
if (t1 < t2 - exp){
if (Ta < Tb){
P D0 = P{TG.x - AS.x, TG.y - AS.y};
double denom = norm2(v2);
if (denom > exp){
double tstar = dot(D0, v2) / denom;
if (tstar >= t1 && tstar <= t2) candidates.pb(tstar);
}
} else {
P D0 = P{TS.x - AG.x, TS.y - AG.y};
double denom = norm2(v1);
if (denom > exp){
double tstar = - dot(D0, v1) / denom;
if (tstar >= t1 && tstar <= t2) candidates.pb(tstar);
}
}
}
double best2 = 1e300;
for (double t : candidates){
P pT, pA;
if (t <= Ta) pT = TS + v1 * t; else pT = TG;
if (t <= Tb) pA = AS + v2 * t; else pA = AG;
best2 = min(best2, norm2(pT - pA));
}
cout << fixed << setprecision(15) << sqrt(best2) << endl;
}
int32_t main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int t=1;
scanf("%d",&t);
while(t--){
solve();
}
cerr<<"Time:"<<1000*((double)clock())/(double)CLOCKS_PER_SEC<<"ms\n";
return 0;
}
Editor is loading...
Leave a Comment