WZJ的数据结构(负十)
难度级别:D; 运行时间限制:5000ms; 运行空间限制:51200KB; 代码长度限制:2000000B
试题描述

给你一个N个节点的有根树,从1到N编号,根节点为1并给出每个点的权值与父亲节点。请你设计一个数据结构,进行以下两种操作:

F x v : 将节点x的子树的每个节点权值+v

Q x : 询问节点x到其根的路径上的节点权值之和

输入
第一行一个正整数N。
接下来N-1行每行一个正整数,分别表示节点2-n的父亲节点编号。
接下来一行N个正整数,表示每个节点的初始权值。
再接下来一行一个正整数M,表示操作的总数。
操作分为以下两种类型。
(1)"Q x"表示询问节点x到其根的路径上的节点权值之和.
(2)"F x v"表示将节点x的子树的每个节点权值+v.
输出
对于每一个操作类型为Q的操作,输出一行一个整数,表示此次询问的答案。
输入示例
6
1
1
2
2
2
4 5 7 1 2 3
4
Q 2
F 1 3
Q 2
Q 5
输出示例
9
15
20
其他说明
1<=N,M<=100000
1<=pi,x<=N
1<=wi<=10^6
1<=v<=1000

今天跟健学了DFS序。。。原来就这么回事,注意好符号、出入。

还有,以后的AddEdge注意好顺序!!!

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#define PAU putchar(' ')
#define ENT putchar('\n')
using namespace std;
typedef long long LL;
const int maxn=+,maxn3=+,inf=-1u>>;
LL addv[maxn3],sumv[maxn3],siz[maxn3],_sum;int n,Q,sig[maxn],A[maxn],si[maxn],so[maxn],cz=,ql,qr,cv;
struct Tedge{int x,y,next;}adj[maxn];int ms=,fch[maxn];
void AddEdge(int u,int v){adj[++ms]=(Tedge){u,v,fch[u]};fch[u]=ms;return;}
void dfs(int u){
si[u]=++cz;sig[cz]=;
for(int i=fch[u];i;i=adj[i].next) dfs(adj[i].y);
so[u]=++cz;sig[cz]=-;
return;
}
void maintain(int o,int L,int R){
int lc=o<<,rc=lc|;
if(L<R) sumv[o]=sumv[lc]+sumv[rc]; else sumv[o]=;
sumv[o]+=addv[o]*siz[o];return;
}
void build(int o,int L,int R){
if(L==R) addv[o]=A[L],siz[o]=sig[L];
else{
int M=L+R>>,lc=o<<,rc=lc|;
build(lc,L,M);build(rc,M+,R);
siz[o]=siz[lc]+siz[rc];
} maintain(o,L,R);return;
}
void update(int o,int L,int R){
if(ql<=L&&R<=qr) addv[o]+=cv;
else{
int M=L+R>>,lc=o<<,rc=lc|;
if(ql<=M) update(lc,L,M);
if(qr>M) update(rc,M+,R);
} maintain(o,L,R);return;
}
void query(int o,int L,int R,int add){
if(ql<=L&&R<=qr) _sum+=add*siz[o]+sumv[o];
else{
int M=L+R>>,lc=o<<,rc=lc|;
if(ql<=M) query(lc,L,M,add+addv[o]);
if(qr>M) query(rc,M+,R,add+addv[o]);
} return;
}
inline int read(){
int x=,sig=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')sig=-;ch=getchar();}
while(isdigit(ch))x=*x+ch-'',ch=getchar();
return x*=sig;
}
inline void write(int x){
if(x==){putchar('');return;}if(x<)putchar('-'),x=-x;
int len=,buf[];while(x)buf[len++]=x%,x/=;
for(int i=len-;i>=;i--)putchar(buf[i]+'');return;
}
inline void write(LL x){
if(x==){putchar('');return;}if(x<)putchar('-'),x=-x;
int len=;LL buf[];while(x)buf[len++]=x%,x/=;
for(int i=len-;i>=;i--)putchar(buf[i]+'');return;
}
inline char readc(){
char tp;for(tp=getchar();!isalpha(tp);tp=getchar());return tp;
}
void init(){
n=read();
for(int i=;i<=n;i++) AddEdge(read(),i); dfs();
for(int i=;i<=n;i++) A[si[i]]=A[so[i]]=read(); build(,,n<<);
return;
}
void work(){
Q=read();
while(Q--){
if(readc()=='Q'){
_sum=;
ql=;qr=si[read()];query(,,n<<,);
write(_sum);ENT;
}
else{
ql=read();
qr=so[ql];ql=si[ql];cv=read();
update(,,n<<);
}
}
return;
}
void print(){
return;
}
int main(){init();work();print();return ;}

搜索

复制

COJ 0990 WZJ的数据结构(负十)的更多相关文章

  1. COJ 1010 WZJ的数据结构(十) 线段树区间操作

    传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=1001 WZJ的数据结构(十) 难度级别:D: 运行时间限制:3000ms: ...

  2. COJ 0970 WZJ的数据结构(负三十)树分治

    WZJ的数据结构(负三十) 难度级别:D: 运行时间限制:1000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 给你一棵N个点的无根树,点和边上均有权值.请你设计 ...

  3. COJ 0981 WZJ的数据结构(负十九)树综合

    WZJ的数据结构(负十九) 难度级别:E: 运行时间限制:3500ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 WZJ的数据结构中有很多都是关于树的.这让很多练习 ...

  4. COJ 0967 WZJ的数据结构(负三十三)

    WZJ的数据结构(负三十三) 难度级别:E: 运行时间限制:7000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 请你设计一个数据结构,完成以下功能: 给定一个大 ...

  5. COJ 0995 WZJ的数据结构(负五)区间操作

    WZJ的数据结构(负五) 难度级别:C: 运行时间限制:1000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 请你设计一个数据结构,完成以下功能: 给定一个大小为 ...

  6. COJ 0999 WZJ的数据结构(负一)

    WZJ的数据结构(负一) 难度级别:D: 运行时间限制:1000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 输入N个模板串Pi和文本串T,输出每个模板串Pi在T ...

  7. COJ 0979 WZJ的数据结构(负二十一)

    WZJ的数据结构(负二十一) 难度级别:C: 运行时间限制:5000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 请你实现一个数据结构,完成这样的功能: 给你一个 ...

  8. COJ 1008 WZJ的数据结构(八) 树上操作

    传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=986 WZJ的数据结构(八) 难度级别:E: 运行时间限制:3000ms: ...

  9. COJ 1007 WZJ的数据结构(七) 树上操作

    传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=983 WZJ的数据结构(七) 难度级别:C: 运行时间限制:1000ms: ...

随机推荐

  1. php 自定义求数组差集,效率比自带的array_diff函数还要快(转)

    <?phpfunction array_different($array_1, $array_2) { $array_2 = array_flip($array_2); //将数组键值调换 fo ...

  2. C#编写QQ找茬外挂

    QQ找茬外挂,用C#代码编写. 使用方法 这个工具的主要运行流程很简单:游戏截图->比较图片->标记图片不同点.实现代码 截图的处理类ScreenCapture: /// /// 提供全屏 ...

  3. entity 实体模型timeout设置

    public Entities(): base("name=Entities") { var adapter = (IObjectContextAdapter)this; var ...

  4. OD: Kernel Vulnerabilities

    内核漏洞概述 内核漏洞的分类 运行在 Ring0 上的操作系统内核.设备驱动.第三方驱动能共享同一个虚拟地址空间,可以完全访问系统空间的所有内存,而不像用户态进程那样拥有独立私有的内存空间.由于内核程 ...

  5. 操作iis

    以后研究 try { string method = "Recycle"; string AppPoolName = "z.chinabett.com"; Di ...

  6. Ajax简单应用-购物车

    1. 2. 3. 4. 5. 6.

  7. Css3中的响应式布局的应用

    Media Queries直译过来就是“媒体查询”,在我们平时的Web页面中head部分常看到这样的一段代码: <link href="css/reset.css" rel= ...

  8. Python文件之----XML

    #coding=utf-8 from xml.dom import minidom from xml.dom.minidom import Document import xml def writeX ...

  9. 爬虫爬oj用户头像

    import requests import Queue import urllib import urllib2 import re import requests alreadyImg = set ...

  10. [500lines]500行代码写web server

    项目地址:https://github.com/aosabook/500lines/tree/master/web-server.作者是来自Mozilla的Greg Wilson.项目是用py2写成. ...