hdu 5678 ztr loves trees (给一颗有根树,树上的每一个节点有一个权值,每次询问某个子树中所有权值的中位数)

本文介绍了一种解决子树中位数查询问题的方法,利用DFS序和主席树技术将子树查询转换为区间查询,有效解决了多次查询的计算难题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ztr loves trees

Time Limit: 6000/2500 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 296    Accepted Submission(s): 46


Problem Description
Super Deity ztr likes trees from childhood,CCTV-children:"The apple on the apple tree.You and I under the apple tree.Play games in front of the apple tree.So many happiness".

One day,qzh visit ztr to ask some questions.To give a tree with a root,each vertex has a value.Each time query the median of a subtree.

ztr said:this is a water problem,do you do it?But qzh show cannot help but want you who is also a Super Deity to help him.Could you help him?
 

Input
There are T test cases. The first line of input contains an positive integer T indicating the number of test cases.

For each test case:

Each line contains two positive integer n,m.indicating the number of vetrex and the number of query times.
The next line contains n numbers, the ith number indicating the value of vertex i.
The next n-1 lines,each line contains two numbers u and v,indicating there is a edge form u to v.

The next m lines, each line contains a numbers x.indicating query the median of subtree x.

1<=T<=3,1<=n<=105,1<=m<=106,1<=u<=v<=n,1<=val<=109.
The vetrex 1 is the root of the tree.Guarantee input a tree with a root.
 

Output
For each test case:print a line.To avoid huge output,you should hash each answer first,then print it.

The method to hash:a[i] indicates the ith query result, ans=a[i]10mimod1,000,000,007  Round to the nearest tenth
 

Sample Input
  
  
1 5 3 1 2 3 4 5 1 2 2 3 3 4 4 5 1 2 3
 

Sample Output
  
  
339.0
 

给一颗有根树,树上的每一个节点有一个权值,每次询问某个子树中所有权值的中位数

 

思路:

这道题就是在一个子树上询问第k大,于是我们用dfs序将树上询问第k大转化为区间询问第k大,套用主席树即可.

#include<cstdio>
#include<algorithm>
#include<time.h>
#include<iostream>
#include<cstdio>
#include<queue>
#include<string.h>
#include<math.h>
#include<vector>
#include<algorithm>
#include<stdio.h>
#include<map>
using namespace std;
#define maxn 100005
const int MOD=1000000007;
int val[maxn],L[maxn],R[maxn],tot,a[maxn],t[maxn],vis[maxn];
vector<int>G[maxn];
int root[maxn],sz;
double ans[maxn];
struct node{
    int l,r,w;
}T[maxn*30];

void dfs(int u){
    vis[u]=1;
    L[u]=++tot;
    t[tot]=val[u],a[tot]=val[u];
    for(int i=0;i<G[u].size();i++){
        int v=G[u][i];
        if(vis[v]==1)
            continue;
        dfs(v);
    }
    R[u]=tot;
}

void update(int &i,int l,int r,int num){
    T[++sz]=T[i],i=sz;
    T[i].w++;
    if(l==r)
        return ;
    int mid=(l+r)>>1;
    if(num<=mid)
        update(T[i].l,l,mid,num);
    else
        update(T[i].r,mid+1,r,num);
}

int query(int x,int y,int l,int r,int k){
    if(l==r)
        return l;
    int mid=(l+r)>>1;
    int num=T[T[y].l].w-T[T[x].l].w;
    if(num>=k)
        return query(T[x].l,T[y].l,l,mid,k);
    else
        return query(T[x].r,T[y].r,mid+1,r,k-num);
}

int main(){
    int _,n,m,u,v,q;
    scanf("%d",&_);
    while(_--){
        scanf("%d%d",&n,&q);
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++){
            scanf("%d",&val[i]);
            G[i].clear();
        }
        for(int i=1;i<n;i++){
            scanf("%d%d",&u,&v);
            G[u].push_back(v);
            G[v].push_back(u);
        }
        tot=0;
        dfs(1);
        sort(t+1,t+n+1);
        int m=unique(t+1,t+n+1)-t-1;
        root[0]=0,sz=0;
        for(int i=1;i<=n;i++){  //有序
            root[i]=root[i-1];
            int num=lower_bound(t+1,t+m+1,a[i])-t;
            update(root[i],1,n,num);
        }
        for(int i=1;i<=n;i++){
            if((R[i]-L[i])%2==1){
                int num1=query(root[L[i]-1],root[R[i]],1,n,(R[i]-L[i]+1)/2);
                int num2=query(root[L[i]-1],root[R[i]],1,n,(R[i]-L[i]+1)/2+1);
                ans[i]=1.0*(t[num1]+t[num2])/2;
            }
            else
                ans[i]=t[query(root[L[i]-1],root[R[i]],1,n,(R[i]-L[i])/2+1)];
        }
        double ANS=0;
        for(int i=1;i<=q;i++){
            scanf("%d",&u);
            ANS=fmod(ans[u]+ANS*10,1.0*MOD);
        }
        printf("%.1f\n",ANS);
    }
    return 0;
}


参考资源链接:[HDU数字图像处理期末试卷复习资料](https://wenku.youkuaiyun.com/doc/771vi6wnkt?utm_source=wenku_answer2doc_content)HDU数字图像处理期末试卷复习资料》是一份宝贵的资源,它包含了历年试题和重要知识,能够帮助你针对性地准备期末考试。为了有效利用这份资料,你可以遵循以下步骤来制定一个复习计划: 1. 理解考试要求和评分标准:首先,明确考试的形式和范围,包括理论部分和实践操作部分,了解哪些内容是考试的重,哪些部分需要特别注意。 2. 划分复习模块:根据数字图像处理课程的知识结构,将复习内容分为不同的模块,如图像处理基础理论、图像分析和理解、实际应用案例、编程和实现等。 3. 制定详细时间表:为每个模块设定具体的复习时间,合理分配时间,确保每个部分都有足够的复习时间。根据自己的掌握程度,对难以理解的部分可以适当增加复习时间。 4. 理论知识复习:通过老师的讲义、课堂笔记以及复习资料中的概念性内容,全面复习理论知识。对于不熟悉或难以理解的理论,可以查找相关文献进行深入学习。 5. 重题目练习:利用历年试题进行实战演练,重关注计算题和编程题,通过实际操作来加深对算法的理解。对于实践性题目,可以通过使用MATLAB、OpenCV等图像处理库进行实际编程练习。 6. 模拟测试:在复习计划的后期,进行模拟测试,按照正式考试的时间限制来完成试题。这有助于你适应考试的奏,同时检验复习效果。 7. 错题集整理:对于模拟测试和练习过程中出现的错误,要进行详细的整理和复习,确保不再犯同样的错误。 8. 考前总结:考试前的最后几天,应该进行知识的快速回顾,重复习易错和重,调整好心态,保持良好的作息,确保考试状态最佳。 利用《HDU数字图像处理期末试卷复习资料》进行有效复习,不仅能帮助你系统地掌握课程知识,还能提升你的实践操作能力,为考试做好充分准备。希望这份计划能够帮助你在期末考试中取得优异的成绩。 参考资源链接:[HDU数字图像处理期末试卷复习资料](https://wenku.youkuaiyun.com/doc/771vi6wnkt?utm_source=wenku_answer2doc_content)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值