cdoj 574 High-level ancients dfs序+线段树
High-level ancients
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://acm.uestc.edu.cn/#/problem/show/574
Description
Love8909 is keen on the history of Kingdom ACM. He admires the heroic undertakings of Lxhgww and Haibo. Inspired by those sagas, Love8909 picked up his courage and tried to build up his own kingdom. He named it as A230.
After hard working for several years, Love8909 is about to fulfill his dream. However, there is still one thing to do: setting up the defense network. As Kingdom EDC looks at territory and people of A230 fiercely as a tiger does, Love8909 has to make it as soon as possible.
The defense network Love8909 wants to use is the same as the one used by Lxhgww and Haibo. He also connects all cities with roads which form a tree structure, and the capital city is City 1, which is the root of this tree. Love8909 sends commands to inform cities to add soldiers. The command, being same to those of the ancients, with two values, X and K, means sending K soldiers to City X, sending K+1 soldiers to sons of City X, sending K+2 soldiers to sons of sons of City X and so on. Initially there are no soldiers in any city.
Love8909 may adjust the arrangement of soldiers ever and again. He asks questions about how many soldiers in the subtree rooted at City X. A subtree rooted at City X includes City X itself and all of its descendants. As Love8909's military counselor, you are responsible to complete all his commands and answer his questions.
Input
The first line of the input will be an integer T (T≤20) indicating the number of cases.
For each case, the first line contains two integers: N P, representing the number of cities in A230 and number of operations given by love8909.
The next line lists N−1 integers, in which the ith number, denoted as Xi+1, represents there is a road from City Xi+1 to City i+1. Note that the City 1has been omitted. 1≤Xi+1≤N for 2≤i≤N.
Then P lines follow, each gives an operation. Each operation belongs to either kind:
A X K
. An adding-soldier command.Q X
. A question about how many soldiers in the subtree rooted at City X.
We guarantee that the cities form a rooted tree and the root is at City 1, which is the capital.
1≤N≤50000, 1≤P≤100000, 1≤X≤N, 0≤K≤1000.
Output
For each case, print Case #k:
first in a single line, in which k represents the case number which starts from 1. Then for each Query X
operation, print the answer in a single line.
Sample Input
1
7 10
1 1 2 2 5 5
Q 1
A 2 1
Q 1
Q 2
Q 5
A 5 0
Q 5
A 3 1
Q 1
Q 2
Sample Output
Case #1:
0
11
11
8
10
14
13
HINT
题意
给你一棵以1为根的树,有两个操作
1.A x k,让x增加k,x的儿子增加k+1,x的孙子增加k+2....x的t代儿子增加k+t
2.Q x , 查询x的子树的权值和是多少
题解:
看到处理子树问题,很显然的dfs序
dfs离散之后,维护线段树区间和,区间更新
我们很容易看出,他的更新是和deep有关的,deep越深的,更新越大
那么我们对于每个节点i,先区间更新y-deep[x],然后再使得更新一次,使得每个节点i增加deep[i]就好了
这样每个属于x的子树的都更新了 y - deep[x] + deep[i]
代码:
#include<iostream>
#include<stdio.h>
#include<vector>
using namespace std;
#define maxn 100010
struct Node
{
int l,r,d,val;
};
Node node[maxn];
vector<int>Q[maxn];
int TTT[maxn];
int cnt = ;
void dfs(int x,int fa,int d)
{
node[x].l = cnt;
node[x].d = d;
TTT[cnt]=x;
cnt++;
for(int i=;i<Q[x].size();i++)
{
int v = Q[x][i];
if(v==fa)continue;
dfs(v,x,d+);
}
node[x].r = cnt;
}
int d[maxn];
struct Seg
{
typedef long long SgTreeDataType;
struct treenode
{
int L , R ;
SgTreeDataType sum , lazy1 , lazy2;
}; treenode tree[maxn*]; inline void push_down(int o)
{
SgTreeDataType lazyval1 = tree[o].lazy1;
SgTreeDataType lazyval2 = tree[o].lazy2;
if(lazyval1==&&lazyval2==)return;
int L = tree[o].L , R = tree[o].R;
int mid = (L+R)/;
tree[*o].lazy1+=lazyval1;tree[*o].sum+=lazyval1*(mid-L+);
tree[*o+].lazy1+=lazyval1;tree[*o+].sum+=lazyval1*(R-mid); tree[*o].lazy2+=lazyval2;tree[*o].sum+=lazyval2*d[*o];
tree[*o+].lazy2+=lazyval2;tree[*o+].sum+=lazyval2*d[*o+];
tree[o].lazy1 = tree[o].lazy2 = ;
} inline void push_up(int o)
{
tree[o].sum = tree[*o].sum + tree[*o+].sum;
} inline void build_tree(int L , int R , int o)
{
tree[o].L = L , tree[o].R = R,tree[o].sum = tree[o].lazy1 = tree[o].lazy2 = ;
if(L==R)
d[o]=node[TTT[L]].d;
if (R > L)
{
int mid = (L+R) >> ;
build_tree(L,mid,o*);
build_tree(mid+,R,o*+);
d[o]=d[o*]+d[o*+];
}
} inline void updata1(int QL,int QR,SgTreeDataType v,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR)
{
tree[o].lazy1 += v;
tree[o].sum += v * (R-L+);
}
else
{
push_down(o);
int mid = (L+R)>>;
if (QL <= mid) updata1(QL,QR,v,o*);
if (QR > mid) updata1(QL,QR,v,o*+);
push_up(o);
}
} inline void updata2(int QL,int QR,SgTreeDataType v,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR)
{
tree[o].lazy2+=v;
tree[o].sum+=v*d[o];
}
else
{
push_down(o);
int mid = (L+R)>>;
if (QL <= mid) updata2(QL,QR,v,o*);
if (QR > mid) updata2(QL,QR,v,o*+);
push_up(o);
}
} inline SgTreeDataType query(int QL,int QR,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR) return tree[o].sum;
else
{
push_down(o);
int mid = (L+R)>>;
SgTreeDataType res = ;
if (QL <= mid) res += query(QL,QR,*o);
if (QR > mid) res += query(QL,QR,*o+);
push_up(o);
return res;
}
}
}T; int main()
{
int t;scanf("%d",&t);
for(int cas=;cas<=t;cas++)
{
printf("Case #%d:\n",cas);
int n,q;
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++)Q[i].clear(),node[i].val=;
cnt = ;
for(int i=;i<=n;i++)
{
int x,y;scanf("%d",&x);
y=i;
Q[x].push_back(y);
Q[y].push_back(x);
}
dfs(,-,);
T.build_tree(,n,);
string ch;
while(q--)
{
cin>>ch;
if(ch[]=='Q')
{
int x;scanf("%d",&x);
printf("%lld\n",T.query(node[x].l,node[x].r-,));
}
else
{
int x,y;scanf("%d%d",&x,&y);
T.updata1(node[x].l,node[x].r-,y-node[x].d,);
T.updata2(node[x].l,node[x].r-,,);
}
}
}
}
cdoj 574 High-level ancients dfs序+线段树的更多相关文章
- cdoj 574 High-level ancients dfs序+线段树 每个点所加权值不同
High-level ancients Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/s ...
- Educational Codeforces Round 6 E dfs序+线段树
题意:给出一颗有根树的构造和一开始每个点的颜色 有两种操作 1 : 给定点的子树群体涂色 2 : 求给定点的子树中有多少种颜色 比较容易想到dfs序+线段树去做 dfs序是很久以前看的bilibili ...
- 【BZOJ-3252】攻略 DFS序 + 线段树 + 贪心
3252: 攻略 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 339 Solved: 130[Submit][Status][Discuss] D ...
- Codeforces 343D Water Tree(DFS序 + 线段树)
题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...
- BZOJ2434 [Noi2011]阿狸的打字机(AC自动机 + fail树 + DFS序 + 线段树)
题目这么说的: 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母.经阿狸研究发现,这个打字机是这样工作的: 输入小 ...
- POJ 3321 DFS序+线段树
单点修改树中某个节点,查询子树的性质.DFS序 子树序列一定在父节点的DFS序列之内,所以可以用线段树维护. 1: /* 2: DFS序 +线段树 3: */ 4: 5: #include < ...
- 【XSY2667】摧毁图状树 贪心 堆 DFS序 线段树
题目大意 给你一棵有根树,有\(n\)个点.还有一个参数\(k\).你每次要删除一条长度为\(k\)(\(k\)个点)的祖先-后代链,问你最少几次删完.现在有\(q\)个询问,每次给你一个\(k\), ...
- F - Change FZU - 2277 (DFS序+线段树)
题目链接: F - Change FZU - 2277 题目大意: 题意: 给定一棵根为1, n个结点的树. 有q个操作,有两种不同的操作 (1) 1 v k x : a[v] += x, a[v ' ...
- BZOJ4551[Tjoi2016&Heoi2016]树——dfs序+线段树/树链剖分+线段树
题目描述 在2016年,佳媛姐姐刚刚学习了树,非常开心.现在他想解决这样一个问题:给定一颗有根树(根为1),有以下 两种操作:1. 标记操作:对某个结点打上标记(在最开始,只有结点1有标记,其他结点均 ...
随机推荐
- sql给数据库加锁问题
加锁是在操作数据时进行了,不能事后加锁. 例: begin tran insert 表 with(TABLOCKX) --加锁 (字段列表) ...
- OSGI框架学习
OSGI框架三个重要概念 OSGi框架是根据OSGi规范中定义的三个概念层设计的:模块.模块生命周期.服务. 模块层定义了OSGi模块的概念(bundle,即包含一个元数据MANIFEST.MF的JA ...
- 五分钟solr4.5教程(搭建、运行)
环境要求 jdk1.6及以上版本 solr发布版本 下载地址 http://lucene.apache.org/solr/mirrors-solr-latest-redir.html? 启动solr ...
- IO负载高的来源定位
前言: 在一般运维工作中经常会遇到这么一个场景,服务器的IO负载很高(iostat中的util),但是无法快速的定位到IO负载的来源进程和来源文件导致无法进行相应的策略来解决问题. 这个现象在MySQ ...
- 1050 数的计数 c语言实现
描述 给定一个正整数,求其各位之和. 输入 输入一行,为一个正整数(最多10,000位). 输出 输出各位之和. 样例输入 17 样例输出 8 解析:这题主要是大数计算的问题,因为10000位的数无法 ...
- 开源侧滑菜单SlidingMenu主要方法介绍
SlidingMenu是一个很好使用的侧滑菜单开源项目,它的表现形式类似于DrawerLayout和SlidingDrawer,具体效果如下图所示,左侧为侧滑Menu菜单,右侧黑色部分为内容显示视图C ...
- ant 安装过程中问题记录
最近在本机安装ant过程中出现一些问题,在此记录一下. 1.Unable to locate tools.jar. Expected to find it in C:/Program Files/Ja ...
- cocos2d CCLayer 触摸相关
要让一个 CCLayer 能够接受触摸输入 需要进行如下设置: [selfsetTouchEnabled:YES]; cocos2d-x提供了两种触摸事件处理机制, 分别是CCStandardTo ...
- 使用Visual Studio 2013对windows应用商店应用进行编码的UI测试
之前进行了一次实验,实验内容是对windows应用商店应用进行编码的UI测试,下面对实验步骤进行详细说明: 1.为 Windows 应用商店应用创建新编码的 UI 测试项目,选择Visual C##→ ...
- javascript 继承、命名空间实现分享
命名空间是用来组织和重用代码的编译单元,在大型项目开发中javascript使用的越来越多时,我们就应该将项目里的js类库管理起来,如何将自己进行归类管理,防止命名冲突,但是Javascript默认不 ...