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 左子树与右子树的逆序对. 左右子树内部的逆序对与是否交换 ...
随机推荐
- html 基础 表单
一.表单 <form id="" name="" method="post/get" action="负责处理的服务端&qu ...
- JavaScript判断对象 是什么类型的.
// 这种方法不起作用 if (x == undefined) { // 作某些操作 } // 这个方法同样不起作用- if (typeof(x) == undefined) { // 作某些 ...
- error: 'for' loop initial declarations are only allowed in C99 mode
error: 'for' loop initial declarations are only allowed in C99 mode 出现错误: error: 'for' loop initia ...
- idea发布到tomcat缺少jar
主要是需要自己添加jar到artifact,如果单个添加jar,则在右边栏右键选择 如果是把整个lib添加到toamcat发布,则选择下面的提示:
- C++线程池总结
本文采用pthread实现线程池,有以下几个类. CTask:任务抽象类,主要提供接口,供子类实现. CMyTask:继承CTask实现接口 CThreadPool:线程池类,用于管理线程. 信号量: ...
- Caffe实现多标签输入,添加数据层(data layer)
因为之前遇到了sequence learning问题(CRNN),里面涉及到一张图对应多个标签.Caffe源码本身是不支持多类标签数据的输入的. 如果之前习惯调用脚本create_imagenet.s ...
- centos7 部署 dotnetcore
一.在centos上下载dotnetcore SDK 二. 选择Linux发行版安装 按上面的步骤安装,在centos终端输入dotnet --version 显示版本信息即安装成功
- Spring与CXF整合
1.首先引入CXF相关jar包以及spring相关jar包,因项目是maven项目,所以直接在pom.xml文件中引入以下依赖即可(以下只是CXF的依赖包,Spring的也要引入,相关的依赖参考我博客 ...
- Pandas迭代
Pandas对象之间的基本迭代的行为取决于类型.当迭代一个系列时,它被视为数组式,基本迭代产生这些值.其他数据结构,如:DataFrame和Panel,遵循类似惯例迭代对象的键. 简而言之,基本迭代( ...
- Java_WebKit_ZC01
1. 1.1. F:\ZC_chrome_download\java_svg\__Java_call_Qt\qtjambi-master\doc\src\snippets\application.xm ...