题目链接:

F - Change

FZU - 2277

题目大意:

题意: 给定一棵根为1, n个结点的树. 有q个操作,有两种不同的操作

(1) 1 v k x : a[v] += x, a[v '] += x – k(v '为v的儿子), a[v ' '] += x – 2 * k(v ' '是v '的儿子) ... ;

(2) 2 v : 输出a[v] % (1e9 + 7);

具体思路:dfs序+线段树

a[v'']=a[v'']+x-k*(depth[v'']-depth[v])=a[v'']+x-k*depth[v"]+k*depth[v]=(a[v"]+k*depth[v]) - k*depth[v""].

第一个括号里面用一个线段树维护,第二个括号里面用另一个线段树维护就可以了。

具体求值的时候:对于一次增加操作,节点x增加的值其实就是x-(deep[x]-deep[v])*k。(deep[v]为该次修改的根节点)。

AC代码:

 #include<iostream>
#include<stdio.h>
#include<vector>
#include<cstring>
using namespace std;
# define ll long long
# define lson l,mid,rt<<
# define rson mid+,r,rt<<|
const int maxn = 3e5+;
const int mod =1e9+;
ll tree1[maxn<<],tree2[maxn<<];
int dfsord,n;
vector<int>edge[maxn];
int st[maxn],ed[maxn],depth[maxn];
int Scan()
{
int res = , ch, flag = ;
if((ch = getchar()) == '-')
flag = ;
else if(ch >= '' && ch <= '')
res = ch - '';
while((ch = getchar()) >= '' && ch <= '' )
res = res * + ch - '';
return flag ? -res : res;
} ll Scan_l()
{
ll res = ;
int ch, flag = ;
if((ch = getchar()) == '-')
flag = ;
else if(ch >= '' && ch <= '')
res = ch - '';
while((ch = getchar()) >= '' && ch <= '' )
res = res * + ch - '';
return flag ? -res : res;
}
void init()
{
for(int i=; i<n; i++)
{
edge[i].clear();
}
dfsord=;
memset(tree1,,sizeof(tree1));
memset(tree2,,sizeof(tree2));
}
void dfs(int cur,int fa,int dep)
{
st[cur]=ed[cur]=++dfsord;
depth[dfsord]=dep+;
for(int i=; i<edge[cur].size(); i++)
{
int to=edge[cur][i];
if(to==fa)
continue;
dfs(to,cur,dep+);
}
ed[cur]=dfsord;
}
void down(int rt)
{
tree1[rt<<]=(tree1[rt<<]+tree1[rt]+mod)%mod;
tree1[rt<<|]=(tree1[rt<<|]+tree1[rt]+mod)%mod;
tree2[rt<<]=(tree2[rt<<]+tree2[rt]+mod)%mod;
tree2[rt<<|]=(tree2[rt<<|]+tree2[rt]+mod)%mod;
tree1[rt]=,tree2[rt]=;
}
void update(int l,int r,int rt,int L,int R,ll val1,ll val2)
{
if(L<=l&&R>=r)
{
tree1[rt]=(tree1[rt]+val1+mod)%mod;
tree2[rt]=(tree2[rt]+val2+mod)%mod;
return ;
}
down(rt);
int mid=(l+r)>>;
if(L<=mid)
update(lson,L,R,val1,val2);
if(R>mid)
update(rson,L,R,val1,val2);
}
ll ask(int l,int r,int rt,int pos)
{
if(l==r)
{
return (tree1[rt]+tree2[rt]*depth[pos]%mod+mod)%mod;
}
int mid=(l+r)>>;
down(rt);
if(pos<=mid)
return ask(lson,pos);
else
return ask(rson,pos);
}
int main()
{
int T,tmp;
T=Scan();
while(T--)
{
n=Scan();
init();
for(int i=; i<=n; i++)
{
tmp=Scan();
edge[tmp].push_back(i);
}
int m;
dfs(,,);
m=Scan();
while(m--)
{
int op,v;
ll x,k;
op=Scan();
if(op==)
{
v=Scan();
x=Scan_l();
k=Scan_l();
update(,n,,st[v],ed[v],x+k*depth[st[v]],-k);
}
else
{
v=Scan();
ll ans=ask(,n,,st[v]);
ans=(ans%mod+mod)%mod;
printf("%lld\n",ans);
}
}
}
return ;
}

F - Change FZU - 2277 (DFS序+线段树)的更多相关文章

  1. 【BZOJ-3252】攻略 DFS序 + 线段树 + 贪心

    3252: 攻略 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 339  Solved: 130[Submit][Status][Discuss] D ...

  2. BZOJ4551[Tjoi2016&Heoi2016]树——dfs序+线段树/树链剖分+线段树

    题目描述 在2016年,佳媛姐姐刚刚学习了树,非常开心.现在他想解决这样一个问题:给定一颗有根树(根为1),有以下 两种操作:1. 标记操作:对某个结点打上标记(在最开始,只有结点1有标记,其他结点均 ...

  3. HDU.5692 Snacks ( DFS序 线段树维护最大值 )

    HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...

  4. 【XSY2667】摧毁图状树 贪心 堆 DFS序 线段树

    题目大意 给你一棵有根树,有\(n\)个点.还有一个参数\(k\).你每次要删除一条长度为\(k\)(\(k\)个点)的祖先-后代链,问你最少几次删完.现在有\(q\)个询问,每次给你一个\(k\), ...

  5. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

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

    Danil decided to earn some money, so he had found a part-time job. The interview have went well, so ...

  7. 洛谷P3178 [HAOI2015]树上操作(dfs序+线段树)

    P3178 [HAOI2015]树上操作 题目链接:https://www.luogu.org/problemnew/show/P3178 题目描述 有一棵点数为 N 的树,以点 1 为根,且树点有边 ...

  8. 【cf343】D. Water Tree(dfs序+线段树)

    传送门 题意: 给出一个以\(1\)为根的有根树,起始每个结点都为\(0\),现在有三种操作: 1.将\(v\)及\(v\)的子树都置为\(1\): 2.将\(v\)及其所有的祖先都置为\(0\): ...

  9. BZOJ 3252题解(贪心+dfs序+线段树)

    题面 传送门 分析 此题做法很多,树形DP,DFS序+线段树,树链剖分都可以做 这里给出DFS序+线段树的代码 我们用线段树维护到根节点路径上节点权值之和的最大值,以及取到最大值的节点编号x 每次从根 ...

随机推荐

  1. 利用SHAPEIT将vcf文件进行基因型(genotype)定相(phasing):查看两个突变是否来源于同一条链(染色体或父本或母本),two mutations carried by the same read

    首先,下载SHAPEIT. 按照里面的步骤安装完后,将vcf文件进行基因型定相,分四步走. 第一步,将vcf文件转化为plink二进制文件(.bed, .bim, .fam). 这一步需要用到GATK ...

  2. 随机数Random

    掷骰子10次,统计1.2出现的次数 public static void Main(string[] args) { ,a2=; Random random=new Random();//创建随机数对 ...

  3. SSH整合redis和MongoDB错误笔记

    由于毕设中做的是图片搜索网站,选择前端框用SSH,因为之间接触过SSH框架,略有了解,但没有深究,现在在整合redis和mongodb的过程中遇到很多错误,也是十分痛苦,只能通过百度和一步步尝试着解决 ...

  4. display详解

    css中的display属性 display属性是我们在前端开发中常常使用的一个属性,其中,最常见的有: none block inline inline-block inherit 下面,我将按照顺 ...

  5. Pyhton之subprocess模块和configparser模块

    一.subprocess模式 # import os # while True: # cmd=input('>>').strip() # if not cmd:continue # if ...

  6. Vue.js 循环语句

    循环使用 v-for 指令. v-for 指令需要以 site in sites 形式的特殊语法, sites 是源数据数组并且 site 是数组元素迭代的别名. v-for 指令: v-for 可以 ...

  7. Nginx+Keeplived双机热备(主从模式)

    Nginx+Keeplived双机热备(主从模式) 参考资料: http://www.cnblogs.com/kevingrace/p/6138185.html 双机高可用一般是通过虚拟IP(漂移IP ...

  8. 信用评分卡 (part 4 of 7)

    python信用评分卡(附代码,博主录制) https://study.163.com/course/introduction.htm?courseId=1005214003&utm_camp ...

  9. awk 处理文本:行转列,列转行

    [root@centos ~]# cat f 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 51 2 3 4 5 awk '{for(i=1;i<=NF;i++)a ...

  10. C语言:使用结构体和指针函数实现面向对象思想(OO编程)

    原文:https://www.linuxidc.com/Linux/2016-12/138789.htm 有关:<C语言:过年回家 发现只有我没有对象> 一.基础研究 观察如下两个程序a. ...