●BZOJ 2588 Spoj 10628. Count on a tree
题链:
http://www.lydsy.com/JudgeOnline/problem.php?id=2588
题解:
主席树,在线,(求LCA)
感觉主席树真的好厉害。。。
在原树上建主席树。
即对于原树的节点 u ,它所对应的线段树维护的是原树中它到根的路径上的点的权值信息。
即建主席树时, u 的线段树是由 fa[u] 的线段树而来的。
然后对于询问的两个点 a,b,
得到其 LCA,记为 c,并令 d = fa[c]
那么 a 到 b 路径上的信息即为
a 对应的线段树 + b 对应的线段树 - c 对应的线段树 - d 对应的线段树
所以在主席树中直接查询第 K 小就好了。
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 100500
#define filein(x) freopen(#x".in","r",stdin)
#define fileout(x) freopen(#x".out","w",stdout)
using namespace std;
int A[MAXN],tmp[MAXN],bgs[MAXN],top[MAXN],dep[MAXN],fa[MAXN];
int N,M,tnt;
struct Edge{
int to[MAXN*2],nxt[MAXN*2],head[MAXN],ent;
void Reset(){
ent=2;
memset(head,0,sizeof(head));
}
void Adde(int u,int v){
to[ent]=v; nxt[ent]=head[u]; head[u]=ent++;
to[ent]=u; nxt[ent]=head[v]; head[v]=ent++;
}
}E;
struct CMT{
int rt[MAXN],ls[MAXN*20],rs[MAXN*20],cnt[MAXN*20],sz;
void Insert(int &u,int l,int r,int p){
sz++; ls[sz]=ls[u]; rs[sz]=rs[u]; cnt[sz]=cnt[u];
u=sz; cnt[u]++; if(l==r) return;
int mid=(l+r)>>1;
if(p<=mid) Insert(ls[u],l,mid,p);
else Insert(rs[u],mid+1,r,p);
}
int Query(int a,int b,int c,int d,int l,int r,int K){
if(l==r) return l;
int mid=(l+r)>>1,lcnt=cnt[ls[a]]+cnt[ls[b]]-cnt[ls[c]]-cnt[ls[d]];
if(K<=lcnt) return Query(ls[a],ls[b],ls[c],ls[d],l,mid,K);
else return Query(rs[a],rs[b],rs[c],rs[d],mid+1,r,K-lcnt);
}
}DT;
void read(int &x){
static int f; static char ch;
x=0; f=1; ch=getchar();
while(ch<'0'||'9'<ch){if(ch=='-')f=-1;ch=getchar();}
while('0'<=ch&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
x=x*f;
}
void dfs1(int u,int dad){
static int num[MAXN];
DT.rt[u]=DT.rt[dad];
DT.Insert(DT.rt[u],1,tnt,A[u]);
num[u]=1; fa[u]=dad; int bgn=0;
for(int i=E.head[u],v;i;i=E.nxt[i]){
v=E.to[i]; if(v==dad) continue;
dfs1(v,u); num[u]+=num[v];
if(num[v]<=bgn) continue;
bgs[u]=v; bgn=num[v];
}
}
void dfs2(int u,int tp){
top[u]=tp; dep[u]=dep[fa[u]]+1;
if(bgs[u]) dfs2(bgs[u],tp);
for(int i=E.head[u],v;i;i=E.nxt[i]){
v=E.to[i]; if(v==fa[u]||v==bgs[u]) continue;
dfs2(v,v);
}
}
int lca(int u,int v){
while(top[u]!=top[v]){
if(dep[top[u]]>dep[top[v]]) swap(u,v);
v=fa[top[v]];
}
if(dep[u]>dep[v]) swap(u,v);
return u;
}
void solve(){
int lastANS=0,a,b,c,d,rta,rtb,rtc,rtd,K,p;
for(int i=1;i<=M;i++){
read(a); read(b); read(K);
a^=lastANS; c=lca(a,b); d=fa[c];
rta=DT.rt[a]; rtb=DT.rt[b];
rtc=DT.rt[c]; rtd=DT.rt[d];
p=DT.Query(rta,rtb,rtc,rtd,1,tnt,K);
lastANS=tmp[p];
printf("%d",lastANS);
if(i!=M) printf("\n");
}
}
int main(){
E.Reset();
read(N); read(M);
for(int i=1;i<=N;i++)
read(A[i]),tmp[i]=A[i];
sort(tmp+1,tmp+N+1);
tnt=unique(tmp+1,tmp+N+1)-tmp-1;
for(int i=1;i<=N;i++)
A[i]=lower_bound(tmp+1,tmp+tnt+1,A[i])-tmp;
for(int i=1,u,v;i<N;i++)
scanf("%d%d",&u,&v),E.Adde(u,v);
dfs1(1,0);
dfs2(1,1);
solve();
return 0;
}
●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,讲的很好, 然后因为这个熟悉了主席树,真是 ...
- 洛谷 2633 BZOJ 2588 Spoj 10628. Count on a tree
[题解] 蜜汁强制在线... 每个点开一个从它到根的可持久化权值线段树.查询的时候利用差分的思想在树上左右横跳就好了. #include<cstdio> #include<algor ...
随机推荐
- 关于python中的operator.itemgetter()函数的用法
1. operator.itemgetter(num)函数 表示对对象的第num维数据进行操作获取. >>>import operator >>>a = [1, 2 ...
- 201621123057 《Java程序设计》第10周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容 2. 书面作业 本次PTA作业题集异常 1. 常用异常 结合题集题目7-1回答 1.1 自己以前编写的代码中经常出现什 ...
- 201421123042 《Java程序设计》第8周学习总结
1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 2. 书面作业 1. ArrayList代码分析 1.1 解释ArrayList的contains源代码 源代码: 答:查找 ...
- 解决background图片拉伸问题
ImageView中XML属性src和background的区别: background会根据ImageView组件给定的长宽进行拉伸,而src就存放的是原图的大小,不会进行拉伸.src是图片内容(前 ...
- eclipse maven项目目录
今天遇见一个错误,关于eclipse项目的路径问题,web-inf的路径,上图和下图出现了两种web-INF,src的web-INFf和webContent的web-INF,src里面的文件需要编译以 ...
- 剑指offer-二叉树中和为某一值的路径
题目描述 输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径. 解题思路 利用前序遍历的思想,定义FindP ...
- Python内置函数(32)——all
英文文档: all(iterable) Return True if all elements of the iterable are true (or if the iterable is empt ...
- Apollo单向SSL认证(1)
参考链接:https://www.cnblogs.com/benwu/articles/4891758.html keytool -genkey -alias mybroker -keyalg RSA ...
- 英语日常词汇:living-room、dining-room vs dining hall
hall 大厅 : living room起居室,客厅 : dining room餐厅.饭厅 dining room是家里的客厅,比较小啊,dining hall一般指酒店或宾馆啊什么的的大厅,宴客厅
- kubernetes进阶(02)kubernetes的node
一.Node概念 Node是Pod真正运行的主机,可以物理机,也可以是虚拟机. 为了管理Pod,每个Node节点上至少要运行container runtime(比如docker或者rkt). kube ...