voidMerge(int &x, int y, int l, int r){ // 你可以把两个线段树合并到一个新点上,这里我是把 y 合并到 x 上 if (!y) return; if (!x) return x = y, void(); // 有一个节点为空的情况 if (l == r) { // 到达边界,直接合并信息 ... return; } int mid = tr[x].l + tr[x].r >> 1; Merge(tr[x].ls, tr[y].ls, l, mid); Merge(tr[x].rs, tr[y].rs, mid + 1, r); pushup(x); // 合并左右子树并更新 }
inlineintread(){ int x = 0, f = 0; char c = 0; while (!isdigit(c)) f |= c == '-', c = getchar(); while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); return f ? -x : x; }
#define N 100010 #define pb push_back
int n, a[N], rt[N], res[N]; vector<int> Hash, e[N];
#define lid tr[id].ls #define rid tr[id].rs structSegment_tree { int l, r, ls, rs, sum; }tr[N * 20]; int cnt = 0; intNew(){return ++ cnt;} voidpushup(int id){ tr[id].sum = tr[lid].sum + tr[rid].sum; } voidAdd(int &id, int l, int r, int x){ if (!id) id = New(); tr[id].l = l, tr[id].r = r; if (l == r) { tr[id].sum ++; return; } int mid = tr[id].l + tr[id].r >> 1; if (x <= mid) Add(lid, l, mid, x); if (x > mid) Add(rid, mid + 1, r, x); pushup(id); } intAsk(int id, int l, int r){ if (!id) return0; if (l <= tr[id].l && tr[id].r <= r) { return tr[id].sum; } int mid = tr[id].l + tr[id].r >> 1, val = 0; if (l <= mid) val += Ask(lid, l, r); if (r > mid) val += Ask(rid, l, r); return val; } voidMerge(int &x, int y, int l, int r){ if (!x) return x = y, void(); if (!y) return; if (l == r) { tr[x].sum += tr[y].sum; return; } int mid = tr[x].l + tr[x].r >> 1; Merge(tr[x].ls, tr[y].ls, l, mid); Merge(tr[x].rs, tr[y].rs, mid + 1, r); pushup(x); }
voiddfs(int x, int fa){ Add(rt[x] = New(), 1, n, a[x]); for (auto y : e[x]) if (y != fa) { dfs(y, x); Merge(rt[x], rt[y], 1, n); } res[x] = Ask(rt[x], a[x] + 1, n); }
intmain(){ n = read();
for (int i = 1; i <= n; i ++) { a[i] = read(), Hash.pb(a[i]); } sort(Hash.begin(), Hash.end()); unique(Hash.begin(), Hash.end()); for (int i = 1; i <= n; i ++) { a[i] = lower_bound(Hash.begin(), Hash.end(), a[i]) - Hash.begin() + 1; }
for (int i = 2; i <= n; i ++) { int x = read(); e[i].pb(x), e[x].pb(i); }
dfs(1, 0);
for (int i = 1; i <= n; i ++) { printf("%d\n", res[i]); } return0; }
inlineintread(){ int x = 0, f = 0; char c = 0; while (!isdigit(c)) f |= c == '-', c = getchar(); while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); return f ? -x : x; }
#define N 100010 #define pb push_back
int n, a[N], rt[N]; longlong res[N]; vector<int> e[N];
structSegment_tree { int l, r, ls, rs; longlong Mx, sum; #define lid tr[id].ls #define rid tr[id].rs }tr[N * 20]; int cnt = 0; intNew(){return ++ cnt;} voidpushup(int id){ tr[id].Mx = max(tr[lid].Mx, tr[rid].Mx); if (tr[lid].Mx > tr[rid].Mx) tr[id].sum = tr[lid].sum; if (tr[lid].Mx < tr[rid].Mx) tr[id].sum = tr[rid].sum; if (tr[lid].Mx == tr[rid].Mx) tr[id].sum = tr[lid].sum + tr[rid].sum; } voidAdd(int &id, int l, int r, int x){ if (!id) id = New(); tr[id].l = l, tr[id].r = r; if (l == r) { tr[id].Mx = 1, tr[id].sum = x; return; } int mid = tr[id].l + tr[id].r >> 1; if (x <= mid) Add(lid, l, mid, x); if (x > mid) Add(rid, mid + 1, r, x); pushup(id); } voidMerge(int &x, int y, int l, int r){ if (!y) return; if (!x) return x = y, void(); if (l == r) { tr[x].Mx += tr[y].Mx; return; } int mid = l + r >> 1; Merge(tr[x].ls, tr[y].ls, l, mid); Merge(tr[x].rs, tr[y].rs, mid + 1, r); pushup(x); }
voiddfs(int x, int fa){ Add(rt[x] = New(), 1, n, a[x]); for (auto y : e[x]) if (y != fa) { dfs(y, x), Merge(rt[x], rt[y], 1, n); } res[x] = tr[rt[x]].sum; }
intmain(){ n = read(); for (int i = 1; i <= n; i ++) { a[i] = read(); } for (int i = 1; i < n; i ++) { int x = read(), y = read(); e[x].pb(y), e[y].pb(x); }
dfs(1, 0);
for (int i = 1; i <= n; i ++) { printf("%lld ", res[i]); } puts(""); return0; }
inlineintread(){ int x = 0, f = 0; char c = 0; while (!isdigit(c)) f |= c == '-', c = getchar(); while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); return f ? -x : x; }
#define N 400010 typedefint Ary[N];
int n, tot = 0; longlong res, s1, s2; Ary a, L, R, rt;
inlineintread(){ int x = 0, f = 0; char c = 0; while (!isdigit(c)) f |= c == '-', c = getchar(); while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); return f ? -x : x; }
#define N 100010 #define pb push_back #define PII pair<int, int> #define mp make_pair #define fi first #define se second
int n, m, res[N], f[N][22], rt[N], dep[N]; vector<int> e[N], root; vector<PII> q[N];
structSegment_tree { int l, r, ls, rs, sum; #define lid tr[id].ls #define rid tr[id].rs }tr[N * 20]; int cnt = 0; voidAdd(int &id, int l, int r, int x){ if (!id) id = ++ cnt; tr[id].l = l, tr[id].r = r; if (l == r) { tr[id].sum ++; return; } int mid = tr[id].l + tr[id].r >> 1; if (x <= mid) Add(lid, l, mid, x); if (x > mid) Add(rid, mid + 1, r, x); } intAsk(int id, int l, int r, int x){ if (!id) return0; if (l == r) return tr[id].sum; int mid = tr[id].l + tr[id].r >> 1; if (x <= mid) returnAsk(lid, l, mid, x); if (x > mid) returnAsk(rid, mid + 1, r, x); } voidMerge(int &x, int y, int l, int r){ if (!x || !y) return x += y, void(); if (l == r) { tr[x].sum += tr[y].sum; return; } int mid = tr[x].l + tr[x].r >> 1; Merge(tr[x].ls, tr[y].ls, l, mid); Merge(tr[x].rs, tr[y].rs, mid + 1, r); }
voiddfs2(int x, int fa){ Add(rt[x] = ++ cnt, 1, n, dep[x]); for (auto y : e[x]) if (y != fa) { dfs2(y, x), Merge(rt[x], rt[y], 1, n); } for (auto i : q[x]) { res[i.fi] = Ask(rt[x], 1, n, dep[x] + i.se) - 1; } }
voiddfs1(int x, int fa){ dep[x] = dep[fa] + 1; f[x][0] = fa; for (int i = 1; i <= 20; i ++) { f[x][i] = f[f[x][i - 1]][i - 1]; } for (auto y : e[x]) { if (y != fa) dfs1(y, x); } }
intmain(){ n = read(); for (int i = 1; i <= n; i ++) { int x = read(); if (x) e[x].pb(i), e[i].pb(x); else root.pb(i); }
for (auto x : root) dfs1(x, 0);
m = read(); for (int i = 1; i <= m; i ++) { int x = read(), y = read(), fa = x; for (int i = 0, j = y; i <= 20; i ++, j >>= 1) { if (j & 1) fa = f[fa][i]; } q[fa].pb(mp(i, y)); }
for (auto x : root) dfs2(x, 0);
for (int i = 1; i <= m; i ++) { printf("%d ", res[i]); } puts(""); return0; }