On Changing Tree

CodeForces - 396C

You are given a rooted tree consisting of n vertices numbered from 1 to n. The root of the tree is a vertex number 1.

Initially all vertices contain number 0. Then come q queries, each query has one of the two types:

  • The format of the query: 1 v x k. In response to the query, you need to add to the number at vertex v number x; to the numbers at the descendants of vertex vat distance 1, add x - k; and so on, to the numbers written in the descendants of vertex v at distance i, you need to add x - (i·k). The distance between two vertices is the number of edges in the shortest path between these vertices.
  • The format of the query: 2 v. In reply to the query you should print the number written in vertex v modulo 1000000007 (109 + 7).

Process the queries given in the input.

Input

The first line contains integer n (1 ≤ n ≤ 3·105) — the number of vertices in the tree. The second line contains n - 1 integers p2, p3, ... pn (1 ≤ pi < i), where pi is the number of the vertex that is the parent of vertex i in the tree.

The third line contains integer q (1 ≤ q ≤ 3·105) — the number of queries. Next qlines contain the queries, one per line. The first number in the line is type. It represents the type of the query. If type = 1, then next follow space-separated integers v, x, k (1 ≤ v ≤ n; 0 ≤ x < 109 + 7; 0 ≤ k < 109 + 7). If type = 2, then next follows integer v (1 ≤ v ≤ n) — the vertex where you need to find the value of the number.

Output

For each query of the second type print on a single line the number written in the vertex from the query. Print the number modulo 1000000007 (109 + 7).

Examples

Input
3
1 1
3
1 1 2 1
2 1
2 2
Output
2
1

Note

You can read about a rooted tree here: http://en.wikipedia.org/wiki/Tree_(graph_theory).

给出一棵以1为根的树,形式是从节点2开始给出每个节点的父亲节点;
然后是m次操作,操作分为两种,1 v, x, k,表示在以v为根的子树上添加,
添加的法则是看这个节点与v节点的距离为i的话,加上x-i*k;2 v查询节点v的值。

sol:子树操作显然可以搞出dfs序然后修改一段区间,然后修改是一个经典操作,看做是子树每个节点加上x+deg[v]*k,查询y的时候减去dep[y]*k即可

/*
给出一棵以1为根的树,形式是从节点2开始给出每个节点的父亲节点;
然后是m次操作,操作分为两种,1 v, x, k,表示在以v为根的子树上添加,
添加的法则是看这个节点与v节点的距离为i的话,加上x-i*k;2 v查询节点v的值。
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=,M=;
const ll Mod=;
int n,Q;
inline void Ad(ll &x,ll y)
{
x+=y; x-=(x>=Mod)?Mod:; x+=(x<)?Mod:;
}
namespace Tree
{
int tot=,Next[M],to[M],head[N];
inline void add(int x,int y)
{
Next[++tot]=head[x];
to[tot]=y;
head[x]=tot;
}
int In[N],Out[N],cnt=,Depth[N];
inline void dfs(int x)
{
int i;
In[x]=++cnt;
for(i=head[x];i;i=Next[i]) Depth[to[i]]=Depth[x]+,dfs(to[i]);
Out[x]=++cnt;
}
struct segment
{
ll S[N<<];
#define lowbit(x) ((x)&(-x))
inline void Ins(int x,int Val)
{
while(x<=cnt)
{
Ad(S[x],Val); x+=lowbit(x);
}
}
inline int Que(int x)
{
ll ans=;
while(x>)
{
Ad(ans,S[x]); x-=lowbit(x);
}
return ans;
}
}SGT[];
inline void Solve()
{
int i;
Depth[]=; dfs();
R(Q);
while(Q--)
{
ll opt,rt,Val,Del;
R(opt); R(rt);
if(opt==)
{
Val=read()%Mod; R(Del);
SGT[].Ins(In[rt],(Val+Del*Depth[rt]%Mod)%Mod);
SGT[].Ins(Out[rt]+,(-)*(Val+Del*Depth[rt]%Mod)%Mod);
SGT[].Ins(In[rt],Del);
SGT[].Ins(Out[rt]+,(-)*Del);
}
else
{
ll tmp,oo;
tmp=SGT[].Que(In[rt]);
oo=SGT[].Que(In[rt]);
Ad(tmp,(-)*oo*Depth[rt]%Mod);
Wl(tmp);
}
}
}
}
#define T Tree
int main()
{
int i;
R(n);
for(i=;i<=n;i++)
{
int x=read(); T::add(x,i);
}
T::Solve();
return ;
}
/*
Input
3
1 1
3
1 1 2 1
2 1
2 2
Output
2
1 Input
10
1 2 3 4 4 3 3 6 7
10
1 6 13 98
1 7 17 66
1 5 32 39
1 1 9 5
1 7 27 11
1 1 24 79
1 5 87 86
2 2
1 5 9 38
2 5
Output
999999956
999999832
*/

codeforces396C的更多相关文章

随机推荐

  1. 关于文本设置overflow:hidden后引起的垂直对齐问题

    目前有这样的需求,一行标题中,前面为图标,后面是文字,文字要实现一行省略的效果 首先把文字设为:display: inline-block; 然后设置省略: overflow: hidden; wor ...

  2. nginx 请求限制和访问控制

    请求限制 限制主要有两种类型: 连接频率限制: limit_conn_module 请求频率限制: limit_req_module HTTP协议的连接与请求 HTTP协议是基于TCP的,如果要完成一 ...

  3. 【OF框架】新建库表及对应实体,并实现简单的增删改查操作,封装操作标准WebApi

    准备 搭建好项目框架及数据库,了解框架规范. 1.数据库表和实体一一对应,表名实体名名字相同,用小写,下划线连接.字段名用驼峰命名法,首字母大写. 2.实体放在Entities目录下,继承Entity ...

  4. 运维开发笔记整理-template的使用

    运维开发笔记整理-Django的template的使用 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在上一篇博客中我们学习了HttpResponse 和JsonResponse方 ...

  5. idea中Git配置,Git的非命令操作

    1.更换Git账户 在idea中File-->Settings-->Appearance-->System Settings-->Passwords,选择不保存密码(Do no ...

  6. Android 测试-Robolectric,mockito,esspresso

    代码参考:https://github.com/googlesamples/android-testing 解释参考: https://www.jianshu.com/p/5732b4afd12f 官 ...

  7. React 零碎笔记

    1.对数组的操作(添加.更新.删除) const posts = [...this.state.posts]; posts.push(post); this.setState({posts}); =& ...

  8. 负载均衡集群(LBC)

    一.LVS简介及工作模式1. LVS简介Linux Virtual Server,该软件的功能是实现LB(load balance) 2.LVS的三种工作模式 1)NAT模式(NAT) LVS 服务器 ...

  9. C#当前运行所在目录集合

    //获取当前进程的完整路径,包含文件名(进程名).string str = this.GetType().Assembly.Location;result: X:\xxx\xxx\xxx.exe (. ...

  10. Selenium常用API的使用java语言之6-WebDriver常用方法

    前面我们已经学习了定位元素, 定位只是第一步, 定位之后需要对这个元素进行操作, 或单击(按钮) 或 输入(输入框) , 下面就来认识这些最常用的方法. 1.WebDriver 常用方法 下面先来认识 ...