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
| #include <bits/stdc++.h> using namespace std;
#define FILE_IO namespace io {...}; using namespace io;
const long long mod = 998244353; long long quickPow(long long x, long long p) { long long res = 1; while (p) { if (p & 1LL) (res *= x) %= mod; (x *= x) %= mod; p >>= 1; } return res; } const long long inv = quickPow(10000, mod - 2);
const int maxn = 3e5 + 5; int n, bcnt; long long a[maxn], b[maxn]; vector<int> tree[maxn];
struct node; typedef node* pos; struct node { pos ls, rs; int l, r; long long val, tag; node() { ls = rs = this; l = r = 0, val = 0, tag = 1; } } buf[maxn << 5], *bufPos = buf; pos root[maxn];
pos newNode(int l, int r) { pos p = ++bufPos; p -> ls = p -> rs = buf; p -> l = l, p -> r = r; p -> val = 0, p -> tag = 1; return p; }
void updateOne(pos p, long long tag) { if (p == buf) return; (p -> tag *= tag) %= mod; (p -> val *= tag) %= mod; }
void pushUp(pos p) { p -> val = (p -> ls -> val + p -> rs -> val) % mod; }
void pushDown(pos p) { if (p != buf && p -> tag != 1) { updateOne(p -> ls, p -> tag); updateOne(p -> rs, p -> tag); p -> tag = 1; } }
void insert(pos p, int x) { if (p -> l == p -> r) { p -> val = 1; return; } pushDown(p); int mid = (p -> l + p -> r) >> 1; if (x <= mid) { if (p -> ls == buf) p -> ls = newNode(p -> l, mid); insert(p -> ls, x); } else { if (p -> rs == buf) p -> rs = newNode(mid + 1, p -> r); insert(p -> rs, x); } pushUp(p); }
pos merge(pos p, pos q, long long pPre, long long pSuf, long long qPre, long long qSuf, long long pMax) { if (p == buf && q == buf) return buf; if (p == buf) { updateOne(q, (pMax * pPre + (1 + mod - pMax) * pSuf) % mod); return q; } else if (q == buf) { updateOne(p, (pMax * qPre + (1 + mod - pMax) * qSuf) % mod); return p; } else { pushDown(p), pushDown(q); long long pL = p -> ls -> val, pR = p -> rs -> val, qL = q -> ls -> val, qR = q -> rs -> val; p -> ls = merge(p -> ls, q -> ls, pPre, (pSuf + pR) % mod, qPre, (qSuf + qR) % mod, pMax); p -> rs = merge(p -> rs, q -> rs, (pPre + pL) % mod, pSuf, (qPre + qL) % mod, qSuf, pMax); pushUp(p); return p; } }
void solve(int u) { if (tree[u].empty()) { root[u] = newNode(1, bcnt); insert(root[u], a[u]); } else if (tree[u].size() == 1) { solve(tree[u].back()); root[u] = root[tree[u].back()]; } else { solve(tree[u][0]); solve(tree[u][1]); root[u] = merge(root[tree[u][0]], root[tree[u][1]], 0, 0, 0, 0, a[u]); } }
long long ans; void calc(pos p) { if (p == buf) return; if (p -> l == p -> r) { (ans += b[p -> l] * p -> l % mod * p -> val % mod * p -> val) %= mod; return; } else { pushDown(p); calc(p -> ls); calc(p -> rs); } }
int main() { read(n); for (int i = 1; i <= n; i++) { int f; read(f); if (f) tree[f].emplace_back(i); } for (int i = 1; i <= n; i++) { read(a[i]); if (tree[i].empty()) { b[++bcnt] = a[i]; } } sort(b + 1, b + bcnt + 1); for (int i = 1; i <= n; i++) { if (tree[i].empty()) { a[i] = lower_bound(b + 1, b + bcnt + 1, a[i]) - b; } else { (a[i] *= inv) %= mod; } } solve(1); calc(root[1]); write(ans); return 0; }
|