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
这个题目的出奇之处就是在更新的时候,每个点的更新多少是不一样的。

我们看题干这里,每个节点更新的是随着深度的增加逐渐递减的,我们必须找出这之间的规律才能做。假如树的祖先深度是deep[v],那么它的一个子节点u的深度是deep[u],子节点更新的值是
x-(deep[u]-deep[v])*k。对于v所有的子树来说,都会更新的值是x+deep[v]*k。剩下的就和自己的深度有关了。那么我们线段树维护两个lazy,一个记录都增加的值,另一个就记录k。
代码如下:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<queue>
#include<cstring>
#define ll unsigned long long
#define mod 1000000007
using namespace std;const int maxx=3e5+100;
struct node{int l;int r;ll lazy1;ll lazy2;ll sum;
}p[maxx<<2];
struct edge{int to;int next;
}e[maxx];
int deep[maxx],in[maxx],out[maxx],pre[maxx],head[maxx<<1];
int tot,sign,n,m;
/*----------------事前准备---------------*/
inline void init()
{memset(head,-1,sizeof(head));tot=sign=0;
}
inline void read(int &x)//快速读入
{int f=1;x=0;char s=getchar();while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}x*=f;
}
void read1(ll &x)
{int f=1;x=0;char s=getchar();while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}x*=f;
}
inline void add(int u,int v)
{e[tot].to=v,e[tot].next=head[u],head[u]=tot++;
}
/*--------------dfs---------------*/
inline void dfs(int u,int f)
{deep[u]=deep[f]+1;in[u]=++sign;pre[sign]=u;for(int i=head[u];i!=-1;i=e[i].next){int to=e[i].to;if(to==f) continue;dfs(to,u);}out[u]=sign;
}
/*-----------------线段树-----------------*/
inline void pushdown(int cur)
{if(p[cur].lazy1){p[cur<<1].lazy1+=p[cur].lazy1;p[cur<<1].lazy1%=mod;p[cur<<1|1].lazy1+=p[cur].lazy1;p[cur<<1|1].lazy1%=mod;p[cur<<1].sum=(p[cur<<1].sum+p[cur].lazy1*(p[cur<<1].r-p[cur<<1].l+1)%mod+mod)%mod;p[cur<<1|1].sum=(p[cur<<1|1].sum+p[cur].lazy1*(p[cur<<1|1].r-p[cur<<1|1].l+1)%mod+mod)%mod;p[cur].lazy1=0;}if(p[cur].lazy2){p[cur<<1].lazy2+=p[cur].lazy2;p[cur<<1].lazy2%=mod;p[cur<<1|1].lazy2+=p[cur].lazy2;p[cur<<1|1].lazy2%=mod;p[cur].lazy2=0;}
}
inline void build(int l,int r,int cur)
{p[cur].l=l;p[cur].r=r;p[cur].lazy1=p[cur].lazy2=p[cur].sum=0;if(l==r) return ;int mid=l+r>>1;build(l,mid,cur<<1);build(mid+1,r,cur<<1|1);
}
inline void update(int l,int r,ll v,ll k,int cur)
{int L=p[cur].l;int R=p[cur].r;if(l<=L&&R<=r){p[cur].sum=(p[cur].sum+v*(R-L+1)%mod+mod)%mod;p[cur].lazy1=(p[cur].lazy1+v+mod)%mod;p[cur].lazy2=(p[cur].lazy2+k+mod)%mod;return ;}pushdown(cur);int mid=L+R>>1;if(r<=mid) update(l,r,v,k,cur<<1);else if(l>mid) update(l,r,v,k,cur<<1|1);else{update(l,mid,v,k,cur<<1);update(mid+1,r,v,k,cur<<1|1);}
}
inline ll query(int pos,int cur)
{int L=p[cur].l;int R=p[cur].r;if(L==R) return (p[cur].sum+deep[pre[L]]*p[cur].lazy2%mod+mod)%mod;//到达所要求的节点的时候,不要忘了加上和这个点深度有关的那一部分值。pushdown(cur);int mid=L+R>>1;if(pos<=mid) return query(pos,cur<<1);else return query(pos,cur<<1|1);
}
int main()
{int t,x,op;ll v,k;read(t);while(t--){init();read(n);for(int i=2;i<=n;i++){read(x);add(i,x);add(x,i);}deep[0]=-1;dfs(1,0);build(1,n,1);read(m);while(m--){read(op);if(op==1){read(x);read1(v);read1(k);update(in[x],out[x],deep[x]*k+v,-k,1);}else if(op==2) {read(x);printf("%I64d\n",query(in[x],1));}}}return 0;
}

努力加油a啊,(o)/~

Change FZU - 2277(线段树+dfs序)相关推荐

  1. bzoj3252攻略(线段树+dfs序)或者(树链剖分+dfs)

    3252: 攻略 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 1341 Solved: 642 [Submit][Status][Discuss] ...

  2. BZOJ_3252_攻略_线段树+dfs序

    BZOJ_3252_攻略_线段树+dfs序 Description 题目简述:树版[k取方格数] 众所周知,桂木桂马是攻略之神,开启攻略之神模式后,他可以同时攻略k部游戏.今天他得到了一款新游戏< ...

  3. bzoj3252攻略(线段树+dfs序)

    3252: 攻略 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 562  Solved: 238 [Submit][Status][Discuss] ...

  4. HDU5692(线段树+dfs序)

    Snacks Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  5. CodeForces - 620E New Year Tree(线段树+dfs序+状态压缩)

    题目链接:点击查看 题目大意:给出一棵无向树,每个节点都有一种颜色,接下来时m次操作: 1 x y:将x及其子树染成y的颜色 2 x:查询x及其子树上共有多少种不同的颜色 题目分析:看完这个题的第一反 ...

  6. HDU - 3974 Assign the task (线段树 + dfs序)

    HDU - 3974 题意:有个公司有一些关系,每个人(除了boss)都有且仅有一个上司,这就是一棵树的关系,然后会有一些操作,C i,询问第i个人现在的任务,T x y,把y任务给x, 给x相当于给 ...

  7. 苹果树(线段树+Dfs序)

    1228 苹果树  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description 在卡卡的房子外面,有一棵苹果树.每年的春天,树上总会结 ...

  8. Codeforces 877 E Danil and a Part-time Job(线段树+dfs序)

    题目地址 题意:给你一棵树,1为根节点,每个节点都有应该状态0或者1,你有两种操作,pow的操作是把该节点以及他的所有子树的每个节点进行自身异或的操作,get就是查询该节点以及他的所有子树的每个节点有 ...

  9. BZOJ 3779 LCT 线段树 DFS序 坑

    hhhh抄了半天lty代码最后T了  对拍也没事.. 药丸 mine #pragma GCC optimize("O3") //By SiriusRen #include < ...

最新文章

  1. css:中文词不断开,整体换行
  2. Java当中的IO一
  3. Go语言与数据库开发:01-11
  4. Max Points on a Line@LeetCode
  5. Kubernetes 小白学习笔记(10)--搭建一个kubernetes集群-组建节点网络
  6. 物联网空开价格_北斗星C2物联网蒸箱集成灶618价格提前开抢,转发送豪礼
  7. 《疯狂的站长》读后感2
  8. 淘晶驰串口屏下载工程慢怎么办
  9. python csv文件比较
  10. 我家猫老喜欢和我躲猫猫,我用Python赶忙写了个猫脸检测器。在哪里都逃不出我的手心。
  11. C语言的奇技淫巧之三
  12. 为了保护您的视力,请对电脑作如下设置
  13. HANA掀起数据处理狂潮 农夫山泉有点甜?
  14. 聊聊Java那些事儿
  15. oracle sparc 服务器系统,oracle sparc服务器基础及管理.pdf
  16. python抢红包程序算法,Python 抢红包算法模拟
  17. P1024 [NOIP2001 提高组] 一元三次方程求解 /1238:一元三次方程求解
  18. 4台机器完全分布式安装hadoop,bigbrother的补充
  19. mysql 只显示第一条记录_MySQL:此种查询结果,怎么仅保留第一条记录?
  20. 电脑外置,笔记本电脑外接显卡,教您笔记本如何外接独显

热门文章

  1. Java代码的维护与更新,Java常用的规则引擎,让你变动的代码更加容易维护
  2. 程序员基本功02对象与内存控制
  3. 创业者具备的五大技能_赛事动态 | 新道科技连续六年支持全国职业院校技能大赛沙盘模拟企业经营赛项...
  4. python电脑推荐_6款Python必备的可视化工具推荐
  5. linux修改ip广播地址,Linux设置查看静态IP之ifconfig命令
  6. linux操作命令comm,Linux
  7. 使用UEFI Shell引导U盘启动
  8. STL库中string类内存布局的探究
  9. 托福试卷真题_托福反复考,反复不过百,你还不知道是谁的问题吗?
  10. python之绝对导入和相对导入