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
题意
节点u加x,节点u的儿子u′加x−k,节点u的孙子u′′加x−2×k,依次类推。  看数据范围标准的线段树
但是这个k不好处理 这个k和深度有关系
所以这题在初始化处理的时候 1LL * x + 1LL * dep[v]*k
这样就便于我们查询了
 #include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#define rtl rt<<1
#define rtr rt<<1|1
#define mem(a,b) memset(a,b,sizeof(a))
#define fuck(x) cout<<"["<<x<<"]"<<endl
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define FIN freopen("DATA.txt","r",stdin)
#define read(x) scanf("%d", &x)
using namespace std;
typedef long long LL;
const int maxn = 1e6 + ;
const int mod = 1e9 + ;
int t, n, q, tot, dfscnt, head[maxn], L[maxn], R[maxn], dep[maxn];
struct Edge {
int v, nxt;
} edge[maxn << ];
void add(int u, int v) {
edge[tot].v = v;
edge[tot].nxt = head[u];
head[u] = tot++;
}
void dfs(int u, int fa, int d) {
L[u] = ++dfscnt;
dep[u] = d;
for (int i = head[u] ; ~i ; i = edge[i].nxt) {
int v = edge[i].v;
if (v != fa) dfs(v, u, d + );
}
R[u] = dfscnt;
}
struct node {
int l, r;
LL num, k;
int mid() {
return (l + r) >> ;
}
} tree[maxn << ];
void build(int l, int r, int rt) {
tree[rt].l = l, tree[rt].r = r;
tree[rt].num = tree[rt].k = ;
if (l == r) return ;
int m = (l + r) >> ;
build(l, m, rtl);
build(m + , r, rtr);
}
void pushdown(int rt) {
if (tree[rt].num != ) {
tree[rtl].num = (tree[rt].num + tree[rtl].num) % mod;
tree[rtr].num = (tree[rt].num + tree[rtr].num) % mod;
tree[rt].num = ;
}
if (tree[rt].k != ) {
tree[rtl].k = (tree[rt].k + tree[rtl].k) % mod;
tree[rtr].k = (tree[rt].k + tree[rtr].k) % mod;
tree[rt].k = ;
}
} void update(LL a, LL b, int L, int R, int rt) {
if (L <= tree[rt].l && tree[rt].r <= R) {
tree[rt].num = ((tree[rt].num + a) % mod + mod) % mod;
tree[rt].k = ((tree[rt].k + b) % mod + mod) % mod;
return ;
}
pushdown(rt);
int mid = tree[rt].mid();
if (R <= mid) update(a, b, L, R, rtl);
else if(L > mid) update(a, b, L, R, rtr);
else {
update(a, b, L, mid, rtl);
update(a, b, mid + , R, rtr);
}
}
LL query(int pos, int d, int rt) {
if (tree[rt].l == tree[rt].r) {
return ((tree[rt].num - d * tree[rt].k % mod) % mod + mod) % mod;
}
pushdown(rt);
int mid = tree[rt].mid();
if (pos <= mid) return query(pos, d, rtl);
return query(pos, d, rtr);
}
int main() {
int op, v, x, k;
sf(t);
while(t--) {
sf(n);
for (int i = ; i <= n ; i++) head[i] = -;
tot = ;
for (int i = ; i <= n ; i++) {
sf(v);
add(v, i);
add(i, v);
}
dfscnt = ;
dfs(, -, );
build(, n, );
sf(q);
while(q--) {
sf(op);
if (op == ) {
sfff(v, x, k);
update(1LL * x + 1LL * dep[v]*k, k, L[v], R[v], );
} else {
sf(v);
printf("%lld\n", query(L[v], dep[v], ));
}
}
}
return ;
}
												

Change FZU - 2277 毒瘤啊 毒瘤题目的更多相关文章

  1. F - Change FZU - 2277 (DFS序+线段树)

    题目链接: F - Change FZU - 2277 题目大意: 题意: 给定一棵根为1, n个结点的树. 有q个操作,有两种不同的操作 (1) 1 v k x : a[v] += x, a[v ' ...

  2. FZU 2277 Change(dfs序+树状数组)

    Problem Description There is a rooted tree with n nodes, number from 1-n. Root’s number is 1.Each no ...

  3. 【FZU 2277】Change

    题意 有一颗有n个节点的有根树,根节点编号时1,每个结点都有一个值ai,开始的时候,所有节点的值都是0. 我们有q个操作,操作只有两种类型 1 v x k,a[v]+=x,a[v']+=x-k,a[v ...

  4. 【HNOI 2018】毒瘤

    Problem Description 从前有一名毒瘤. 毒瘤最近发现了量产毒瘤题的奥秘.考虑如下类型的数据结构题:给出一个数组,要求支持若干种奇奇怪怪的修改操作(例如给一个区间内的数同时加上 \(c ...

  5. [bzoj5287] [HNOI2018]毒瘤

    题目描述 从前有一名毒瘤. 毒瘤最近发现了量产毒瘤题的奥秘.考虑如下类型的数据结构题:给出一个数组,要求支持若干种奇奇怪怪的修改操作(比如区间加一个数,或者区间开平方),并支持询问区间和.毒瘤考虑了n ...

  6. UOJ67 新年的毒瘤【Tarjan,割点】

    Online Judge:#uoj 67 Label:Tarjan,割点,细节 题目描述 辞旧迎新之际,喜羊羊正在打理羊村的绿化带,然后他发现了一棵长着毒瘤的树.这个长着毒瘤的树可以用\(n\)个结点 ...

  7. [HNOI2018]毒瘤

    Description 从前有一名毒瘤. 毒瘤最近发现了量产毒瘤题的奥秘.考虑如下类型的数据结构题:给出一个数组,要求支持若干种奇奇怪怪的修改操作(比如区间加一个数,或者区间开平方),并支持询问区间和 ...

  8. @loj - 2496@ 「AHOI / HNOI2018」毒瘤

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 从前有一名毒瘤. 毒瘤最近发现了量产毒瘤题的奥秘.考虑如下类型的 ...

  9. fzu 1911 Construct a Matrix(矩阵快速幂+规律)

    题目链接:fzu 1911 Construct a Matrix 题目大意:给出n和m,f[i]为斐波那契数列,s[i]为斐波那契数列前i项的和.r = s[n] % m.构造一个r * r的矩阵,只 ...

随机推荐

  1. 转:vue生命周期流程图

  2. JAVA基础学习之路(十)this关键字

    class Book { String name; int price; int num;//构造方法之间的互相调用解决了代码的重复问题,但是一定要留出口 public Book() { ,); } ...

  3. 如何实现iframe页面与父级页面js交互

    处理办法:1.同一域下,相同端口2.父级.子集页面上同时标记 document.domain = "xxx.com" 操作内部元素:1.jQuery使用 iframe.conten ...

  4. 蓝桥杯算法训练 区间k大数查询

    算法训练 区间k大数查询   问题描述 给定一个序列,每次询问序列中第l个数到第r个数中第K大的数是哪个. 输入格式 第一行包含一个数n,表示序列长度. 第二行包含n个正整数,表示给定的序列. 第三个 ...

  5. HADOOP docker(七):hive权限管理

    1. hive权限简介1.1 hive中的用户与组1.2 使用场景1.3 权限模型1.3 hive的超级用户2. 授权管理2.1 开启权限管理2.2 实现超级用户2.3 实现hiveserver2用户 ...

  6. Thunder团队第五周 - Scrum会议4

    Scrum会议4 小组名称:Thunder 项目名称:i阅app Scrum Master:李传康 工作照片: 邹双黛同学在拍照,所以不在照片内. 参会成员: 王航:http://www.cnblog ...

  7. unordered_map(hash_map)和map的比较

    测试代码: #include <iostream> using namespace std; #include <string> #include <windows.h& ...

  8. UVALive - 6886 Golf Bot 多项式乘法(FFT)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/129724 Golf Bot Time Limit: 15000MS 题意 给你n个数,m个查询,对于每个查询 ...

  9. TCP系列27—窗口管理&流控—1、概述

    在前面的内容中我们依次介绍了TCP的连接建立和终止过程和TCP的各种重传方式.接着我们在这部分首先关注交互式应用TCP连接相关内容如延迟ACK.Nagle算法.Cork算法等,接着我们引入流控机制(f ...

  10. 【Docker】- 基本命令

    1.docker ps -a    显示所有容器 2.doker ps -l 显示最近一次启动的容器 3.docker ps   显示正在运行的容器 4.docker start [容器ID]  启动 ...