1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
#include <cctype> #include <cstdio> #include <vector> #include <cstring> #include <algorithm> using namespace std;
namespace IO { const int MAXSIZE = 1 << 18 | 1; char buf[MAXSIZE], *p1, *p2;
inline int Gc() { return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin), p1 == p2)? EOF: *p1++; } template<typename T> inline void read(T& x) { x = 0; int f = 0, ch = Gc(); while (!isdigit(ch)) f |= ch == '-', ch = Gc(); while (isdigit(ch)) x = x * 10 + ch - '0', ch = Gc(); if (f) x = -x; } } using IO::read;
typedef long long LL; const int MAXN = 3e5 + 5, LOG = 20;
struct Ask { int idx, t; Ask() = default; Ask(int _i, int _t): idx(_i), t(_t) { } };
int n, q; int pre[MAXN], W[MAXN];
int Mx[LOG][MAXN], anc[LOG][MAXN]; int depth[MAXN], son[MAXN], dfn[MAXN], clk;
LL Ans[MAXN]; vector<Ask> Q[MAXN];
namespace Graph { struct Edge { int nxt, to; } edges[MAXN]; int head[MAXN], d[MAXN], eidx;
inline void init() { memset(head, -1, sizeof head), eidx = 1; } inline void AddEdge(int from, int to) { edges[++eidx] = (Edge){ head[from], to }, head[from] = eidx; }
void dfs(int u) { depth[u] = depth[pre[u]] + 1; for (int j = 1; (1 << j) <= n; ++j) if (anc[j - 1][u] > 0) { anc[j][u] = anc[j - 1][anc[j - 1][u]]; Mx[j][u] = max(Mx[j - 1][u], Mx[j - 1][anc[j - 1][u]]); } for (int v, i = head[u]; ~i; i = edges[i].nxt) { dfs(v = edges[i].to); if (d[v] > d[son[u]]) son[u] = v; } d[u] = d[son[u]] + 1; }
inline void solve(int rt) { d[0] = -1; for (int u = 1; u <= n; ++u) Mx[0][u] = W[u], anc[0][u] = pre[u]; dfs(rt); } }
inline int QryMax(int u, const int& v) { int ret = 0; for (int j = LOG - 1; j >= 0; --j) if (depth[anc[j][u]] >= depth[v]) ret = max(ret, Mx[j][u]), u = anc[j][u]; return ret; }
namespace Que { struct Node { int d, w; Node(int _d = 0, int _w = 0): d(_d), w(_w) { } } Qu[MAXN], stk[MAXN];
LL s[MAXN]; int Head[MAXN], Tail[MAXN];
inline void newnode(const int& u, int head, int tail) { Head[u] = head, Tail[u] = tail; }
void Ins(int u, const Node& v) { int &head = Head[u], &tail = Tail[u]; while (head <= tail && Qu[head].w <= v.w) ++head; if (head > tail || Qu[head].d > v.d) { s[head - 1] = 0; if (head <= tail) s[head - 1] = s[head] + 1LL * Qu[head].d * (Qu[head].w - v.w); Qu[--head] = v; } }
void Mrg(const int& u, const int& v) { int &hu = Head[u], &tu = Tail[u], &hv = Head[v], &tv = Tail[v]; int top = 0; while (hu <= tu && Qu[hu].d < Qu[tv].d) stk[++top] = Qu[hu++]; while (top > 0 && hv <= tv) { if (Qu[tv].d > stk[top].d) Ins(u, Qu[tv--]); else Ins(u, stk[top--]); } while (top > 0) Ins(u, stk[top--]); while (hv <= tv) Ins(u, Qu[tv--]); } }
void dfs(int u) { using Graph::edges; dfn[u] = ++clk; if (son[u] > 0) dfs(son[u]), Que::newnode(u, Que::Head[son[u]], Que::Tail[son[u]]); else Que::newnode(u, clk, clk - 1); for (int v, i = Graph::head[u]; ~i; i = edges[i].nxt) if ((v = edges[i].to) != son[u]) dfs(v), Que::Mrg(u, v); for (const Ask& a: Q[u]) { using namespace Que; const int &head = Head[u], &tail = Tail[u]; int L = head, R = tail, w = QryMax(pre[a.t], u); while (L < R) { int Mid = (L + R) / 2; if (Qu[Mid].w < w) L = Mid + 1; else R = Mid; } if (Qu[head].w <= w) { Ans[a.idx] += s[head] - s[L] + 1LL * Qu[head].d * Qu[head].w; Ans[a.idx] += 1LL * Qu[L].d * (w - Qu[L].w) - 1LL * w * depth[u]; } else { Ans[a.idx] += 1LL * w * (Qu[L].d - depth[u]); } Ans[a.idx] += depth[a.t] - depth[u]; } Que::Ins(u, Que::Node(depth[u], W[u])); }
int main() { #ifndef ONLINE_JUDGE freopen("data/ex_rewrite3.in", "r", stdin); #endif Graph::init();
read(n); for (int i = 1; i <= n; ++i) read(W[i]); for (int i = 2; i <= n; ++i) read(pre[i]), Graph::AddEdge(pre[i], i); read(q); for (int s, t, i = 1; i <= q; ++i) read(s), read(t), Q[s].push_back(Ask(i, t));
Graph::solve(1), dfs(1);
for (int i = 1; i <= q; ++i) printf("%lld\n", Ans[i]); return 0; }
|