牛客练习赛6 B-点权和

#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string.h>
using namespace std;
#define maxn 100010
#define LL long long
#define mod 19260817
int f[maxn] ;
int sz[maxn];
LL sum[maxn],tag[maxn],sontag[maxn];
int main()
{
#ifdef shuaishuai
    freopen("in.txt","r",stdin );
    //freopen("out.txt","w",stdout);
#endif // shuaishuai
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=2,x;i<=n;i++)
    {
        scanf("%d",&x);
        f[i]=x;sz[x]++;sz[i]++;
    }
    LL ans=0;
    for(LL i=1;i<=m;i++)
    {
        int x;
        scanf("%d",&x);
        sum[x]+=sz[x]+1;
        sum[f[x]]+=2;
        sum[f[f[x]]]++;
        tag[x]++;
        sontag[f[x]]++;
        LL t=0;//巧妙的容斥- -
        t=(t+sum[x])%mod;//对x操作,x的子节点操作,x的子节点的子节点操作的贡献和
        t=(t+tag[f[x]]*2)%mod;//对fx操作给fx和x带来的累计贡献
        t=(t+tag[f[f[x]]])%mod;//对ffx操作给fx带来的贡献
        t=(t+sontag[f[x]]-tag[x])%mod;//fx子节点中除去x的贡献
        ans=(ans+i*t)%mod;
    }
    printf("%lld\n",ans );
    return 0;
}

 

posted @ 2017-11-18 00:23  MeowMeowMeow  阅读(278)  评论(1编辑  收藏  举报