HDU - 6430:TeaTree (线段树合并)
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.
InputOn 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]<=100000OutputYour 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.Sample Input
4
1 1 3
4 1 6 9
Sample Output
2
-1
3
-1
题意:对于每个点,求以它为LCA的最大GCD。
思路:求出每个点的子树的 因子线段树,然后暴力合并,3000ms过了,(没有启发式,就是裸的合并,我也不知道复杂度怎么算的)。
不过好像可以启发式(我尝试了下用size来排序后合并,时间上并没有优化,所以不知所措); 以及bitset两种方式来优化。占位。
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
struct in{ int l,r,Max; }s[maxn*];
vector<int>G[maxn],P[maxn]; //图,因子
int rt[maxn],ans[maxn],a[maxn],cnt;
void prepare()
{
for(int i=;i<maxn;i++)
for(int j=i;j<maxn;j+=i)
P[j].push_back(i);
}
void update(int &Now,int L,int R,int val)
{
if(!Now) Now=++cnt;
if(L==R){ s[Now].Max=val; return ;}
int Mid=(L+R)>>;
if(val<=Mid) update(s[Now].l,L,Mid,val);
else update(s[Now].r,Mid+,R,val);
s[Now].Max=max(s[s[Now].l].Max,s[s[Now].r].Max);
}
int merrge(int u,int v,int &ans)
{
if(!u||!v) return u|v;
if(s[u].Max==s[v].Max) ans=max(ans,s[u].Max);
if(s[u].l||s[v].l) s[u].l=merrge(s[u].l,s[v].l,ans);
if(s[u].r||s[v].r) s[u].r=merrge(s[u].r,s[v].r,ans);
return u;
}
void dfs(int u)
{
for(int i=,L=G[u].size();i<L;i++){
dfs(G[u][i]);
merrge(rt[u],rt[G[u][i]],ans[u]);
}
}
int main()
{
prepare();
int N,x,mx=; scanf("%d",&N);
rep(i,,N) ans[i]=-;
rep(i,,N) scanf("%d",&x),G[x].push_back(i);
rep(i,,N) scanf("%d",&a[i]),mx=max(mx,a[i]);
rep(i,,N) {
for(int j=,L=P[a[i]].size();j<L;j++)
update(rt[i],,mx,P[a[i]][j]);
}
dfs();
rep(i,,N) printf("%d\n",ans[i]);
return ;
}
HDU - 6430:TeaTree (线段树合并)的更多相关文章
- 2018多校第十场 HDU 6430 (线段树合并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6430 题意:一棵树上每个节点权值为v[i],每个节点的heard值是:以它为LCA的两个节点的GCD的 ...
- 2017多校第8场 HDU 6133 Army Formations 线段树合并
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6133 题意:给你一棵n个节点的二叉树,每个节点有一个提交任务的时间,每个节点总的提交任务的罚时为:提交 ...
- hdu 5511 Minimum Cut-Cut——分类讨论思想+线段树合并
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5511 题意:割一些边使得无向图变成不连通的,并且恰好割了两条给定生成树上的边.满足非树边两段一定在给定生成 ...
- HDU 5649 DZY Loves Sorting(二分答案+线段树/线段树合并+线段树分割)
题意 一个 \(1\) 到 \(n\) 的全排列,\(m\) 种操作,每次将一段区间 \([l,r]\) 按升序或降序排列,求 \(m\) 次操作后的第 \(k\) 位. \(1 \leq n \le ...
- HDU - 6704 K-th occurrence (后缀数组+主席树/后缀自动机+线段树合并+倍增)
题意:给你一个长度为n的字符串和m组询问,每组询问给出l,r,k,求s[l,r]的第k次出现的左端点. 解法一: 求出后缀数组,按照排名建主席树,对于每组询问二分或倍增找出主席树上所对应的的左右端点, ...
- HDU - 4358 Boring counting (树上启发式合并/线段树合并)
题目链接 题意:统计树上每个结点中恰好出现了k次的颜色数. dsu on tree/线段树合并裸题. 启发式合并1:(748ms) #include<bits/stdc++.h> usin ...
- BZOJ 4771 七彩树(可持久化线段树合并)
题意 https://www.lydsy.com/JudgeOnline/problem.php?id=4771 思路 和 HDU 3333 其实有点像,不过是把序列的问题放在了树上,多维护一个深度即 ...
- [XJOI NOI2015模拟题13] C 白黑树 【线段树合并】
题目链接:XJOI - NOI2015-13 - C 题目分析 使用神奇的线段树合并在 O(nlogn) 的时间复杂度内解决这道题目. 对树上的每个点都建立一棵线段树,key是时间(即第几次操作),动 ...
- [BZOJ 2212] [Poi2011] Tree Rotations 【线段树合并】
题目链接:BZOJ - 2212 题目分析 子树 x 内的逆序对个数为 :x 左子树内的逆序对个数 + x 右子树内的逆序对个数 + 跨越 x 左子树与右子树的逆序对. 左右子树内部的逆序对与是否交换 ...
随机推荐
- hadoop 2.0安装及HA配置简述
一.单机模式 a.配置本机到本机的免密登录 b.解压hadoop压缩包,修改hadoop.env.sh中的JAVA_HOME c.修改core-site.xml <configuration&g ...
- Boot-Repair&usb_repair
https://help.ubuntu.com/community/Boot-Repair https://askubuntu.com/questions/500647/unable-to-mount ...
- 关于JS解析编历JSON数组(含多维数组)
第一部分: ].xing) 第二部分: } 解释: countryObj 为value对象的一个属性明 value[coun ...
- 《网络攻防》 MSF基础应用
20145224陈颢文 <网络攻防>MSF基础应用 基础问题回答 用自己的话解释什么是exploit,payload,encode: exploit:攻击手段,是能使攻击武器(payloa ...
- 【传感器】BMA253 数字,三轴加速度传感器
参考文档:BMA253E DataSheet 参考文档链接 密码:9new BMA253 数字,三轴加速度传感器 关键特性: 关键特性 封装方式 LGA封装(12pins),长*宽(2mm*2mm ...
- Easy-RSA 3快速入门自述文件
Easy-RSA 3快速入门自述文件 这是使用Easy-RSA版本3的快速入门指南.运行./easyrsa -h可以找到有关使用和特定命令的详细帮助.可以在doc /目录中找到其他文档. 如果您从Ea ...
- 用“倍增法”求最近公共祖先(LCA)
1.最近公共祖先:对于有根树T的两个结点u.v,最近公共祖先LCA(T,u,v)表示一个结点x,满足x是u.的祖先且x的深度尽可能大. 2.朴素算法:记录下每个节点的父亲,使节点u,v一步一步地向上找 ...
- ubuntu 致命错误: zlib.h:没有那个文件或目录【转】
本文转载自:https://blog.csdn.net/u013359794/article/details/44922685?locationnum=15&fps=1 编译时,出现错误,提示 ...
- Linux系统故障-Repair filesystem
fsck /dev/hddn (代表根目录所在的区) fsck -A -y 重启系统可以进去了:- ) fsck命令的主要选项如下: -A 检查所有列在etc/fstab文件中的文件系统.带有这个选项 ...
- log4j.appender.AFile.File日志的相对路径
log4j.appender.AFile=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.AFile.DatePattern='.'yy ...