Problem E. TeaTree

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 722    Accepted Submission(s): 255

Problem Description
Recently, TeaTree acquire new knoledge gcd (Greatest Common Divisor), now she want to test you.
As we know, TeaTree is a tree and her root is node 1, she have n nodes and n-1 edge, for each node i, it has it’s value v[i].
For every two nodes i and j (i is not equal to j), they will tell their Lowest Common Ancestors (LCA) a number : gcd(v[i],v[j]).
For each node, you have to calculate the max number that it heard. some definition:
In graph theory and computer science, the lowest common ancestor (LCA) of two nodes u and v in a tree is the lowest (deepest) node that has both u and v as descendants, where we define each node to be a descendant of itself.
 
Input
On the first line, there is a positive integer n, which describe the number of nodes.
Next line there are n-1 positive integers f[2] ,f[3], …, f[n], f[i] describe the father of node i on tree.
Next line there are n positive integers v[2] ,v[3], …, v[n], v[i] describe the value of node i.
n<=100000, f[i]<i, v[i]<=100000
 
Output
Your output should include n lines, for i-th line, output the max number that node i heard.
For the nodes who heard nothing, output -1.
 
求树上每点的一个值
这个值 是 该点 以及它的子树所有点的 最大gcd
 
#include <iostream>
#include <vector> #define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=a-1;i>=b;i--) const int MX = 1e5;
const int MXX = *MX;
using namespace std; int n; vector<int> G[MX+],vv[MX+]; // init函数 实现将i因数分解 (i < 1e5)
void init() {
rep(i,,MX+) vv[i].push_back();
rep(i,,MX+) {
vv[i].push_back(i);
for(int j=i+i;j<=MX;j+=i) vv[j].push_back(i);
} // rep(i,1,MX+1) {
// rep(j,0,vv[i].size()) {
// printf("%d ",vv[i][j]);
// }puts("");
// }
} int root[MX+],ls[MXX],rs[MXX],sum[MXX],rear,ans[MX]; inline void push_up(int rt) {
if(ls[rt] && rs[rt]) sum[rt] = max(sum[ls[rt]],sum[rs[rt]]);
else if(ls[rt]) sum[rt] = sum[ls[rt]];
else if(rs[rt]) sum[rt] = sum[rs[rt]];
} void update(int &rt,int l,int r,int p) {
if(rt==) rt = ++rear;
if(l == r) {
sum[rt] = p;
return ;
}
int m = (l+r)>>;
if(p <= m) update(ls[rt],l,m,p);
else update(rs[rt],m+,r,p);
push_up(rt);
} int merge(int rt, int prt, int &ans) {
if(rt== || prt==) return rt^prt;
//这里维护最大的gcd
if(sum[rt] == sum[prt]) ans = max(ans,sum[rt]);
//这里只有有因子,就归并到rt上面
if(ls[rt] | ls[prt]) ls[rt] = merge(ls[rt], ls[prt], ans);
if(rs[rt] | rs[prt]) rs[rt] = merge(rs[rt], rs[prt], ans);
push_up(rt);
return rt;
} void dfs(int u) {
ans[u] = -;
rep(i, , G[u].size()) {
int v = G[u][i];
dfs(v);
root[u] = merge(root[u],root[v],ans[u]);
}
} int main () {
freopen("in.txt" ,"r",stdin);
freopen("out.txt","w",stdout);
init(); scanf("%d", &n);
//建边
rep(i,,n+) {
int fa; scanf("%d",&fa);
G[fa].push_back(i);
}
//对每个v[i]建线段树
rear=;
rep(i,,n+) {
int x; scanf("%d", &x);
root[i]=;
rep(j, , vv[x].size()) {
update(root[i], , MX, vv[x][j]);
}
}
//暴力更新 gcd
dfs();
//输出答案
rep(i,,n+) printf("%d\n", ans[i]);
return ;
}
 

hdu 6430 线段树 暴力维护的更多相关文章

  1. hdu 4288 线段树 暴力 **

    题意: 维护一个有序数列{An},有三种操作: 1.添加一个元素. 2.删除一个元素. 3.求数列中下标%5 = 3的值的和. 解题思路: 看的各种题解,今天终于弄懂了. 由于线段树中不支持添加.删除 ...

  2. V - Can you answer these queries? HDU - 4027 线段树 暴力

    V - Can you answer these queries? HDU - 4027 这个题目开始没什么思路,因为不知道要怎么去区间更新这个开根号. 然后稍微看了一下题解,因为每一个数开根号最多开 ...

  3. hdu 5068 线段树维护矩阵乘积

    http://acm.hdu.edu.cn/showproblem.php?pid=5068 题意给的略不清晰 m个询问:从i层去j层的方法数(求连段乘积)或者修改从x层y门和x+1层z门的状态反转( ...

  4. hdu 1556 Color the ball(线段树区间维护+单点求值)

    传送门:Color the ball Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/3276 ...

  5. hdu 4578 线段树(标记处理)

    Transformation Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 65535/65536 K (Java/Others) ...

  6. HDU 2795 线段树单点更新

    Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. HDU 6218 (线段树+set)

    HDU 6218 Bridge Problem : 给一个2×n的矩阵,一开始矩阵所有相邻点之间有一条边.有其.个询问,每次给出两个相邻的点的坐标,将其中的边删除或者添加,问如此操作之后整张图的割边数 ...

  8. 2018 CCPC 吉林站 H Lovers || HDU 6562 (线段树哦)

    http://acm.hdu.edu.cn/showproblem.php?pid=6562 题意: q次操作 1.将第l~r个数的左边和和右边都加上一个数d, 使得这个数变成 dsiddsid的形式 ...

  9. G - Queue HDU - 5493 线段树+二分

    G - Queue HDU - 5493 题目大意:给你n个人的身高和这个人前面或者后面有多少个比他高的人,让你还原这个序列,按字典序输出. 题解: 首先按高度排序. 设每个人在其前面有k个人,设比这 ...

随机推荐

  1. jQuery实例化的优势,为什么要有实例化,到底实例化后在解决什么问题?

    jQuery实例化对象的方法相比于普通方法 优势: 1.不需要出现大量的new关键字. 2.可实现链式写法. 3.书写更方便 实例化的原因: 1.实例化有利于管理程序中不同的DOM选择和处理(不同的选 ...

  2. sencha touch 问题汇总

    做sencha touch有一段时间了,目前而言,sencha touch在android上问题比较严重,在此对android中sencha touch的问题做一些汇总: 1.内存问题: 打包成安装程 ...

  3. 使用servlet3.0提供的API来进行文件的上传操作

    servlet 3.0针对文件上传做了一些优化,提供了一些更加人性化的API可以直接在request中的到文件的名称.文件size,MIME类型,以及用InputStream表示的文件流的信息 @Re ...

  4. 【BZOJ4094】[Usaco2013 Dec]Optimal Milking 线段树

    [BZOJ4094][Usaco2013 Dec]Optimal Milking Description Farmer John最近购买了N(1 <= N <= 40000)台挤奶机,编号 ...

  5. 从SVN一键对比版本

    公司的部署程序太多,每次部署安装完后,还得从SVN上对比版本,手工做实在太麻烦. 比如下面的一个版本 思路: 将需要检查的部件及安装的位置.SVN相关信息写入配置文件,然后程序读取配置文件 配置文件内 ...

  6. MUI窗口管理

    参考:窗口管理 http://dev.dcloud.net.cn/mui/window/ 页面初始化:在app开发中,若要使用HTML5+扩展api,必须等plusready事件发生后才能正常使用,m ...

  7. talib 中文文档(十二):Pattern Recognition Functions K线模式识别,形态识别

    Pattern Recognition Functions K线模式识别,形态识别 CDL2CROWS - Two Crows 函数名:CDL2CROWS 名称:Two Crows 两只乌鸦 简介:三 ...

  8. Python知识总汇

    一.python基础 python基础 python编码问题 逻辑运算 二.python数据类型 二.python数据类型 三.IO(文件处理) 三.IO(文件处理) 四.函数 函数基础 名称空间与作 ...

  9. 三个小时学会wordpress模板制作

    最近接了一个项目需要用wordpress建站,版面相对简单,ytkah就琢磨着自己来设计wordpress模板,首页栏目页文章页(很多网站无外乎就这些页面),其中栏目页和首页又很像,都是调用文章列表. ...

  10. RDD的基础知识

    以下的这些分析都是基于spark2.1进行的 (一)什么是RDD A Resilient Distributed Dataset (RDD), the basic abstraction in Spa ...