此题很有意思,有多种解法
1.用天天爱跑步的方法,进入子树的时候ans-query,出去子树的时候ans+query,query可以用树状数组或线段树来搞
2.按dfs序建立主席树
3.线段树的合并
前两个都会,于是学习一下线段树的合并。。
道理用文字解释不清。。。直接看代码就能看懂。。
可以脑补出,合并的操作复杂度是logn的,总时间复杂度nlogn
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 100001 int n, m, cnt, size; int head[N], to[N << 1], next[N << 1], root[N], sum[N * 20], ls[N * 20], rs[N * 20], ans[N], a[N], b[N]; inline int read() { int x = 0, f = 1; char ch = getchar(); for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1; for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0'; return x * f; } inline void add(int x, int y) { to[cnt] = y; next[cnt] = head[x]; head[x] = cnt++; } inline void insert(int &now, int l, int r, int x) { now = ++size; if(l == r) { sum[now] = 1; return; } int mid = (l + r) >> 1; if(x <= mid) insert(ls[now], l, mid, x); else insert(rs[now], mid + 1, r, x); sum[now] = sum[ls[now]] + sum[rs[now]]; } inline void merge(int &x, int y) { if(!x || !y) { x += y; return; } sum[x] += sum[y]; merge(ls[x], ls[y]); merge(rs[x], rs[y]); } inline int query(int now, int l, int r, int x, int y) { if(x <= l && r <= y) return sum[now]; int mid = (l + r) >> 1, ret = 0; if(x <= mid) ret += query(ls[now], l, mid, x, y); if(mid < y) ret += query(rs[now], mid + 1, r, x, y); return ret; } inline void dfs(int u) { int i, v; for(i = head[u]; i ^ -1; i = next[i]) { v = to[i]; dfs(v); merge(root[u], root[v]); } ans[u] = query(root[u], 1, m, a[u] + 1, m); } int main() { int i, x; n = read(); memset(head, -1, sizeof(head)); for(i = 1; i <= n; i++) a[i] = b[i] = read(); for(i = 2; i <= n; i++) { x = read(); add(x, i); } std::sort(b + 1, b + n + 1); m = std::unique(b + 1, b + n + 1) - b - 1; for(i = 1; i <= n; i++) a[i] = std::lower_bound(b + 1, b + m + 1, a[i]) - b; for(i = 1; i <= n; i++) insert(root[i], 1, m, a[i]); dfs(1); for(i = 1; i <= n; i++) printf("%d ", ans[i]); return 0; }