2588: Spoj 10628. Count on a tree
2588: Spoj 10628. Count on a tree
Time Limit: 12 Sec  Memory Limit: 128 MB
Submit: 5766  Solved: 1374
[Submit][Status][Discuss]
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
Source
/*
树上第K大.
对于每一个节点以key(权值)为下标
用一颗权值线段树维护它到根的路径的区间K大值.
然后用主席树维护.
树剖求lca.
*/
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=1e5+,M=N*;
struct edge{int v,next;}e[N<<];int tot,head[N];
int n,m,sz,a[N],s[N],son[N],top[N],siz[N],fa[N],dep[N];
int root[N],sum[M],ls[M],rs[M];
int ans;
inline int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
void add(int x,int y){
e[++tot].v=y;e[tot].next=head[x];head[x]=tot;
e[++tot].v=x;e[tot].next=head[y];head[y]=tot;
}
//=========================================预处理
void insert(int &k,int last,int l,int r,int p){
k=++sz;
sum[k]=sum[last]+;
if(l==r) return ;
ls[k]=ls[last];
rs[k]=rs[last];
int mid=l+r>>;
if(p<=mid) insert(ls[k],ls[last],l,mid,p);
else insert(rs[k],rs[last],mid+,r,p);
}
int query(int l,int r,int x1,int x2,int x3,int x4,int K){
if(l==r) return l;
int mid=l+r>>;
int cnt=sum[ls[x1]]+sum[ls[x2]]-sum[ls[x3]]-sum[ls[x4]];
if(K<=cnt) return query(l,mid,ls[x1],ls[x2],ls[x3],ls[x4],K);
else return query(mid+,r,rs[x1],rs[x2],rs[x3],rs[x4],K-cnt);
}
//=========================================主席树
void dfs(int x,int f,int de){
fa[x]=f;dep[x]=de;siz[x]=;
int p=lower_bound(s+,s+n+,a[x])-s;
insert(root[x],root[f],,n,p);
for(int i=head[x];i;i=e[i].next){
if(e[i].v!=f){
dfs(e[i].v,x,de+);
siz[x]+=siz[e[i].v];
if(siz[son[x]]<siz[e[i].v]) son[x]=e[i].v;
}
}
}
void getpos(int x,int tp){
top[x]=tp;//pos[x]=++dfs_cnt;
if(!son[x]) return ;
getpos(son[x],tp);
for(int i=head[x];i;i=e[i].next){
if(e[i].v!=fa[x]&&e[i].v!=son[x]){
getpos(e[i].v,e[i].v);
}
}
}
inline int lca(int x,int y){
fa[]=;//求lca,fa[1]必须=1,否则就GG了
for(;top[x]!=top[y];x=fa[top[x]]){
if(dep[top[x]]<dep[top[y]]) swap(x,y);
}
fa[]=;//当anc=1时,防止query(...root[anc],root[fa[anc]])出问题
return dep[x]<dep[y]?x:y;
}
//=========================================树链剖分
void Q_ans(int x,int y,int K){
int anc=lca(x,y);//类比区间[l,r]
ans=query(,n,root[x],root[y],root[anc],root[fa[anc]],K);
printf("%d",ans=s[ans]);
}
int main(){
n=read();m=read();
for(int i=;i<=n;i++) s[i]=a[i]=read();
for(int i=,x,y;i<n;i++) x=read(),y=read(),add(x,y);
sort(s+,s+n+);
dfs(,,);getpos(,);
for(int i=,x,y,z;i<=m;i++){
x=read()^ans;y=read();z=read();
Q_ans(x,y,z);
if(i!=m) printf("\n");
}
return ;
}
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)感觉主席树真的好厉害...在原树上建主席树.即对于原 ...
 
随机推荐
- 【UVA】11468-Substring(AC自己主动机)
			
AC自己主动机的题,须要注意的,建立失配边的时候,假设结点1失配边连到的那个结点2,那个结点2是一个单词的结尾,那么这个结点1也须要标记成1(由于能够看成,这个结点包括了这个单词),之后在Trie树上 ...
 - Java 8 List
			
排序 依据自定义对象的某个属性进行排序. List<Student> students = Arrays.asList(student1, student2, student3); stu ...
 - 【Java】Java_01初步
			
1.编程语言的发展史和发展主线 计算机语言如果你将它当做一个产品,就像我们平时用的电视机.剃须刀.电脑.手机等, 他的发展也是有规律的. 任何一个产品的发展规律都是:向着人更加容易使用.功能越来越强大 ...
 - STL坑汇总
			
1. Q:vector的push_back()方法到底做了些什么? 为什么声明写的是void push_back (const value_type& val); A:的确,乍一看,似乎pus ...
 - oc block 遍历数组及字典
			
原遍历数组NSArray * lines = ...for (NSString * line in lines) { // ...}for (int i = 0; i < lines.count ...
 - linux 设置tomcat快捷启动方式
			
在linux下搭建好tomcat之后,每次启动和关闭都要去tomcat的bin目录下执行./startup.sh和./shutdown.sh 这是很不方便的,下面介绍如何像执行ls mv cp等命令一 ...
 - Linux如何关机与关机命令祥解
			
Linux关机命令祥解 1.直接关电源 2.init 0 3.telinit 0 4.shutdown -h now 5.halt6.poweroff 1.shutdown shutdown命令安全地 ...
 - java 中 HashMap 遍历与删除
			
HashMap的遍历 方法一.这是最常见的并且在大多数情况下也是最可取的遍历方式 /** * 在键值都需要时使用 */ Map<Integer, Integer> map = new Ha ...
 - c++ simple class template example: Stack
			
main.cpp #include "Stack.h" #include <iostream> using namespace std; class Box { pub ...
 - iOS 如何缩小打包项目ipa大小
			
之前项目上线完全由技术老大搞,这次独立开发自己来,觉得自己的打包项目体积略大,网上搜索了一些比较不错的方法,这里总结下 1.配置编译选项 (Levels选项内)Genetate Debug Symbo ...