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)感觉主席树真的好厉害...在原树上建主席树.即对于原 ...
随机推荐
- ArcGIS Server Identify结果属性 AliasName
最近做地图服务相关工作,一般在数据库中,字段名有好多限制,而实际工作中,需要显示的经常有一些较长的字段或者包含单位等特殊符号. 为了方便属性的操作,将属性字段名改为英文,AliasName中保存了属性 ...
- Elastic Search快速上手(2):将数据存入ES
前言 在上手使用前,需要先了解一些基本的概念. 推荐 可以到 https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.htm ...
- codeforce 849D. Make a Permutation!
D. Make a Permutation! time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- Linux入职基础-1.1_国内开源的主要镜像站
Linux入职基础-1.1_国内开源的主要镜像站 东北地区: 东北大学 http://mirror.neu.edu.cn 大连理工大学 http://mirror.dlut.edu.cn 大连东软 ...
- Python中函数的知识点
1.函数的定义与调用 2.函数的参数 3.函数的返回值 1. 1.1.函数的定义: 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 1.2.定义一个函数: 规则: 函数代码块以 ...
- 【转载】C#使用FirstOrDefault方法快速查找List集合中符合条件的第一个实体
在C#的List集合的操作中,有时候我们需要根据相关条件快速从List集合中获取到第一个符合条件的实体对象,例如有个全校班级的List集合,我们需要根据班级代码快速从List集合中查找出班级信息.可以 ...
- win10如何安装mariadb
一.下载.安装 1.下载mariadb(https://downloads.mariadb.org/),解压 2.进入bin目录下执行(管理员模型-powershell) .\mysqld.exe - ...
- U-Boot补丁 S3C2440
# tar xvf u-boot-1.1.6.tar.bz2 //解压 # cd u-boot-1.1.6/ 制作补丁文件 # diff -urN u-boot-1.1.6 u-boot-1.1.6. ...
- stm32 ADXL345传感器
加速度灵敏度轴 沿敏感轴加速时相应输出电压增加 寄存器映射 寄存器定义 0x31-DATA_FORMAT SELF_TEST位:设置为1,自测力应用至传感器,造成输出数据转换.值为0时,禁用自测力 S ...
- 【nodejs代理服务器三】nodejs注册windows服务
node-windows I no longer have enough time to properly maintain this project and am seeking a new pri ...