Bzoj 2588 Spoj 10628. Count on a tree(树链剖分LCA+主席树)
2588: Spoj 10628. Count on a tree
Time Limit: 12 Sec Memory Limit: 128 MB
Description
给定一棵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:
N,M<=100000
暴力自重。。。
Source
鸣谢seter
/*
树上第K大.
对于每一个节点
以key为下标
用一颗权值线段树维护它到根的路径的区间K大值.
然后用主席树维护.
树剖求lca.
ans=tree[x].size+tree[y].size-tree[lca].size-tree[lca_father].size.
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#define MAXN 100001
using namespace std;
int n,m,ans,cut,maxsize,root[MAXN],tot,s[MAXN],a[MAXN],head[MAXN];
int fa[MAXN],pos[MAXN],top[MAXN],deep[MAXN],size[MAXN];
struct data{int lc,rc,size;}tree[MAXN*20];
struct node{int v,next;}e[MAXN*2];
int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
return x*f;
}
void add(int u,int v)
{
e[++cut].v=v;e[cut].next=head[u];head[u]=cut;
}
void add(int &k,int last,int l,int r,int x)
{
k=++tot;tree[k].lc=tree[last].lc,tree[k].rc=tree[last].rc;
tree[k].size=tree[last].size+1;
if(l==r) return ;
int mid=(l+r)>>1;
if(x<=mid) add(tree[k].lc,tree[last].lc,l,mid,x);
else add(tree[k].rc,tree[last].rc,mid+1,r,x);
return ;
}
void dfs1(int u,int f)
{
size[u]=1;
int p=lower_bound(s+1,s+n+1,a[u])-s;
add(root[u],root[f],1,n,p);
for(int i=head[u];i;i=e[i].next)
{
int v=e[i].v;
if(v!=f) fa[v]=u,deep[v]=deep[u]+1,dfs1(v,u),size[u]+=size[v];
}
return ;
}
void dfs2(int u,int top1)
{
int k=0;pos[u]=++maxsize;top[u]=top1;
for(int i=head[u];i;i=e[i].next)
{
int v=e[i].v;
if(fa[v]==u&&size[v]>size[k]) k=v;
}
if(!k) return ;
dfs2(k,top1);
for(int i=head[u];i;i=e[i].next)
{
int v=e[i].v;
if(fa[v]==u&&v!=k) dfs2(v,v);
}
return ;
}
int lca(int x,int y)
{
fa[1]=1;
while(top[x]!=top[y])
{
if(deep[top[x]]<deep[top[y]]) swap(x,y);//1 R.
x=fa[top[x]];
}
fa[1]=0;
if(pos[x]>pos[y]) swap(x,y);
return x;
}
int query(int L,int R,int lc,int falc,int l,int r,int k)
{
if(l==r) return l;
int mid=(l+r)>>1;
int sum=tree[tree[L].lc].size+tree[tree[R].lc].size-
tree[tree[lc].lc].size-tree[tree[falc].lc].size;
if(sum>=k)
return query(tree[L].lc,tree[R].lc,tree[lc].lc,tree[falc].lc,l,mid,k);
else return query(tree[L].rc,tree[R].rc,tree[lc].rc,tree[falc].rc,mid+1,r,k-sum);
}
void slovequery(int x,int y,int k)
{
int lc=lca(x,y);
ans=query(root[x],root[y],root[lc],root[fa[lc]],1,n,k);
printf("%d",ans=s[ans]);return ;
}
int main()
{
int x,y,z;
n=read(),m=read();
for(int i=1;i<=n;i++) s[i]=a[i]=read();
for(int i=1;i<=n-1;i++) x=read(),y=read(),add(x,y),add(y,x);
sort(s+1,s+n+1);dfs1(1,0),dfs2(1,1),fa[1]=0;
while(m!=1)
{
x=read(),y=read(),z=read();x^=ans;
slovequery(x,y,z);
m--;printf("\n");//2 W.
}
x=read(),y=read(),z=read();x^=ans;
slovequery(x,y,z);
return 0;
}
Bzoj 2588 Spoj 10628. Count on a tree(树链剖分LCA+主席树)的更多相关文章
- SPOJ 10628 Count on a tree(Tarjan离线LCA+主席树求树上第K小)
COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to ...
- BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5217 Solved: 1233 ...
- BZOJ 2588: Spoj 10628. Count on a tree 树上跑主席树
2588: Spoj 10628. Count on a tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/J ...
- Bzoj 2588: Spoj 10628. Count on a tree 主席树,离散化,可持久,倍增LCA
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2588 2588: Spoj 10628. Count on a tree Time Limit ...
- BZOJ 2588: Spoj 10628. Count on a tree( LCA + 主席树 )
Orz..跑得还挺快的#10 自从会树链剖分后LCA就没写过倍增了... 这道题用可持久化线段树..点x的线段树表示ROOT到x的这条路径上的权值线段树 ----------------------- ...
- bzoj 2588 Spoj 10628. Count on a tree (可持久化线段树)
Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 7669 Solved: 1894[Submi ...
- 主席树 || 可持久化线段树 || LCA || BZOJ 2588: Spoj 10628. Count on a tree || Luogu P2633 Count on a tree
题面: Count on a tree 题解: 主席树维护每个节点到根节点的权值出现次数,大体和主席树典型做法差不多,对于询问(X,Y),答案要计算ans(X)+ans(Y)-ans(LCA(X,Y) ...
- BZOJ 2588: Spoj 10628. Count on a tree 主席树+lca
分析:树上第k小,然后我想说的是主席树并不局限于线性表 详细分析请看http://www.cnblogs.com/rausen/p/4006116.html,讲的很好, 然后因为这个熟悉了主席树,真是 ...
- ●BZOJ 2588 Spoj 10628. Count on a tree
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2588 题解: 主席树,在线,(求LCA)感觉主席树真的好厉害...在原树上建主席树.即对于原 ...
随机推荐
- jquery封装的方法
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- Springboot对JPA的支持及使用
目的: 1.springboot之jpa支持 2.Springboot+bootstrap界面版之增删改查及图片上传 springboot之jpa支持 导入相关pom依赖 <dependency ...
- List、dictionary、hashtable、ArrayList集合
集合的引用命名空间在 system.Collections下 1.为什么引入集合 因为数组长度是固定的,为了建立一个动态的"数组",所以引入了集合. 2.为什么引入ArrayLis ...
- springboot mvc自动配置(目录)
对于长时间基于spring框架做web开发的我们,springmvc几乎成为了开发普通web项目的标配.本系列文章基于快速启动的springboot,将从源码角度一点点了解springboot中mvc ...
- 'vue' 不是内部或外部命令
运用cnpm淘宝镜像安装vue-cli,然后输入vue,显示“'vue' 不是内部或外部命令”,然后百度查找方法,解决办法如下: 虽然电脑是64位的电脑,然后node我也下载安装的是64位,然后,我重 ...
- iOS应用开发---返回到指定界面
关于ios中 viewcontroller的跳转问题,其中有一种方式是采用navigationController pushViewController 的方法,比如我从主页面跳转到了一级页面,又从一 ...
- 基本代码、插值表达式、v-cloak
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- git相关的一篇不错的文章
原文地址:http://josh-persistence.iteye.com/blog/2215214 点击进入
- 【OF框架】框架规范介绍
一.目录规范 二.命名规范 三.其它规范
- 商汤开源的mmdetection技术报告
目录 1. 简介 2. 支持的算法 3. 框架与架构 6. 相关链接 前言:让我惊艳的几个库: ultralytics的yolov3,在一众yolov3的pytorch版本实现算法中脱颖而出,收到开发 ...