题目描述
给你一棵树,最开始点权为0,每次将与一个点x树上距离<=1的所有点点权+1,之后询问这些点修改后的点权和.
输入描述:
第一行两个数n和m
第二行n-1个数,第i个数fa[i + 1]表示i + 1点的父亲编号,保证fa[i + 1]<i + 1
第三行m个数,每个数x依次表示这次操作的点是x
输出描述:
输出一个数,即这m次操作的答案的hash值
如果是第i次操作,这次操作结果为ans,则这个hash值加上
i * ans
输出hash值对19260817取模的结果
示例1 输入 6 3 1 1 2 3 3 1 2 3 输出 34 示例2 输入 6 10 1 1 2 3 3 1 4 6 5 2 3 3 3 3 3 输出 869
解题思路
#pragma GCC target("avx,sse2,sse3,sse4,popcnt") #pragma GCC optimize("O2,O3,Ofast,inline,unroll-all-loops,-ffast-math") #include <bits/stdc++.h> using namespace std; #define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0) #define all(__vv__) (__vv__).begin(), (__vv__).end() #define endl "\n" #define pai pair<int, int> #define mk(__x__,__y__) make_pair(__x__,__y__) #define ms(__x__,__val__) memset(__x__, __val__, sizeof(__x__)) typedef long long ll; typedef unsigned long long ull; typedef long double ld; const int MOD = 19260817; const int INF = 0x3f3f3f3f; inline ll read() { ll s = 0, w = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') w = -1; for (; isdigit(ch); ch = getchar()) s = (s << 1) + (s << 3) + (ch ^ 48); return s * w; } inline void print(ll x) { if (!x) { putchar('0'); return; } char F[40]; ll tmp = x > 0 ? x : -x; if (x < 0)putchar('-'); int cnt = 0; while (tmp > 0) { F[cnt++] = tmp % 10 + '0'; tmp /= 10; } while (cnt > 0)putchar(F[--cnt]); } inline ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; } ll qpow(ll a, ll b) { ll ans = 1; while (b) { if (b & 1) ans *= a; b >>= 1; a *= a; } return ans; } ll qpow(ll a, ll b, ll mod) { ll ans = 1; while (b) { if (b & 1)(ans *= a) %= mod; b >>= 1; (a *= a) %= mod; }return ans % mod; } inline int lowbit(int x) { return x & (-x); } const int N = 1e5 + 7; ll son[N], father[N], fa[N]; int d[N], cnt[N]; int main() { int n = read(), m = read(); for (int i = 2; i <= n; ++i) { int x = read(); fa[i] = x; ++d[x], ++d[i]; } ll ans = 0; for (int i = 1; i <= m; ++i) { int x = read(); ++cnt[x]; // x节点的操作次数+1 son[x] = (son[x] + d[x]) % MOD; son[fa[x]] = (son[fa[x]] + 2) % MOD; son[fa[fa[x]]] = (son[fa[fa[x]]] + 1) % MOD; father[x] = (father[x] + 1) % MOD; father[fa[x]] = (father[fa[x]] + 1) % MOD; ans = (ans + i * (son[x] + father[fa[x]] + cnt[fa[x]] + cnt[fa[fa[x]]]) % MOD) % MOD; } print(ans); return 0; }