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. [转载]WebService使用的一些总结

    什么是WebService: 这个不用我在这里废话,网上的资料一搜一大把,如果你没有接触过这方面的知识,你可以先去网上查一下.这里我只想说一下我印象比较深刻的几点: WebService是基于soap ...

  2. Top 5 Reasons to Get BMW ICOM A2 with Latest Software

    Top 5 Reasons to Get BMW ICOM A2 with Latest Software 1.BMW ICOM A2 Hardware Version: V2018.03 2.Sup ...

  3. web前端学习路线:HTML5教程之前端模块化开发

    1. 命名冲突 首先从一个简单的习惯开始. 由于以前一直做 JavaEE 开发的缘故,在 JavaScript 开发中,我已经习惯将项目中的一些通用功能抽象出来,形成一个个的独立函数,以便于实现代码复 ...

  4. 大数据学习路线分享-Hbase shell的基本操作完整流程

    HBase的命令行工具,最简单的接口,适合HBase管理使用,可以使用shell命令来查询HBase中数据的详细情况.安装完HBase之后,启动hadoop集群(利用hdfs存储),启动zookeep ...

  5. mysql命令(三)

    创建一个名字为Student库: create database Student; 用以下地命令来查看创建的数据库是否成功: show databases; 进入数据库: use Student; 用 ...

  6. js 简易时钟

    html部分 <div id="clock"> </div> css部分 #clock{ width:600px ; text-align: center; ...

  7. mysql日志种类、二进制日志模式、mysqlbinlog解析二进制日志

    mysql日志的种类 二进制日志(binary log):记录数据更新的操作,mysqlbinlog 可查看二进制日志文件 错误日志(error log):记录mysql服务进程mysqld的启动.关 ...

  8. 一套权威的 MQTT Client 库

    主流的语言都支持,可链接到 github ,亲测golang client 简单好用 http://www.eclipse.org/paho/downloads.php

  9. Elasticsearch.Net使用(一)【入门篇】

    http://blog.csdn.net/wulex/article/details/52138564 加数据 //在调用下面的index方法的时候,如果没有指定使用哪个index,ElasticSe ...

  10. Linux基础笔记——RAID

    关于RAID RAID0:理论上来说一个有n块磁盘组成的raid0,它的读写是单个磁盘性能的n倍,具有低成本,高性能,低安全性,可用于可靠性不高的应用,如:视频.音频.临时数据存储等 RAID1:也称 ...