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+,M=2e6+,inf=1e9+;
const LL INF=1e18+,mod=1e9+; struct isss
{
int v,nex;
}edge[N<<];
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+);
}
out[u]=tot;
}
struct AYT
{
LL tree[N];
void init()
{
memset(tree,,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=;
while(x)
{
ans+=tree[x];
x-=lowbit(x);
}
return ans;
}
}TX,TK; void init()
{
tot=;
memset(head,,sizeof(head));
memset(deep,,sizeof(deep));
TX.init();
TK.init();
edg=;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
init();
for(int i=;i<=n;i++)
{
int f;
scanf("%d",&f);
add(f,i);
add(i,f);
}
dfs(,-,);
int q;
scanf("%d",&q);
while(q--)
{
int t,v;
LL x,k;
scanf("%d%d",&t,&v);
if(t==)
{
scanf("%I64d%I64d",&x,&k);
x+=1LL*deep[v]*k;
x%=mod;
TX.update(in[v],x);
TX.update(out[v]+,-x);
TK.update(in[v],k);
TK.update(out[v]+,-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 ;
}

FZU oj 2277 Change 树状数组+dfs序的更多相关文章

  1. 【BZOJ】2434: [Noi2011]阿狸的打字机 AC自动机+树状数组+DFS序

    [题意]阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母. 经阿狸研究发现,这个打字机是这样工作的: l 输入小写 ...

  2. 【BZOJ-1103】大都市meg 树状数组 + DFS序

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2009  Solved: 1056[Submit][Sta ...

  3. [bzoj4034][HAOI2015]树上操作——树状数组+dfs序

    Brief Description 您需要设计一种数据结构支持以下操作: 把某个节点 x 的点权增加 a . 把某个节点 x 为根的子树中所有点的点权都增加 a . 询问某个节点 x 到根的路径中所有 ...

  4. BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]

    2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2545  Solved: 1419[Submit][Sta ...

  5. 【BZOJ-3881】Divljak AC自动机fail树 + 树链剖分+ 树状数组 + DFS序

    3881: [Coci2015]Divljak Time Limit: 20 Sec  Memory Limit: 768 MBSubmit: 508  Solved: 158[Submit][Sta ...

  6. POJ 3321 Apple Tree (树状数组+dfs序)

    题目链接:http://poj.org/problem?id=3321 给你n个点,n-1条边,1为根节点.给你m条操作,C操作是将x点变反(1变0,0变1),Q操作是询问x节点以及它子树的值之和.初 ...

  7. BZOJ 1103 [POI2007]大都市meg(树状数组+dfs序)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1103 [题目大意] 给出一棵树,每条边的经过代价为1,现在告诉你有些路不需要代价了, ...

  8. [luogu P3787][新创无际夏日公开赛] 冰精冻西瓜 [树状数组][dfs序]

    题目背景 盛夏,冰之妖精琪露诺发现了一大片西瓜地,终于可以吃到美味的冻西瓜啦. 题目描述 琪露诺是拥有操纵冷气程度的能力的妖精,一天她发现了一片西瓜地.这里有n个西瓜,由n-1条西瓜蔓连接,形成一个有 ...

  9. HDU5293(SummerTrainingDay13-B Tree DP + 树状数组 + dfs序)

    Tree chain problem Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

随机推荐

  1. HDU 2176 取(m堆)石子游戏 (尼姆博奕)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2176 m堆石子,两人轮流取.只能在1堆中取.取完者胜.先取者负输出No.先取者胜输出Yes,然后输出怎 ...

  2. flask框架----基于flask的扩展实现的简单的页面登录

    废话不多说,直接上代码 from flask import Flask,render_template,request,redirect,session app = Flask(__name__,te ...

  3. 前端框架VUE----对象的单体模式

    对象的单体模式 为了解决箭头函数this指向的问题 推出来一种写法 对象的单体模式 1 var person = { 2 name:'小马哥', 3 age:12, 4 fav(){ 5 consol ...

  4. dubbo原理

    1,观察DubboBeanDefinitionParser 的构造方法,给它打一个断点,发现其前一步在DubboNamespaceHandler 应用启动会连续调此方法 DubboBeanDefini ...

  5. 将表单序列化为JSON对象

    将表单序列化为JSON对象的工具方法: $(function() { //工具方法,可以将指定的表单中的输入项目序列化为JSON数据 $.fn.serializeJson = function() { ...

  6. ltp-fcntl36 偶尔出现fail unexpected data offset 20928 value 94

    每次出错的都是和posix相关先把结论说了: fn_ofd_w和fn_ofd_r的SAFE_FCNTL参数F_OFD_SETLKW fn_posix_w和fn_posix_r的SAFE_FCNTL参数 ...

  7. Linux下php添加memcache扩展

    很多时候我们都会遇到在已经安装的php中添加其它的扩展. 那我们应该怎么做呢? 这样做.(我们的nginx和php都是已经安装好了的,这里就不做赘述了) 首先,我们需要下载php的memcache扩展 ...

  8. selenium 模拟手机

    import time from selenium import webdriver mobileEmulation = {'deviceName': 'Galaxy S5'} options = w ...

  9. 最简单的uwsgi+nginx配置多个django站点

    1. nginx.conf http{ server { listen       80; server_name  www.web1.com ....... location / { uwsgi_p ...

  10. python基础--概念性问题

    面试其他篇 目录: 头条面试题:https://blog.csdn.net/m0_37947204/article/details/80103151 1.1 python常见基础题 1.遍历文件夹 # ...