From 6f5fc106f3040de34ab368e18198509b363bf9b3 Mon Sep 17 00:00:00 2001 From: Ta180m Date: Tue, 3 Sep 2019 21:31:25 -0500 Subject: Create newbarn.cpp --- 2018/February/Platinum/newbarn.cpp | 78 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 2018/February/Platinum/newbarn.cpp diff --git a/2018/February/Platinum/newbarn.cpp b/2018/February/Platinum/newbarn.cpp new file mode 100644 index 0000000..e702480 --- /dev/null +++ b/2018/February/Platinum/newbarn.cpp @@ -0,0 +1,78 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define init_io(pname) ifstream cin((string)pname+".in"); ofstream cout((string)pname+".out"); ios_base::sync_with_stdio(false); cin.tie(NULL) +#define FOR(i, a, b, in) for (int i = (a); i < (b); i += in) +#define REP(i, a, b) for (int i = (a); i < (b); i++) +#define RFOR(i, a, b, in) for (int i = (a) - 1; i >= (b); i -= in) +#define RREP(i, a, b) for (int i = (a) - 1; i >= (b); i--) +#define trav(a, x) for (auto& a : x) +#define mp make_pair +#define pb push_back +#define lb lower_bound +#define ub upper_bound +#define sz(x) (int)x.size() +#define all(x) begin(x), end(x) +#define rsz resize +#define mem(a, b) memset(a, (b), sizeof(a)) +using namespace std; +typedef string str; +typedef long long ll; +typedef long double ld; +typedef pair ii; typedef pair pl; typedef pair pd; +typedef vector vi; typedef vector vl; typedef vector vd; +typedef vector vii; typedef vector vpl; typedef vector vpd; +constexpr auto INF = (int)1e9; +constexpr auto LINF = (ll)1e18; + +int S[100005], D[100005], L[100005][16] = { 0 }; +ii P[100005]; + +int lca(int u, int v) { + if (D[u] > D[v]) swap(u, v); + for (int i = 15; i >= 0; i--) if (D[v] - (1 << i) >= D[u]) v = L[v][i]; + if (u == v) return u; + for (int i = 15; i >= 0; i--) if (L[u][i] && L[u][i] != L[v][i]) u = L[u][i], v = L[v][i]; + return L[u][0]; +} + +inline int dist(int u, int v) { return D[u] + D[v] - 2 * D[lca(u, v)]; } + +int main() { + init_io("newbarn"); + + int Q; + cin >> Q; + int u = 0, s = 0; + while (Q--) { + char c; + cin >> c; + if (c == 'B') { + int p; + cin >> p; + u++; + D[u] = (p != -1 ? D[p] + 1 : 0); + S[u] = (p != -1 ? S[p] : s++); + for (int v = p, i = 0; v > 0 && i < 16; v = L[v][i++]) L[u][i] = v; + if (p != -1) { + if (dist(u, P[S[u]].first) > dist(P[S[u]].first, P[S[u]].second)) P[S[u]].second = u; + else if (dist(u, P[S[u]].second) > dist(P[S[u]].first, P[S[u]].second)) P[S[u]].first = u; + } + else P[S[u]] = ii(u, u); + } + else { + int k; + cin >> k; + cout << max(dist(k, P[S[k]].first), dist(k, P[S[k]].second)) << endl; + } + } +} \ No newline at end of file -- cgit v1.2.3-70-g09d2