Submission #101771

#TimeUsernameProblemLanguageResultExecution timeMemory
101771BenqGrad (COI14_grad)C++14
100 / 100
808 ms94856 KiB
#pragma GCC optimize ("O3") #pragma GCC target ("sse4") #include <bits/stdc++.h> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/assoc_container.hpp> #include <ext/rope> using namespace std; using namespace __gnu_pbds; using namespace __gnu_cxx; typedef long long ll; typedef long double ld; typedef complex<ld> cd; typedef pair<int, int> pi; typedef pair<ll,ll> pl; typedef pair<ld,ld> pd; typedef vector<int> vi; typedef vector<ld> vd; typedef vector<ll> vl; typedef vector<pi> vpi; typedef vector<pl> vpl; typedef vector<cd> vcd; template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define F0R(i, a) for (int i = 0; i < (a); i++) #define FORd(i,a,b) for (int i = (b)-1; i >= (a); i--) #define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--) #define trav(a, x) for (auto& a : x) #define mp make_pair #define pb push_back #define f first #define s second #define lb lower_bound #define ub upper_bound #define sz(x) (int)x.size() #define beg(x) x.begin() #define en(x) x.end() #define all(x) beg(x), en(x) #define resz resize const int MOD = 1000000007; // 998244353 const ll INF = 1e18; const int MX = 200005; const ld PI = 4*atan((ld)1); template<class T> void ckmin(T &a, T b) { a = min(a, b); } template<class T> void ckmax(T &a, T b) { a = max(a, b); } template<class A, class B> A operator+=(A& l, const B& r) { return l = l+r; } template<class A, class B> A operator-=(A& l, const B& r) { return l = l-r; } template<class A, class B> A operator*=(A& l, const B& r) { return l = l*r; } template<class A, class B> A operator/=(A& l, const B& r) { return l = l/r; } namespace input { template<class T> void re(complex<T>& x); template<class T1, class T2> void re(pair<T1,T2>& p); template<class T> void re(vector<T>& a); template<class T, size_t SZ> void re(array<T,SZ>& a); template<class T> void re(T& x) { cin >> x; } void re(double& x) { string t; re(t); x = stod(t); } void re(ld& x) { string t; re(t); x = stold(t); } template<class Arg, class... Args> void re(Arg& first, Args&... rest) { re(first); re(rest...); } template<class T> void re(complex<T>& x) { T a,b; re(a,b); x = cd(a,b); } template<class T1, class T2> void re(pair<T1,T2>& p) { re(p.f,p.s); } template<class T> void re(vector<T>& a) { F0R(i,sz(a)) re(a[i]); } template<class T, size_t SZ> void re(array<T,SZ>& a) { F0R(i,SZ) re(a[i]); } } using namespace input; namespace output { template<class T1, class T2> void pr(const pair<T1,T2>& x); template<class T, size_t SZ> void pr(const array<T,SZ>& x); template<class T> void pr(const vector<T>& x); template<class T> void pr(const set<T>& x); template<class T1, class T2> void pr(const map<T1,T2>& x); template<class T> void pr(const T& x) { cout << x; } template<class Arg, class... Args> void pr(const Arg& first, const Args&... rest) { pr(first); pr(rest...); } template<class T1, class T2> void pr(const pair<T1,T2>& x) { pr("{",x.f,", ",x.s,"}"); } template<class T> void prContain(const T& x) { pr("{"); bool fst = 1; trav(a,x) pr(!fst?", ":"",a), fst = 0; pr("}"); } template<class T, size_t SZ> void pr(const array<T,SZ>& x) { prContain(x); } template<class T> void pr(const vector<T>& x) { prContain(x); } template<class T> void pr(const set<T>& x) { prContain(x); } template<class T1, class T2> void pr(const map<T1,T2>& x) { prContain(x); } void ps() { pr("\n"); } template<class Arg, class... Args> void ps(const Arg& first, const Args&... rest) { pr(first," "); ps(rest...); // print w/ spaces } } using namespace output; namespace io { void setIn(string s) { freopen(s.c_str(),"r",stdin); } void setOut(string s) { freopen(s.c_str(),"w",stdout); } void setIO(string s = "") { ios_base::sync_with_stdio(0); cin.tie(0); // fast I/O if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO } } using namespace io; template<class T> T invGeneral(T a, T b) { a %= b; if (a == 0) return b == 1 ? 0 : -1; T x = invGeneral(b,a); return x == -1 ? -1 : ((1-(ll)b*x)/a+b)%b; } template<class T> struct modInt { T val; T mod = 0; // static const T mod = MOD; void normalize() { if (mod == 0) return; val %= mod; if (val < 0) val += mod; } modInt(T v = 0, T m = 0) : val(v), mod(m) { normalize(); } // modInt(T v = 0, T m = 0) : val(v) { normalize(); } explicit operator T() const { return val; } friend ostream& operator<<(ostream& os, const modInt& a) { return os << a.val; } friend bool operator==(const modInt& a, const modInt& b) { return a.val == b.val; } friend bool operator!=(const modInt& a, const modInt& b) { return !(a == b); } friend void check(modInt& a, modInt& b) { // make sure all operations are valid // comment out if mod is static const if (a.mod > 0 && b.mod > 0) { assert(a.mod == b.mod); return; } T mod = max(a.mod,b.mod); if (mod == 0) mod = MOD; if (a.mod != mod) { a.mod = mod; a.normalize(); } if (b.mod != mod) { b.mod = mod; b.normalize(); } } friend modInt operator+(modInt a, modInt b) { check(a,b); a.val += (T)b; if (a.val >= a.mod) a.val -= a.mod; return a; } friend modInt operator-(modInt a, modInt b) { check(a,b); a.val -= (T)b; if (a.val < 0) a.val += a.mod; return a; } friend modInt operator-(const modInt& a) { return modInt(0)-a; } friend modInt operator*(modInt a, modInt b) { check(a,b); a.val = (ll)a.val*(T)b%a.mod; return a; } friend modInt exp(modInt a, ll p) { modInt ans(1,a.mod); for (; p; p /= 2, a *= a) if (p&1) ans *= a; return ans; } friend modInt inv(const modInt& a) { return {invGeneral(a.val,a.mod),a.mod}; // return exp(b,b.mod-2) if prime } friend modInt operator/(modInt a, modInt b) { check(a,b); return a*inv(b); } }; typedef modInt<int> mi; typedef pair<mi,mi> pmi; typedef vector<mi> vmi; typedef vector<pmi> vpmi; int N,verts, eds, oneEdge[MX]; pi pos[MX]; array<int,2> ed[MX]; map<array<int,2>,int> de; double dist(int a, int b) { if (a == b) return 0; assert(de.count({a,b}) || de.count({b,a})); return sqrt(pow(pos[a].f-pos[b].f,2)+pow(pos[a].s-pos[b].s,2)); } template<int SZ> struct LCA { const int MAXK = 32-__builtin_clz(SZ); int N, R = 1; // vertices from 1 to N, R = root vi adj[SZ]; int par[32-__builtin_clz(SZ)][SZ], depth[SZ]; array<array<double,2>,2> dists[32-__builtin_clz(SZ)][SZ]; void addEdge(int u, int v) { adj[u].pb(v), adj[v].pb(u); } void dfs(int u, int prev){ par[0][u] = prev; depth[u] = depth[prev]+1; if (prev) { F0R(i,2) F0R(j,2) dists[0][u][i][j] = dist(ed[u][i],ed[prev][j]); // ps("HUH",u,dists[0][u]); } for (int v: adj[u]) if (v != prev) dfs(v, u); } void init(int _N) { N = _N; dfs(R, 0); FOR(k,1,MAXK) FOR(i,1,N+1) { par[k][i] = par[k-1][par[k-1][i]]; F0R(x,2) F0R(z,2) { dists[k][i][x][z] = INF; F0R(y,2) ckmin(dists[k][i][x][z], dists[k-1][i][x][y]+dists[k-1][par[k-1][i]][y][z]); } } } int lca(int u, int v){ if (depth[u] < depth[v]) swap(u,v); F0Rd(k,MAXK) if (depth[u] >= depth[v]+(1<<k)) u = par[k][u]; F0Rd(k,MAXK) if (par[k][u] != par[k][v]) u = par[k][u], v = par[k][v]; if(u != v) u = par[0][u], v = par[0][v]; return u; } int getPar(int x, int y) { F0Rd(k,MAXK) if (depth[y] < depth[x]-(1<<k)) x = par[k][x]; assert(depth[y]+1 == depth[x]); return x; } array<array<double,2>,2> comb(array<array<double,2>,2> a, array<array<double,2>,2> b) { array<array<double,2>,2> res = array<array<double,2>,2>(); F0R(i,2) F0R(j,2) res[i][j] = INF; F0R(i,2) F0R(j,2) F0R(k,2) ckmin(res[i][k],a[i][j]+b[j][k]); return res; } array<array<double,2>,2> getDist(int x, int y) { assert(depth[x] >= depth[y]); array<array<double,2>,2> cur = array<array<double,2>,2>(); cur[0][1] = cur[1][0] = INF; F0Rd(k,MAXK) if (depth[y] <= depth[x]-(1<<k)) { cur = comb(cur,dists[k][x]); x = par[k][x]; } return cur; } /*int dist(int u, int v) { return depth[u]+depth[v]-2*depth[lca(u,v)]; }*/ }; LCA<MX> L; double solve(int a, int b) { int ea = oneEdge[a], eb = oneEdge[b]; // ps(a,b,oneEdge[a],oneEdge[b],ed[oneEdge[a]],ed[oneEdge[b]]); int x = L.lca(ea,eb), flag = 0; if (x == ea || x == eb) flag = 0; else { auto AA = L.getPar(ea,x), BB = L.getPar(eb,x); if (ed[AA][0] != ed[x][0] && ed[BB][1] != ed[x][1] && ed[AA][0] == ed[BB][1]) flag = 1; if (ed[AA][1] != ed[x][1] && ed[BB][0] != ed[x][0] && ed[AA][1] == ed[BB][0]) flag = 1; if (flag) { auto A = L.getDist(ea,AA), B = L.getDist(eb,BB); swap(B[0][1],B[1][0]); double ans = INF; // ps("HUH",ed[ea],ed[AA],ed[eb],ed[BB],A,B); F0R(i,2) F0R(j,2) F0R(k,2) F0R(l,2) { ckmin(ans,dist(a,ed[ea][i])+A[i][j]+dist(ed[AA][j],ed[BB][k])+B[k][l]+dist(ed[eb][l],b)); } return ans; } // ps("HUH",x,A,B,ed[A],ed[B]); } auto A = L.getDist(ea,x), B = L.getDist(eb,x); swap(B[0][1],B[1][0]); // ps("HA",ea,eb,x,A,B,dist(1,2)); double ans = INF; F0R(i,2) F0R(j,2) F0R(k,2) { ckmin(ans,dist(a,ed[ea][i])+A[i][j]+B[j][k]+dist(ed[eb][k],b)); } return ans; } int main() { setIO(); re(pos[1],pos[2],N); de[ed[1] = {1,2}] = 1; verts = 2, eds = 1; oneEdge[1] = oneEdge[2] = 1; vpi query; F0R(i,N) { char t; re(t); if (t == 'd') { int A,B; re(pos[++verts],A,B); if (!de.count({A,B})) swap(A,B); assert(de.count({A,B})); eds ++; de[ed[eds] = {A,verts}] = eds; eds ++; de[ed[eds] = {verts,B}] = eds; L.addEdge(de[{A,B}],eds-1); L.addEdge(de[{A,B}],eds); oneEdge[verts] = eds; } else { int A,B; re(A,B); query.pb({A,B}); } } // ps(ed[oneEdge[6]], ed[oneEdge[5]]); L.init(eds); cout << fixed << setprecision(3); trav(t,query) ps(solve(t.f,t.s)); } /* stuff you should look for * int overflow, array bounds * special cases (n=1?), set tle * do smth instead of nothing and stay organized */

Compilation message (stderr)

grad.cpp: In function 'void io::setIn(std::__cxx11::string)':
grad.cpp:117:35: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     void setIn(string s) { freopen(s.c_str(),"r",stdin); }
                            ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
grad.cpp: In function 'void io::setOut(std::__cxx11::string)':
grad.cpp:118:36: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     void setOut(string s) { freopen(s.c_str(),"w",stdout); }
                             ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...