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 左子树与右子树的逆序对. 左右子树内部的逆序对与是否交换 ...
随机推荐
- 关于Webpage Not Found问题解决~~~
还是外文网站好,以下解决办法: IIS6.0 UI vs. IIS 7.x UI Series: More about Web Service Extensions This week in the ...
- 编写Tesseract的Python扩展
Tesseract是一个开源的OCR(光学字符识别)引擎,用于识别并输出图片中的文字.虽然和商业软件比起来识别精度不算很高,但是如果你要寻找免费开源的OCR引擎,可能Tesseract就是唯一的选择了 ...
- HTTP与服务器的四种交互方式
Http定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT,DELETE.URL全称是资源描述符,我们可以这样认为:一个URL地址,它用于描述一个网络上的资源,而HTTP ...
- CSS Id 和 Class选择器
CSS Id 和 Class选择器 如果你要在HTML元素中设置CSS样式,你需要在元素中设置"id" 和 "class"选择器. 一.id 选择器 id 选择 ...
- 防止CSRF的攻击—Origin和Referer
防止CSRF的攻击—Origin和Referer 为了防止CSRF的攻击,我们建议修改浏览器在发送POST请求的时候加上一个Origin字段,这个Origin字段主要是用来标识出最初请求是从哪里发起的 ...
- [pixhawk笔记]4-如何写一个简单的应用程序
本文主要内容来自于:https://dev.px4.io/en/tutorials/tutorial_hello_sky.html,并对文档中的部分问题进行更正. 本文假设已经建立好开发环境并能正确编 ...
- 解决 SMTP Error: Could not connect to SMTP host. 问题
我在使用PHPmailer发邮件时候,遇到了这个问题“SMTP Error: Could not connect to SMTP host.”,分享一下解决方法. 这个错误是PHP版本7产生的.如果我 ...
- 20162326 齐力锋 2017-2018学期 Bag类的补写博客
要求: 代码运行在命令行中,路径要体现学号信息,IDEA中,伪代码要体现个人学号信息 参见Bag的UML图,用Java继承BagInterface实现泛型类Bag,并对方法进行单元测试(JUnit), ...
- 关于ARM指令中位置无关和位置相关代码的认识【转】
本文转载自:https://blog.csdn.net/talent_CYJ/article/details/50533153 今天在一个问题上折腾了又是半天.就是在学JZ2440串口通信的时候,在s ...
- Python 类的三大特性的综合运用 案例
# --------------------- 类的三大特性的综合运用 案例 ------------------------- # 定义三个类:小狗,小猫,人 # 小狗:姓名,年龄(默认1岁) 吃饭 ...