【BZOJ1803】Spoj1487 Query on a tree III 主席树+DFS序

本文介绍了一种解决BZOJ1803树上查询问题的方法,利用主席树结合DFS序实现对树中节点标签的第k大查询。通过具体的代码示例展示了如何构建树结构,进行DFS遍历以及实现主席树的相关操作。

【BZOJ1803】Spoj1487 Query on a tree III

Description

You are given a node-labeled rooted tree with n nodes. Define the query (x, k): Find the node whose label is k-th largest in the subtree of the node x. Assume no two nodes have the same labels.

Input

The first line contains one integer n (1 <= n <= 10^5). The next line contains n integers li (0 <= li <= 109) which denotes the label of the i-th node. Each line of the following n - 1 lines contains two integers u, v. They denote there is an edge between node u and node v. Node 1 is the root of the tree. The next line contains one integer m (1 <= m <= 10^4) which denotes the number of the queries. Each line of the next m contains two integers x, k. (k <= the total node number in the subtree of x)

Output

For each query (x, k), output the index of the node whose label is the k-th largest in the subtree of the node x.

Sample Input

5
1 3 5 2 7
1 2
2 3
1 4
3 5
4
2
3
4 1
3 2
3 2

Sample Output

5 4 5 5

题解:这样例你告诉我是求子树第k大?(the k_th largest)分明是第k小啊!

直接主席树+DFS序搞定。

#include <cstdio> 
#include <cstring> 
#include <iostream> 
#include <algorithm> 
using namespace std; 
const int maxn=100010; 
int n,m,cnt,tot; 
int to[maxn<<1],next[maxn<<1],head[maxn],v[maxn],p[maxn],q[maxn],rt[maxn]; 
int ls[maxn*30],rs[maxn*30],siz[maxn*30],s[maxn*30]; 
struct number 
{ 
    int val,org; 
}num[maxn]; 
bool cmp(number a,number b) 
{ 
    return a.val<b.val; 
} 
void insert(int x,int &y,int l,int r,int a,int b) 
{ 
    if(l>r)  return ; 
    y=++tot,siz[y]=siz[x]+1; 
    if(l==r) 
    { 
        s[y]=b; 
        return ; 
    } 
    int mid=l+r>>1; 
    if(a<=mid)   rs[y]=rs[x],insert(ls[x],ls[y],l,mid,a,b); 
    else    ls[y]=ls[x],insert(rs[x],rs[y],mid+1,r,a,b); 
} 
int query(int a,int b,int l,int r,int c) 
{ 
    if(l==r)    return s[a]; 
    int mid=l+r>>1,sm=siz[ls[a]]-siz[ls[b]]; 
    if(c<=sm)    return query(ls[a],ls[b],l,mid,c); 
    else    return query(rs[a],rs[b],mid+1,r,c-sm); 
} 
void dfs(int x,int fa) 
{ 
    p[x]=++p[0]; 
    insert(rt[p[0]-1],rt[p[0]],1,n,v[x],x); 
    for(int i=head[x];i!=-1;i=next[i])  if(to[i]!=fa)   dfs(to[i],x); 
    q[x]=p[0]; 
} 
void add(int a,int b) 
{ 
    to[cnt]=b,next[cnt]=head[a],head[a]=cnt++; 
} 
int main() 
{ 
    scanf("%d",&n); 
    int i,a,b; 
    for(i=1;i<=n;i++)    scanf("%d",&num[i].val),num[i].org=i; 
    sort(num+1,num+n+1,cmp); 
    for(i=1;i<=n;i++)    v[num[i].org]=i; 
    memset(head,-1,sizeof(head)); 
    for(i=1;i<n;i++) 
    { 
        scanf("%d%d",&a,&b); 
        add(a,b),add(b,a); 
    } 
    dfs(1,0); 
    scanf("%d",&m); 
    for(i=1;i<=m;i++) 
    { 
        scanf("%d%d",&a,&b); 
        printf("%d\n",query(rt[q[a]],rt[p[a]-1],1,n,b)); 
    } 
    return 0; 
}

 

转载于:https://www.cnblogs.com/CQzhangyu/p/6952518.html

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值