FOJ2277(dfs序 + 树状数组区间更新)

本文介绍了一种使用树状数组来优化树形结构中特定操作的方法。通过将树的操作映射到树状数组上,可以高效地进行区间更新和查询。文章详细解释了解题思路,并提供了完整的代码实现。

Problem 2277 Change
Accept: 91 Submit: 450
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

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

解题思路:这种东西肯定是要把对树的操作弄到一个数据结构上去维护,刚开始想用树链剖分,但是因为更新一个点,以这个节点为根的子树都要改变,这样话,每次更新可能改变的链有很多条,于是树链剖分肯定是不行的,我们考虑一下dfs序,因为dfs序把一棵子树上的点都在一段连续的区间,那么到底要维护什么东西呢?

我们考虑一般情况,假设我们当前更新v节点,其中x = x1, k = k1,v节点子树上的一个节点s,于是v节点更新的值就是x1 + (depth[v] - depth[s]) * k1,其中depth[i]表示节点i的深度,我们化简上面式子可以得到为:x1 + depth[v] * k1 - depth[s]k1,观察式子可以得到,对于一次更新,每个受影响的节点(以v为根的子树上所有的节点)都加上了一个相同的数x1 + depth[v] k1,然后还要减去一个数k1 * depth[s],前者我们可以用树状数组区间维护更新维护就行,后者也同样可以这样维护,因为每个节点的depth[s]是不变的,每次更新改变的只是k的值,而且k的值是满足可加减性的,所以我们也要用树状数组维护一下k的值。下面简单的证明一下上面的做法:
假设更新节点v,x = x1, k = k1,更新节点u, x = x2, k = k2.节点s既是v,又是u的子树上的节点,我们单独考虑节点u, v更新对节点s的影响,先考虑v:a[v] = x1 + depth[v] * k1 - depth[s] * k1,a[u] = x2 + depth[u] * k2 - depth[s] * k2;那么我们把两者相加可以得到a[v] + a[u] = x1 + x2 + depth[v]k1 + depth[u] k2 - (k1 + k2) * depth[s],黑体的部分就表明了k的可加性了。除了树状数组,线段树也应该能够做。

#include <iostream>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int maxn = 3e5 + 10;
int n, q;
int depth[maxn];
int Rank[maxn];
vector<int> g[maxn];
ll TreeK[maxn];
ll TreeX[maxn];
int L[maxn];
int R[maxn];
int res;
int root;
void init()
{
    res = 0;
    root = 1;
    memset(TreeK, 0, sizeof(TreeK));
    memset(TreeX, 0, sizeof(TreeX));
    for(int i = 1; i <= n; i++)
    {
        g[i].clear();
    }
}
int lowbit(int x)
{
    return x&(-x);
}
void update(int loc, ll value, int type)
{
    value = (value + mod) % mod;//这里要注意,我在这里wa了很多次
    if(type == 0)
    {
        for(int i = loc; i <= res; i += lowbit(i))
        {
            TreeK[i] = (TreeK[i] + value) % mod;
        }
    }
    else
    {
        for(int i = loc; i <= res; i += lowbit(i))
        {
            TreeX[i] = (TreeX[i] + value) % mod;
        }
    }
}
ll sum(int loc, int type)
{
    ll ans = 0;
    if(type == 0)
    {
        for(int i = loc; i >= 1; i -= lowbit(i))
        {
            ans = (ans + TreeK[i]) % mod;
        }
    }
    else
    {
        for(int i = loc; i >= 1; i -= lowbit(i))
        {
            ans = (ans + TreeX[i]) % mod;;
        }
    }
    return ans;
}
void dfs(int u, int f, int d)
{
    depth[u] = d;
    L[u] = ++res;
    Rank[res] = u;
    for(int i = 0; i < g[u].size(); i++)
    {
        int v = g[u][i];
        if(v == f) continue;
        dfs(v, u, d + 1);
    }
    R[u] = res;
}
int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d", &n);
        init();
        int pi;
        for(int i = 2; i <= n; i++)
        {
            scanf("%d", &pi);
            g[pi].push_back(i);
        }
        dfs(root, 1, 0);
        scanf("%d", &q);
        int op, v;
        ll x, k;
        for(int i = 1; i <= q; i++)
        {
           scanf("%d", &op);
           if(op == 1)
           {
               scanf("%d%I64d%I64d", &v, &x, &k);
               x = (x + depth[Rank[L[v]]] * k) % mod;
               update(L[v], k, 0);
               update(R[v] + 1, -k, 0);
               update(L[v], x, 1);
               update(R[v] + 1, -x, 1);
           }
           else
           {
               scanf("%d", &v);
               ll ans1 = sum(L[v], 1);
               ll ans2 = sum(L[v], 0);
               ans1 %= mod;
               ans2 %= mod;
               ans1 = (ans1 - (depth[Rank[L[v]]] * ans2) % mod + mod) % mod;
               printf("%I64d\n", ans1);
           }
        }
    }
    return 0;
}



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值