inline LL power(LL x, int p){ LL cnt = 1; while (p) { if (p & 1) cnt = cnt * x % MOD; x = x * x % MOD; p >>= 1; } return cnt; }
structLFS {int to, next; } ; LFS Link[MAXN << 1]; int Head[MAXN]= {0}, size = 0; voidInsert(int u, int v){ Link[++ size].to = v; Link[size].next = Head[u];
Head[u] = size; }
int N; int a[MAXN]= {0}, son[MAXN]= {0}; int b[MAXN]= {0}, m = 0; LL ans[MAXN]= {0}; LL inv; LL f1[MAXN]= {0}, f2[MAXN]= {0}; inline LL calc(int p){ int base = p / M, rest = p - base * M; return f2[base] * f1[rest] % MOD; } LL work(int root, int t){ LL r; if (t == 1) r = a[root] + 1; else r = (1 - calc (a[root] + 1) + MOD) % MOD * inv % MOD; for (int i = Head[root]; i; i = Link[i].next) { int v = Link[i].to; r = r * work (v, t) % MOD; } if (t == son[root]) ans[root] = r; return r; }
inlineintgetnum(){ int num = 0; char ch = getchar (); while (! isdigit (ch)) ch = getchar (); while (isdigit (ch)) num = (num << 3) + (num << 1) + ch - '0', ch = getchar (); return num; }
intmain(){ N = getnum (); for (int i = 2; i <= N; i ++) { int fa = getnum (); Insert (fa, i); son[fa] ++; } for (int i = 1; i <= N; i ++) a[i] = getnum (); for (int i = 1; i <= N; i ++) b[++ m] = son[i]; sort (b + 1, b + m + 1); m = unique (b + 1, b + m + 1) - b - 1; for (int i = 1; i <= m; i ++) { inv = power (1 - b[i] + MOD, MOD - 2); f1[0] = 1, f2[0] = 1; for (int j = 1; j <= M; j ++) f1[j] = f1[j - 1] * b[i] % MOD; for (int j = 1; j <= M; j ++) f2[j] = f2[j - 1] * f1[M] % MOD; work (1, b[i]); } for (int i = 1; i <= N; i ++) printf ("%lld\n", ans[i]);
structLFS {int to, w, next; } ; LFS Link[MAXN << 1]; int Head[MAXN]= {0}, size = 0; voidInsert(int u, int v, int w){ Link[++ size].to = v; Link[size].w = w; Link[size].next = Head[u];
Head[u] = size; }
int N, M;
int son[MAXN]= {0}, subsize[MAXN]= {0}; int d[MAXN]= {0}; LL dist[MAXN]= {0}, mpp[MAXN]= {0}, m = 0; voidDFS(int root, int father){ subsize[root] = 1; mpp[++ m] = dist[root]; son[root] = - 1; for (int i = Head[root]; i; i = Link[i].next) { int v = Link[i].to, w = Link[i].w; if (v == father) continue; dist[v] = dist[root] + w; DFS (v, root); subsize[root] += subsize[v]; if (son[root] == - 1 || subsize[v] > subsize[son[root]]) son[root] = v; } } vector<pair<int, int> > mat[MAXN]; // <纵坐标, 横坐标> set<int> st[MAXN]; set<int> :: iterator it, it2; int ext[MAXN]= {0}; voidmerge(int x, int y, int id){ for (it = st[y].begin(); it != st[y].end(); it ++) { int i = * it; it2 = st[x].lower_bound(i); int pre = i, nxt = i; if (it2 != st[x].begin()) { it2 --; pre = * it2; } it2 = st[x].upper_bound(i); if (it2 != st[x].end()) nxt = * it2; if (nxt != i) mat[id].push_back(make_pair (nxt, i)); if (pre != i) mat[id].push_back(make_pair (i, pre)); } for (it = st[y].begin(); it != st[y].end(); it ++) st[x].insert(* it); } voidmake(int root, int father){ if (son[root] == - 1) { st[root].insert(root); ext[root] = root; return ; } make (son[root], root); ext[root] = ext[son[root]]; st[ext[root]].insert(root); for (int i = Head[root]; i; i = Link[i].next) { int v = Link[i].to; if (v == father || v == son[root]) continue; make (v, root); merge (ext[root], ext[v], d[root]); } } structnode { int x, y, type, id; // type: - 1, + 1, 2(dot) node (int x = 0, int y = 0, int type = 0, int id = 0) : x (x), y (y), type (type), id (id) {} booloperator < (const node& p) const { if (x == p.x) return type < p.type; return x < p.x; } } ; node a[MAXM << 2]; int n = 0; voidmesh(int id){ sort (mat[id].begin(), mat[id].end()); int lx = 1; for (int i = 0; i < (int) mat[id].size(); i ++) { int y = mat[id][i].first, x = mat[id][i].second; if (x >= lx) { a[++ n] = node (lx, y, 1, id); a[++ n] = node (x + 1, y, - 1, id); lx = x + 1; } } } int answer[MAXM]= {0}; inlineintlowbit(int x){ return x & (- x); } int subsum[MAXN]= {0}; voidadd(int x, int delta){ while (x <= N) { subsum[x] += delta; x += lowbit (x); } } intquery(int x){ int ret = 0; while (x > 0) { ret += subsum[x]; x -= lowbit (x); } return ret; } voidsolve(){ sort (a + 1, a + n + 1); for (int i = 1; i <= n; i ++) { int y = a[i].y, type = a[i].type, id = a[i].id; if (type == 2) answer[id] = query (y); else add (y, type); } }
inlineintgetnum(){ int num = 0; char ch = getchar (); bool isneg = false; while (! isdigit (ch)) { if (ch == '-') isneg = true; ch = getchar (); } while (isdigit (ch)) num = (num << 3) + (num << 1) + ch - '0', ch = getchar (); return isneg ? - num : num; }
intmain(){ N = getnum (), M = getnum (); for (int i = 1; i < N; i ++) { int u = getnum (), v = getnum (), w = getnum (); Insert (u, v, w), Insert (v, u, w); } DFS (1, 0); sort (mpp + 1, mpp + m + 1); m = unique (mpp + 1, mpp + m + 1) - mpp - 1; for (int i = 1; i <= N; i ++) d[i] = lower_bound (mpp + 1, mpp + m + 1, dist[i]) - mpp; for (int i = 1; i <= N; i ++) mat[d[i]].push_back(make_pair (i, i)); make (1, 0); for (int i = 1; i <= m; i ++) mesh (i); for (int q = 1; q <= M; q ++) { int l = getnum (), r = getnum (); a[++ n] = node (l, r, 2, q); } solve (); for (int i = 1; i <= M; i ++) printf ("%d\n", answer[i]);