Spoj 10628. Count on a tree 树上主席树

给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权。其中lastans是上一个询问的答案,初始为0,即第一个询问的u是明文。

Input
第一行两个整数N,M。
第二行有N个整数,其中第i个整数表示点i的权值。
后面N-1行每行两个整数(x,y),表示点x到点y有一条边。
最后M行每行两个整数(u,v,k),表示一组询问。

Output
M行,表示每个询问的答案。最后一个询问不输出换行符

Sample Input
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
0 5 2
10 5 3
11 5 4
110 8 2
Sample Output
2
8
9
105
7
Hint

HINT:

N,M<=100000

暴力自重。。。

树上建主席树,得到一条u,v的链的sum是 sum[x]+sum[y]-sum[lca(x,y)]-sum(fa[lca(x,y)][0]) 因为 公共祖先以上都会重复两遍,只保留一个公共祖先和链就好,改了好久,成自己板子了,终于过了 傻逼oj的re了 开心啊开心,旧板子换成新的了,这个新板子非常好用。

#include <bits/stdc++.h>
using namespace std;

const int maxn=1e5+5;
int n,m;
int v[maxn],b[maxn],tsize,tot=0,root[maxn],dep[maxn];

int ls[maxn*30],rs[maxn*30],sum[maxn*30];

vector<int> w[maxn];
int fa[maxn][20];
int build(int l,int r)
{
    int rot=++tot;
    sum[rot]=0;
    if(l==r)
        return rot;
    int mid=(l+r)>>1;
    ls[rot]=build(l,mid);
    rs[rot]=build(mid+1,r);
    return rot;
}

void hash1()
{
    sort(b+1,b+1+n);
    tsize=unique(b+1,b+1+n)-b-1;
}
int find_index(int x)
{
    return lower_bound(b+1,b+1+tsize,x)-b;
}

int update(int rot,int x)
{
    int now=++tot;
    int tmp=now;
    sum[now]=sum[rot]+1;
    int l=1,r=tsize;
    while(l<r)
    {
        int mid=(l+r)>>1;
        if(x<=mid)
        {
            ls[now]=++tot;
            rs[now]=rs[rot];
            now=ls[now];
            r=mid;
            rot=ls[rot];
        }
        else
        {
            ls[now]=ls[rot];
            rs[now]=++tot;
            now=rs[now];
            l=mid+1;
            rot=rs[rot];
        }
        sum[now]=sum[rot]+1;
    }
    return tmp;
}

void dfs(int x,int y)
{
    root[x]=update(root[y],find_index(v[x]));
    dep[x]=dep[y]+1;
    for(int i=0;i<w[x].size();i++)
    {
        if(w[x][i]==y) continue;
        int vv=w[x][i];
        fa[vv][0]=x;
         for(int j=1;fa[fa[vv][j-1]][j-1];j++)
            fa[vv][j]=fa[fa[vv][j-1]][j-1];
        dfs(vv,x);
    }
}

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];
}


int ask(int x,int y,int k)
{
    int tt=lca(x,y);
    int a=root[x],bb=root[y],c=root[tt],d=root[fa[tt][0]];
    int l=1,r=tsize;
    while(l<r)
    {
        int mid=(l+r)>>1;
        int ss=sum[ls[a]]+sum[ls[bb]]-sum[ls[c]]-sum[ls[d]];
        if(ss>=k)
        {
            r=mid;
            a=ls[a];
            bb=ls[bb];
            c=ls[c];
            d=ls[d];
        }
        else
        {
            l=mid+1;
            k-=ss;
            a=rs[a];
            bb=rs[bb];
            c=rs[c];
            d=rs[d];
        }
    }
    return l;
}

int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        scanf("%d",&v[i]),b[i]=v[i];
    int l,r,k;
    for(int i=1;i<n;i++)
    {
        scanf("%d%d",&l,&r);
        w[l].push_back(r);
        w[r].push_back(l);
    }

    hash1();
    root[0]=build(1,tsize);
    dep[0]=0;
    dfs(1,0);
    while(m--)
    {
        scanf("%d%d%d",&l,&r,&k);
        printf("%d\n",b[ask(l,r,k)] );
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值