COJ 0990 WZJ的数据结构(负十)
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的数据结构(负十)的更多相关文章
- COJ 1010 WZJ的数据结构(十) 线段树区间操作
传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=1001 WZJ的数据结构(十) 难度级别:D: 运行时间限制:3000ms: ...
- COJ 0970 WZJ的数据结构(负三十)树分治
WZJ的数据结构(负三十) 难度级别:D: 运行时间限制:1000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 给你一棵N个点的无根树,点和边上均有权值.请你设计 ...
- COJ 0981 WZJ的数据结构(负十九)树综合
WZJ的数据结构(负十九) 难度级别:E: 运行时间限制:3500ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 WZJ的数据结构中有很多都是关于树的.这让很多练习 ...
- COJ 0967 WZJ的数据结构(负三十三)
WZJ的数据结构(负三十三) 难度级别:E: 运行时间限制:7000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 请你设计一个数据结构,完成以下功能: 给定一个大 ...
- COJ 0995 WZJ的数据结构(负五)区间操作
WZJ的数据结构(负五) 难度级别:C: 运行时间限制:1000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 请你设计一个数据结构,完成以下功能: 给定一个大小为 ...
- COJ 0999 WZJ的数据结构(负一)
WZJ的数据结构(负一) 难度级别:D: 运行时间限制:1000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 输入N个模板串Pi和文本串T,输出每个模板串Pi在T ...
- COJ 0979 WZJ的数据结构(负二十一)
WZJ的数据结构(负二十一) 难度级别:C: 运行时间限制:5000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 请你实现一个数据结构,完成这样的功能: 给你一个 ...
- COJ 1008 WZJ的数据结构(八) 树上操作
传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=986 WZJ的数据结构(八) 难度级别:E: 运行时间限制:3000ms: ...
- COJ 1007 WZJ的数据结构(七) 树上操作
传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=983 WZJ的数据结构(七) 难度级别:C: 运行时间限制:1000ms: ...
随机推荐
- Android数据存储(1)少量数据保存之SharedPreferences接口实例
SharedPreferences数据保存主要是通过键值的方式存储在xml文件中 xml文件在data/此程序的包名/XX.xml 格式 <?xml version='1.0' encoding ...
- [Falcor] Indroduce to Model
How to work with JSON data indirectly through a Falcor Model. The Falcor Model allows you to work wi ...
- Linux Top 命令解析 比较详细--转
TOP是一个动态显示过程,即可以通过用户按键来不断刷新当前状态.如果在前台执行该命令,它将独占前台,直到用户终止该程序为止.比较准确的说,top命令提供了实时的对系统处理器的状态监视.它将显示系统中C ...
- 实例化讲解 RunLoop
实例化讲解RunLoop 之前看过很多有关RunLoop的文章,其中要么是主要介绍RunLoop的基本概念,要么是主要讲解RunLoop的底层原理,很少用真正的实例来讲解RunLoop的,这其中有大部 ...
- TCP/IP协议原理与应用笔记09:数据通信---封装
2016-08-091. 数据通信----封装: 2. 协议数据单元: PDU:对等层数据通信的单元. 比如Source端的应用层 和 Destination端的应用层是对等层(L7),这个时候L7 ...
- 9.8 noip模拟试题
LazyChild黑OJ(blackoj.pas/c/cpp) LazyChild开了一家“善良OJ”.但大多数人都不知道,这其实是家黑OJ.亲爱的同学,请不要惊讶,古时候有黑店,现代为什么不能有黑O ...
- mysql复习增删改查
select * from torder where status='退款申请' UPDATE torder SET `status`='退款申请' WHERE status='等待付款' and i ...
- Linq101-Ordering
using System; using System.Collections.Generic; using System.Linq; namespace Linq101 { class Orderin ...
- 初识Activity
Callback Description onCreate() This is the first callback and called when the activity is first cre ...
- Linux 抓取网站命令
wget -m -e robots=off -U "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.6) Gecko/200 ...