Count on a tree

本文介绍了一种在树形结构上实现主席树的方法,并结合容斥原理解决特定查询问题。通过从父节点构建树的方式,实现了对树上路径的第k大元素查询。代码示例使用C++实现,包括树的DFS遍历、LCA查找等关键步骤。

树上的主席树。
从父节点建树。
容斥原理 root[a]+root[b]-root[lca(a,b)]-root[fa[lca(a,b)]] 上的第k大。

#include <cstdio>
#include <iostream>
#include <algorithm>
#define il inline
#define ls l,mid
#define rs mid+1,r
using namespace std;
const int maxm=410000;
int val[maxm],hash[maxm];
int head[maxm],net[2*maxm],to[2*maxm];
int cnt,t;
int dep[maxm],fa[maxm][17];
int pos[maxm],id[maxm],ind;
int root[maxm];
int sum[maxm<<5],lson[maxm<<5],rson[maxm<<5],size;
int build(int l,int r)
{
    int rt=++size;
    sum[rt]=0;
    if(l<r)
    {
        int mid=(l+r)>>1;
        rson[rt]=build(rs);
        lson[rt]=build(ls);
    }
    return rt;
}
void add(int x,int y)
{
    cnt++;
    to[cnt]=y;
    net[cnt]=head[x];
    head[x]=cnt;
} 
il int read()
{
    int x=0,w=1;
    char ch=0;
    while(ch<'0'||ch>'9')
    {
        if(ch=='-') w=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=(x<<3)+(x<<1)+ch-'0';
        ch=getchar();
    }
    return x*w;
}
int updata(int pre,int l,int r,int v)
{
    int rt=++size;
    lson[rt]=lson[pre],rson[rt]=rson[pre],sum[rt]=sum[pre]+1;
    if(l<r)
    {
        int mid=(l+r)>>1;
        if(v<=mid) lson[rt]=updata(lson[pre],ls,v);
        else rson[rt]=updata(rson[pre],rs,v);
    }
    return rt;
}
void dfs(int x)
{
    ind++,id[x]=ind,pos[ind]=x;
    for(int i=1;i<=16&&(1<<i)<=dep[x];i++)
     fa[x][i]=fa[fa[x][i-1]][i-1];
    root[id[x]]=updata(root[fa[x][0]],1,t,val[x]);
    for(int i=head[x];i;i=net[i])
     if(to[i]!=fa[x][0]) fa[to[i]][0]=x,dep[to[i]]=dep[x]+1,dfs(to[i]);
}
il int LCA(int x,int y)
{
    if(dep[x]<dep[y]) swap(x,y);
    int t=dep[x]-dep[y];
    for(int i=0;i<=16;i++)
     if((1<<i)&t) x=fa[x][i];
    for(int i=16;i>=0;i--)
     if(fa[x][i]!=fa[y][i])
      x=fa[x][i],y=fa[y][i];
    if(x==y) return x;
    return fa[x][0];
}
il int ask(int a,int b,int c,int d,int l,int r,int kth)
{
   if(l>=r) return hash[l];
   int nowsum=sum[lson[a]]+sum[lson[b]]-sum[lson[c]]-sum[lson[d]];
   int mid=(l+r)>>1;
   if(nowsum>=kth) return ask(lson[a],lson[b],lson[c],lson[d],ls,kth);
   else return ask(rson[a],rson[b],rson[c],rson[d],rs,kth-nowsum);
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
     scanf("%d",&val[i]),hash[i]=val[i];
    sort(hash+1,hash+n+1);
    t=unique(hash+1,hash+n+1)-hash-1;
    for(int i=1;i<=n;i++)
     val[i]=lower_bound(hash+1,hash+t+1,val[i])-hash;
    for(int i=1,u,v;i<n;i++)
     scanf("%d%d",&u,&v),add(u,v),add(v,u);
    dfs(1); 
    //root[0]=build(1,t);
    int last=0;
    for(int i=1;i<=m;i++)
    {
        int u,v,k;
        scanf("%d%d%d",&u,&v,&k);
        u^=last;
        int lca=LCA(u,v);
        last=ask(root[id[u]],root[id[v]],root[id[lca]],root[id[fa[lca][0]]],1,t,k);
        printf("%d",last);
        if(i!=m) puts("");
    }

    return 0;
}
# COT - Count on a tree ## 题面翻译 # 本题必须使用 C++98 提交 给你一棵有n个结点的树,节点编号为1~n。 每个节点都有一个权值。 要求执行以下操作: U V K:求从节点u到节点v的第k小权值。 # 输入输出格式 ## 输入格式 第一行有两个整数n和m(n,m≤100000) 第二行有n个整数。 第i个整数表示第i个节点的权值。 接下来的n-1行中,每行包含两个整数u v,表示u和v之间有一条边。 接下来的m行,每行包含三个整数U V K,进行一次操作。 ## 输出格式 对于每个操作,输出结果。 ## 题目描述 You are given a tree with **N** nodes. The tree nodes are numbered from **1** to **N**. Each node has an integer weight. We will ask you to perform the following operation: - **u v k** : ask for the kth minimum weight on the path from node **u** to node **v** ## 输入格式 In the first line there are two integers **N** and **M**. (**N, M** <= 100000) In the second line there are **N** integers. The ith integer denotes the weight of the ith node. In the next **N-1** lines, each line contains two integers **u** **v**, which describes an edge (**u**, **v**). In the next **M** lines, each line contains three integers **u** **v** **k**, which means an operation asking for the kth minimum weight on the path from node **u** to node **v**. ## 输出格式 For each operation, print its result. ## 样例 #1 ### 样例输入 #1 ``` 8 5 105 2 9 3 8 5 7 7 1 2 1 3 1 4 3 5 3 6 3 7 4 8 2 5 1 2 5 2 2 5 3 2 5 4 7 8 2 ``` ### 样例输出 #1 ``` 2 8 9 105 7 ```
07-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值