BZOJ 2588: Spoj 10628. Count on a tree 主席树+lca
分析:树上第k小,然后我想说的是主席树并不局限于线性表
详细分析请看http://www.cnblogs.com/rausen/p/4006116.html,讲的很好,
然后因为这个熟悉了主席树,真是神器,强制在线,然后顺便学习了LCA倍增算法
LCA倍增算法是O(nlogn)预处理,然后O(logn)查询,其实和ST是一样的,但是好写啊
/**************************************************************
Problem: 2588
User: 96655
Language: C++
Result: Accepted
Time:4792 ms
Memory:47884 kb
****************************************************************/ #include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 1e5+;
const int INF=0x3f3f3f3f;
typedef unsigned long long ULL;
typedef long long LL;
int a[N],c[N],pos[N],n,q,cnt; int root[N],sz;
struct Node{
int l,r,v;
}o[N*]; void update(int &rt,int l,int r,int x){
o[++sz]=o[rt],rt=sz;
++o[rt].v;
if(l==r)return;
int m=(l+r)>>;
if(x<=m)update(o[rt].l,l,m,x);
else update(o[rt].r,m+,r,x);
} int d[N],fa[N][],head[N],tot;
struct Edge{
int v,next;
}edge[N<<];
void add(int u,int v){
edge[tot].v=v;
edge[tot].next=head[u];
head[u]=tot++;
}
void dfs(int u,int f){
fa[u][]=f;
d[u]=d[f]+;
update(root[u]=root[f],,cnt,pos[u]);
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].v;
if(v==f)continue;
dfs(v,u);
}
}
int lca(int u,int v){
if(d[u]<d[v])swap(u,v);
for(int t=d[u]-d[v],i=;t;t>>=,++i)
if(t&)u=fa[u][i];
if(u==v)return u;
for(int i=;i>=;--i){
if(fa[u][i]!=-&&fa[u][i]!=fa[v][i])
u=fa[u][i],v=fa[v][i];
}
return fa[u][];
}
int query(int u,int v,int k){
int k1=lca(u,v),k2=fa[k1][];
u=root[u],v=root[v],k1=root[k1],k2=root[k2];
int l=,r=cnt;
while(l!=r){
int m=(l+r)>>;
int tmp=o[o[u].l].v+o[o[v].l].v-o[o[k1].l].v-o[o[k2].l].v;
if(k<=tmp)r=m,u=o[u].l,v=o[v].l,k1=o[k1].l,k2=o[k2].l;
else l=m+,k-=tmp,u=o[u].r,v=o[v].r,k1=o[k1].r,k2=o[k2].r;
}
return a[l];
}
int main()
{
scanf("%d%d",&n,&q);
for(int i=;i<=n;++i)
scanf("%d",&a[i]),c[i]=a[i];
sort(a+,a+n+);
cnt=unique(a+,a++n)-a-;
for(int i=;i<=n;++i)
pos[i]=lower_bound(a+,a++cnt,c[i])-a;
tot=root[]=sz=;
memset(head,-,sizeof(head));
memset(fa,-,sizeof(fa));
for(int i=;i<n;++i){
int u,v;
scanf("%d%d",&u,&v);
add(u,v),add(v,u);
}
dfs(,);
for(int j=;(<<j)<=n;++j)
for(int i=;i<=n;++i)
if(fa[i][j-]!=-&&i!=&&j!=)
fa[i][j]=fa[fa[i][j-]][j-];
int lastans=;
while(q--){
int u,v,k;
scanf("%d%d%d",&u,&v,&k);
u^=lastans;
printf("%d",lastans=query(u,v,k));
if(q)printf("\n");
}
return ;
}
BZOJ 2588: Spoj 10628. Count on a tree 主席树+lca的更多相关文章
- 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 ...
- 【BZOJ2588】Spoj 10628. Count on a tree 主席树+LCA
[BZOJ2588]Spoj 10628. Count on a tree Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lasta ...
- 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 + 主席树 )
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
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2588 题解: 主席树,在线,(求LCA)感觉主席树真的好厉害...在原树上建主席树.即对于原 ...
随机推荐
- NOSQL之【Redis学习:配置说明】
# yes:后台运行:no:不是后台运行(老版本默认) daemonize yes # redis的进程文件 pidfile /var/run/redis.pid # 端口 port # bind_a ...
- Javascript 计算分页
var getPageData = function (last_page, current_page) { var numberLinks = []; var i = 0; for (i; i &l ...
- ecshop会员中心增加订单搜索功能
在user.php中的act=order_list中增加以下程序. $order_sn = isset($_REQUEST['order_sn'])?$_REQUEST['order_sn']:''; ...
- hdu 1695 GCD 莫比乌斯反演入门
GCD 题意:输入5个数a,b,c,d,k;(a = c = 1, 0 < b,d,k <= 100000);问有多少对a <= p <= b, c <= q <= ...
- poj 1818 ATP
ATP 题意:足球锦标赛使用二分的策略,每次淘汰剩下人的一半,并且数据表明:排名相差k(include)之内的运动员,胜负难料,否则排名前的必定战胜排名后的:问给定n(n = 2x, x∈N, n & ...
- 【方言】Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set 几种 方言配置差异 <?xml v ...
- listview滚动时背景闪烁,背景黑或白问题解决
android在使用listview时出现滚动时背景闪烁,变成背景黑或白的问题这样处理: 1:在布局文件中listview标签中加入: android:cacheColorHint="#00 ...
- ios短信和电话--参考
调用打电话功能 [[UIApplicationsharedApplication] openURL:[NSURL URLWithString:@"tel://10086"]]; 调 ...
- C++练习题
1. 用面向对象的程序描述员工拥有的股票,股票有公司,价格,数量属性,且拥有展现基本数据,更新价格,买进,卖出操作,并具有比较两个股票对象股值大小的比较方法. 2. 用面向对象的程序描述一个栈的操作, ...
- HAProxy 的负载均衡服务器,Redis 的缓存服务器
问答社区网络 StackExchange 由 100 多个网站构成,其中包括了 Alexa 排名第 54 的 StackOverflow.StackExchang 有 400 万用户,每月 5.6 亿 ...