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面试题及答案
JAVA 1.GC是什么? 为什么要有GC? GC是垃圾收集的意思(Gabage Collection),内存处理是编程人员容易出现问题的地方,忘记或者错误的内存回收会导致程序或系统的不稳定甚至崩溃, ...
- STL——临时对象的产生与运用
所谓临时对象,就是一种无名对象.它的出现如果不在程序员的预期之下(例如任何pass by value操作都会引发copy操作,于是形成一个临时对象),往往造成效率上的负担.但有时候刻意制造一些临时对象 ...
- codeforces 251A Points on Line(二分or单调队列)
Description Little Petya likes points a lot. Recently his mom has presented him n points lying on th ...
- 中国剩余定理模板poj1006
#include <cstdio> #include <iostream> #include <cstring> #include <cmath> #i ...
- css 圆角效果
http://intacto10years.com/index_start.php<div style="width:800px; height:1300px;">&l ...
- My First Blog.
I just wanna mark my first blog in order to have a wonderful memories in the future.
- leetcode修炼之路——83. Remove Duplicates from Sorted List
哈哈,我又来了.昨天发现题目太简单就没有放上来,今天来了一道有序链表的题.题目如下: Given a sorted linked list, delete all duplicates such th ...
- Python算术运算符
Python 运算符 什么是运算符? 本章节主要说明Python的运算符.举个简单的例子 4 +5 = 9 . 例子中,4和5被称为操作数,"+"号为运算符. Python语言支持 ...
- Linux sed命令在指定行前后添加内容
一.在匹配行前后加内容在包含www.baidu.com的行前面或后面添加多一行内容www.qq.com#匹配行前加sed -i '/www.baidu.com/i www.qq.com' domain ...
- 简单的js反选,全选,全不选
<html> <head> <base href="<%=basePath%>"> <title>My JSP 'che ...