FZU oj 2277 Change 树状数组+dfs序

Posted xjhz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FZU oj 2277 Change 树状数组+dfs序相关的知识,希望对你有一定的参考价值。

Problem 2277 Change


Time Limit: 2000 mSec    Memory Limit : 262144 KB

技术分享 Problem Description

There is a rooted tree with n nodes, number from 1-n. Root’s number is 1.Each node has a value ai.

Initially all the node’s value is 0.

We have q operations. There are two kinds of operations.

1 v x k : a[v]+=x , a[v’]+=x-k (v’ is child of v) , a[v’’]+=x-2*k (v’’ is child of v’) and so on.

2 v : Output a[v] mod 1000000007(10^9 + 7).

技术分享 Input

First line contains an integer T (1 ≤ T ≤ 3), represents there are T test cases.

In each test case:

The first line contains a number n.

The second line contains n-1 number, p2,p3,…,pn . pi is the father of i.

The third line contains a number q.

Next q lines, each line contains an operation. (“1 v x k” or “2 v”)

1 ≤ n ≤ 3*10^5

1 ≤ pi < i

1 ≤ q ≤ 3*10^5

1 ≤ v ≤ n; 0 ≤ x < 10^9 + 7; 0 ≤ k < 10^9 + 7

技术分享 Output

For each operation 2, outputs the answer.

技术分享 Sample Input

1 3 1 1 3 1 1 2 1 2 1 2 2

技术分享 Sample Output

2 1

技术分享 Source

第八届福建省大学生程序设计竞赛-重现赛(感谢承办方厦门理工学院)

 lld wa一天

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define LL __int64
#define pi (4*atan(1.0))
#define eps 1e-8
#define bug(x)  cout<<"bug"<<x<<endl;
const int N=3e5+10,M=2e6+10,inf=1e9+10;
const LL INF=1e18+10,mod=1e9+7;

struct isss
{
    int v,nex;
}edge[N<<1];
int head[N],edg;
int in[N],out[N],tot;
LL deep[N];
void add(int u,int v)
{
    ++edg;
    edge[edg].v=v;
    edge[edg].nex=head[u];
    head[u]=edg;
}
void dfs(int u,int fa,int dp)
{
    in[u]=++tot;
    deep[u]=dp;
    for(int i=head[u];i;i=edge[i].nex)
    {
        int v=edge[i].v;
        if(v==fa)continue;
        dfs(v,u,dp+1);
    }
    out[u]=tot;
}
struct AYT
{
    LL tree[N];
    void init()
    {
        memset(tree,0,sizeof(tree));
    }
    int lowbit(int x)
    {
        return x&-x;
    }
    void update(int x,LL c)
    {
        while(x<N)
        {
            tree[x]+=c;
            x+=lowbit(x);
        }
    }
    LL query(int x)
    {
        LL ans=0;
        while(x)
        {
            ans+=tree[x];
            x-=lowbit(x);
        }
        return ans;
    }
}TX,TK;

void init()
{
    tot=0;
    memset(head,0,sizeof(head));
    memset(deep,0,sizeof(deep));
    TX.init();
    TK.init();
    edg=0;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        init();
        for(int i=2;i<=n;i++)
        {
            int f;
            scanf("%d",&f);
            add(f,i);
            add(i,f);
        }
        dfs(1,-1,0);
        int q;
        scanf("%d",&q);
        while(q--)
        {
            int t,v;
            LL x,k;
            scanf("%d%d",&t,&v);
            if(t==1)
            {
                scanf("%I64d%I64d",&x,&k);
                x+=1LL*deep[v]*k;
                x%=mod;
                TX.update(in[v],x);
                TX.update(out[v]+1,-x);
                TK.update(in[v],k);
                TK.update(out[v]+1,-k);
            }
            else
            {
                LL xx=TX.query(in[v]);
                LL y=TK.query(in[v]);
                y%=mod;
                LL ans=xx-1LL*deep[v]*y;
                ans%=mod;ans+=mod;ans%=mod;
                printf("%I64d\n",ans);
            }
        }
    }
    return 0;
}

 

以上是关于FZU oj 2277 Change 树状数组+dfs序的主要内容,如果未能解决你的问题,请参考以下文章

Change FZU - 2277 毒瘤啊 毒瘤题目

FZU 2277Change

FZU-2134 上车(树状数组)

HDU 2277 Change the ball

hrbust oj 1526+2028 树状数组

uestc oj 1217 The Battle of Chibi (dp + 离散化 + 树状数组)