bzoj 2588 : Spoj 10628. Count on a tree
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
HINT
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid int m = (l + r) >> 1
const int M = 2e5 + ;
int p[M][],dep[M],head[M],sum[M*],ls[M*],rs[M*],root[M*];
int cnt1,n,idx,cnt,f[M]; struct node {
int to,next;
}e[M]; void add(int u,int v){
e[++cnt1].to=v;e[cnt1].next=head[u];head[u]=cnt1;
e[++cnt1].to=u;e[cnt1].next=head[v];head[v]=cnt1;
} int lca(int a,int b){
if(dep[a] > dep[b]) swap(a,b);
int h = dep[b] - dep[a];
for(int i = ;(<<i)<=h;i++){
if((<<i)&h) b = p[b][i];
} if(a!=b){
for(int i = ;i >= ;i --){
if(p[a][i]!=p[b][i]){
a = p[a][i]; b = p[b][i];
}
}
a = p[a][];
}
return a;
} void build(int l,int r,int &rt){
rt = ++idx;
sum[rt] = ;
if(l == r) return;
mid;
build(l,m,ls[rt]);
build(m+,r,rs[rt]);
} void update(int p,int l,int r,int old,int &rt){
rt = ++idx;
ls[rt] = ls[old]; rs[rt] = rs[old]; sum[rt] = sum[old] + ;
if(l == r) return ;
mid;
if(p <= m) update(p,l,m,ls[old],ls[rt]);
else update(p,m+,r,rs[old],rs[rt]);
} int query(int a,int b,int lc,int cl,int l,int r,int k){
if(l == r) return l;
mid;
int cnt = sum[ls[a]] + sum[ls[b]] - sum[ls[lc]] - sum[ls[cl]];
if(k <= cnt)
return query(ls[a],ls[b],ls[lc],ls[cl],l,m,k);
else
return query(rs[a],rs[b],rs[lc],rs[cl],m+,r,k-cnt);
}
int a[M],b[M]; void dfs(int u,int fa){
f[u] = fa;
update(a[u],,cnt,root[fa],root[u]);
for(int i = head[u];i;i = e[i].next){
int v = e[i].to;
if(v == fa) continue;
p[v][] = u;
dep[v] = dep[u] + ;
dfs(v,u);
}
} void init()
{
cnt1 = ; idx = ;
memset(head,,sizeof(head));
memset(dep,,sizeof(dep));
memset(p,,sizeof(p));
memset(f,,sizeof(f));
dep[] = ;
} int main()
{
int m;
while(scanf("%d%d",&n,&m)!=EOF){
init();
for(int i = ; i <= n;i ++){
scanf("%d",&a[i]);
b[i] = a[i];
}
int l,r,c;
sort(b+,b+n+);
cnt = unique(b+,b++n)-b-;
for(int i = ;i <= n;i ++)
a[i] = lower_bound(b+,b+cnt+,a[i]) - b;
for(int i = ;i <= n-;i ++){
scanf("%d%d",&l,&r);
add(l,r);
}
build(,cnt,root[]);
dfs(,);
for(int j = ;(<<j)<=n;j++)
for(int i = ;i <= n;i++)
p[i][j] = p[p[i][j-]][j-];
int num = ;
for(int i = ;i <= m;i ++){
scanf("%d%d%d",&l,&r,&c);
l = l^num;
int lc = lca(l,r);
int id = query(root[l],root[r],root[lc],root[f[lc]],,cnt,c);
num = b[id];
printf("%d\n",b[id]);
}
}
return ;
}
bzoj 2588 : Spoj 10628. Count on a tree的更多相关文章
- 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(树链剖分LCA+主席树)
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MB Description 给定一棵N个节点的树,每个点 ...
- 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)感觉主席树真的好厉害...在原树上建主席树.即对于原 ...
- 洛谷 2633 BZOJ 2588 Spoj 10628. Count on a tree
[题解] 蜜汁强制在线... 每个点开一个从它到根的可持久化权值线段树.查询的时候利用差分的思想在树上左右横跳就好了. #include<cstdio> #include<algor ...
随机推荐
- elasticsearch(6.2.3)安装Head插件
一.安装elasticsearch,参照:https://www.cnblogs.com/dyh004/p/8872443.html 二.安装nodejs,参照:https://www.runoob. ...
- centos7 安装phpmyadmin
1.先安装epel,不然安装pgpmyadmin时会出现找不到包. yum install epel-release rpm -ivh http://rpms.famillecollet.com/en ...
- Day15 Python基础之logging模块(十三)
参考源:http://www.cnblogs.com/yuanchenqi/articles/5732581.html logging模块 (****重点***) 一 (简单应用) import lo ...
- js手机短信验证
贴代码之前,我们先讲一下这里我们用到的技术主要有1个.setInterval(),这个方法可以实现倒计时的效果. css: .weui_btn_disabled.weui_btn_default { ...
- h5-canvas(其他api)
###1.使用图片(需要image对象) drawImage(image,x,y,width,height) 其中image是image或者canvas对象,x和y 是其在目标canvas的起始坐标 ...
- SQLServer数据库分页
以 项目表 PM_Project 为例. PM_Project 全部内容如下(共6条数据): 一.Top – Not In - Top 方式分页 直接的,原始的,不采用函数,纯手动挡. 分步探索过 ...
- java.util(Date和Calendar)
public class Date implements java.io.Serializable, Cloneable, Comparable<Date> { public Date() ...
- oracle建表流程
--创建表空间test1 create tablespace test1 datafile 'd:\test1.dbf' size 100m autoextend on next 10m --创建用户 ...
- spring AOP源码分析(二)
现在,我们将对代理对象的生成过程进行分析. 在springAOP源码分析(一)的例子中,将会生成哪些对象呢? 可以看到将会生成六个对象,对应的beanName分别是: userDao:目标对象 log ...
- VMWARE中NAT下获取不到IP
1.编辑-虚拟网络编辑器-dhcp设置 2.虚拟机-可移动设备-网络适配器-设置,注意:这里一定要选nat,当初我就是选了桥接,死活上不去,搞了2个小时.