Count on a tree(bzoj 2588)
Description
Input
Output
M行,表示每个询问的答案。最后一个询问不输出换行符
Sample Input
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
8
9
105
7
/*
树上第K大
对于每一个节点,维护一棵权值线段树,记录它到根节点的状态,询问的时候,类似于区间第K大,这条链上的
总数就是sum[a]+sum[b]-sum[lca]-sum[fa[lca]]
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#define N 200010
using namespace std;
int a[N],b[N],val[N],n,m,len;
int head[N],fa[N][],dep[N],root[N],lc[N*],rc[N*],sum[N*],cnt;
struct node{int v,pre;}e[N*];
void add(int i,int u,int v){
e[i].v=v;
e[i].pre=head[u];
head[u]=i;
}
void pushup(int now){
sum[now]=sum[lc[now]]+sum[rc[now]];
}
void change(int last,int &now,int x,int l,int r){
now=++cnt;
if(l==r){
sum[now]=sum[last]+;
return;
}
int mid=l+r>>;
if(x<=mid) change(lc[last],lc[now],x,l,mid),rc[now]=rc[last];
else change(rc[last],rc[now],x,mid+,r),lc[now]=lc[last];
pushup(now);
}
void dfs(int x,int f,int c){
dep[x]=c;fa[x][]=f;change(root[f],root[x],a[x],,len);
for(int i=head[x];i;i=e[i].pre){
if(e[i].v==f) continue;
dfs(e[i].v,x,c+);
}
}
void get_fa(){
for(int j=;j<=;j++)
for(int i=;i<=n;i++)
fa[i][j]=fa[fa[i][j-]][j-];
}
int get_same(int u,int t){
for(int i=;i<=;i++)
if(t&(<<i))
u=fa[u][i];
return u;
}
int LCA(int u,int v){
if(dep[u]<dep[v]) swap(u,v);
u=get_same(u,dep[u]-dep[v]);
if(u==v) return u;
for(int i=;i>=;i--)
if(fa[u][i]!=fa[v][i])
u=fa[u][i],v=fa[v][i];
return fa[u][];
}
int query(int a,int b,int A,int B,int k,int l,int r){
int x=sum[lc[a]]+sum[lc[b]]-sum[lc[A]]-sum[lc[B]];
if(l==r) return l;
int mid=l+r>>;
if(k<=x) return query(lc[a],lc[b],lc[A],lc[B],k,l,mid);
else return query(rc[a],rc[b],rc[A],rc[B],k-x,mid+,r);
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b+,b+n+);
len=unique(b+,b+n+)-b-;
for(int i=;i<=n;i++){
int t=lower_bound(b+,b+len+,a[i])-b;
val[t]=a[i];a[i]=t;
}
for(int i=;i<n;i++){
int u,v;scanf("%d%d",&u,&v);
add(i*-,u,v);add(i*,v,u);
}
dfs(,,);get_fa();
int ans=;
for(int i=;i<=m;i++){
int u,v,k;scanf("%d%d%d",&u,&v,&k);u^=ans;
int anc=LCA(u,v);
ans=val[query(root[u],root[v],root[anc],root[fa[anc][]],k,,len)];
printf("%d",ans);
if(i!=m) printf("\n");
}
return ;
}
Count on a tree(bzoj 2588)的更多相关文章
- 【BZOJ2588】Count On a Tree(主席树)
[BZOJ2588]Count On a Tree(主席树) 题面 题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第 ...
- bzoj 2588 Spoj 10628. Count on a tree(主席树)
Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...
- hdu 4670 Cube number on a tree(点分治)
Cube number on a tree Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/ ...
- 04-树6 Complete Binary Search Tree(30 分)
title: 04-树6 Complete Binary Search Tree(30 分) date: 2017-11-12 14:20:46 tags: - 完全二叉树 - 二叉搜索树 categ ...
- P3690 【模板】Link Cut Tree (动态树)
P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...
- 1127 ZigZagging on a Tree (30 分)
1127 ZigZagging on a Tree (30 分) Suppose that all the keys in a binary tree are distinct positive in ...
- 【PAT】1043 Is It a Binary Search Tree(25 分)
1043 Is It a Binary Search Tree(25 分) A Binary Search Tree (BST) is recursively defined as a binary ...
- 1110 Complete Binary Tree (25 分)
1110 Complete Binary Tree (25 分) Given a tree, you are supposed to tell if it is a complete binary t ...
- 【POJ1741】Tree(点分治)
[POJ1741]Tree(点分治) 题面 Vjudge 题目大意: 求树中距离小于\(K\)的点对的数量 题解 完全不觉得点分治了.. 简直\(GG\),更别说动态点分治了... 于是来复习一下. ...
随机推荐
- C#中窗体边框隐藏
设置窗体属性 FormBorderStyle 为 None
- ERROR 2059 (HY000): Authentication plugin 'caching_sha2_password' cannot be loaded: /usr/lib64/mysql/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory
部署docker下的mysql时出现以下报错 [root@docker ~]# mysql -h192.168.30.22 -uroot -p Enter password: 出现报错: ERROR ...
- bootstrap 按钮组的嵌套
您可以在一个按钮组内嵌套另一个按钮组,即,在一个 .btn-group 内嵌套另一个 .btn-group .当您想让下拉菜单与一系列按钮组合使用时,就会用到这个. 实例: <!DOCTYPE ...
- Spring框架 全注解annotation不使用配置文件(SpringConfiguration.java类代替) 补充 xml配置文件没有提示解决
全注解不使用配置文件 首先还是倒包 在原有的jar包: 需Spring压缩包中的四个核心JAR包 beans .context.core 和expression 下载地址: https://pan.b ...
- C# 使用Epplus导出Excel [3]:合并列连续相同数据
C# 使用Epplus导出Excel [1]:导出固定列数据 C# 使用Epplus导出Excel [2]:导出动态列数据 C# 使用Epplus导出Excel [3]:合并列连续相同数据 C# 使用 ...
- 洛谷 P2921 在农场万圣节
https://www.luogu.org/problemnew/show/P2921 开始感觉这题30行代码就可以搞定,还是太菜啦,还是乖乖地写了tarjan. 对图进行缩点,那么这个强联通分量中的 ...
- 使用Redis作为高速缓存
Redis适合哪些业务场景常规业务系统的数据库访问中,读写操作的比例一般在7/3到9/1,也就是说读操作远多于写操作,因此高并发系统设计里,通过NoSQL技术将热点数据(短期内变动概率小的数据)放入内 ...
- Spring Boot -- Idea搭建下搭建web项目
最近公司准备使用Spring Boot框架,让小瑾先来学习一下,为了表示小瑾的办事效率,小瑾直接先学习用Idea搭建一个Spring Boot项目,哈哈哈,坐等领导夸. 废话不多说了,先来总结一下用I ...
- linux中的硬盘及flash操作
磁盘操作是块设备的必备操作,需要认真掌握. 一.硬盘 1.硬盘文件 默认串口硬盘的设备文件为sda(第一块硬盘).sdb(第二块硬盘).... 默认并口硬盘的设备文件为hda(第一块硬盘).hdb(第 ...
- progit 学习笔记-- 1 第一章 第二章
* 1 起步** 关于版本控制*** 什么是版本控制?记录文件变化,查阅特定版本,回溯到之前的状态.任何类型的文件进行版本控制.复制整个目录 加上备份时间 简单 混淆 无法恢复本地版本控制 数据库记 ...