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 左子树与右子树的逆序对. 左右子树内部的逆序对与是否交换 ...
随机推荐
- Codeforces - 55D Beautiful numbers (数位dp+数论)
题意:求[L,R](1<=L<=R<=9e18)区间中所有能被自己数位上的非零数整除的数的个数 分析:丛数据量可以分析出是用数位dp求解,区间个数可以转化为sum(R)-sum(L- ...
- c++中指针作为函数参数的详细理解
在C语言中,函数的参数不仅可以是整数.小数.字符等具体的数据,还可以是指向它们的指针.用指针变量作函数参数可以将函数外部的地址传递到函数内部,使得在函数内部可以操作函数外部的数据,并且这些数据不会随着 ...
- NSwag Tutorial: Integrate the NSwag toolchain into your ASP.NET Web API project
https://blog.rsuter.com/nswag-tutorial-integrate-the-nswag-toolchain-into-your-asp-net-web-api-proje ...
- sqlserver一般需求
1.创建一个函数:将用逗号分隔的字符串变成一个表的一列,这样就可以运用在select 语句的in中查询 create Function StrToTable(@str varchar()) Retur ...
- 全卷积网络(FCN)与图像分割
最近在做物体检测,也用到了全卷积网络,来此学习一波. 这篇文章写了很好,有利于入门,在此记录一下: http://blog.csdn.net/taigw/article/details/5140144 ...
- DPDK l3fwd
l3fwd负责三层转发,比l2fwd要复杂点. /*- * BSD LICENSE * * Copyright(c) 2010-2014 Intel Corporation. All rights r ...
- 通过HBase API进行开发
http://www.cnblogs.com/netbloomy/p/6683509.html 一.将HBase的jar包及hbase-site.xml添加到IDE 1.到安装HBase集群的任意一台 ...
- 新东方雅思词汇---8.2、chron
新东方雅思词汇---8.2.chron 一.总结 一句话总结:时间 chronic 英 ['krɒnɪk] 美 ['krɑnɪk] adj. 慢性的:长期的:习惯性的 n. (Chronic)人名 ...
- python之list,tuple,str,dic简单记录(一)
list是处理一组有序项目的数据结构,即你可以在一个列表中存储一个序列的项目.列表中的项目.列表中的项目应该包括在方括号中,这样python就知道你是在指明一个列表.一旦你创建了一个列表,你就可以添加 ...
- 牛客比赛-假的字符串-Trie+拓扑
链接:https://www.nowcoder.com/acm/contest/59/B来源:牛客网 题目描述 给定n个字符串,互不相等,你可以任意指定字符之间的大小关系(即重定义字典序),求有多少个 ...