BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]
2588: Spoj 10628. Count on a tree
Time Limit: 12 Sec Memory Limit: 128 MB
Submit: 5217 Solved: 1233
[Submit][Status][Discuss]
Description
Input
Output
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<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=1e5+,INF=1e9+;
int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int n,Q,u,v,k,last,mp[N],m,w[N];
struct ques{
int u,v,k;
}q[N];
int Bin(int v){
int l=,r=m;
while(l<=r){
int mid=(l+r)>>;
if(mp[mid]==v) return mid;
else if(v<mp[mid]) r=mid-;
else l=mid+;
}
return -;
}
struct edge{
int v,ne;
}e[N<<];
int h[N],cnt;
inline void ins(int u,int v){
cnt++;
e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt;
cnt++;
e[cnt].v=u;e[cnt].ne=h[v];h[v]=cnt;
}
int size[N],fa[N],deep[N],mx[N],top[N],tid[N],tot;
void dfs(int u){
size[u]=;
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
if(v==fa[u]) continue;
fa[v]=u;deep[v]=deep[u]+;
dfs(v);
size[u]+=size[v];
if(size[mx[u]]<size[v]) mx[u]=v;
}
}
void dfs(int u,int anc){
if(!u) return;
tid[u]=++tot;
top[u]=anc;
dfs(mx[u],anc);
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
if(v!=fa[u]&&v!=mx[u]) dfs(v,v);
}
}
int lca(int x,int y){
while(top[x]!=top[y]){
if(deep[top[x]]<deep[top[y]]) swap(x,y);
x=fa[top[x]];
}
if(tid[x]>tid[y]) swap(x,y);
return x;
} struct node{
int l,r,size;
}t[N*];
int sz,root[N];
void insert(int &x,int l,int r,int num){
t[++sz]=t[x];x=sz;
t[x].size++;
if(l==r) return;
int mid=(l+r)>>;
if(num<=mid) insert(t[x].l,l,mid,num);
else insert(t[x].r,mid+,r,num);
}
void build(int u){
root[u]=root[fa[u]];
insert(root[u],,m,Bin(w[u]));
if(mx[u]) build(mx[u]);
for(int i=h[u];i;i=e[i].ne)
if(e[i].v!=fa[u]&&e[i].v!=mx[u]) build(e[i].v);
}
inline int ls(int x){return t[t[x].l].size;}
int query(int u,int v,int k){
int p=lca(u,v),q=fa[p];
u=root[u];v=root[v];p=root[p];q=root[q];
int l=,r=m;
while(l!=r){
int mid=(l+r)>>,_=ls(u)+ls(v)-ls(p)-ls(q);
if(k<=_) r=mid,u=t[u].l,v=t[v].l,p=t[p].l,q=t[q].l;
else l=mid+,u=t[u].r,v=t[v].r,p=t[p].r,q=t[q].r,k-=_;
}
return l;
}
int main(){
n=read();Q=read();
for(int i=;i<=n;i++) mp[i]=w[i]=read();
for(int i=;i<=n-;i++) u=read(),v=read(),ins(u,v);
dfs();dfs(,);
for(int i=;i<=Q;i++) q[i].u=read(),q[i].v=read(),q[i].k=read();
sort(mp+,mp++n);
m=;
for(int i=;i<=n;i++) if(mp[i]!=mp[i-]) mp[++m]=mp[i]; build();
for(int i=;i<=Q;i++){
u=last^q[i].u;v=q[i].v;k=q[i].k;
last=mp[query(u,v,k)];
printf("%d",last);
if(i!=Q) putchar('\n');
}
}
BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]的更多相关文章
- BZOJ 2588: Spoj 10628. Count on a tree( LCA + 主席树 )
Orz..跑得还挺快的#10 自从会树链剖分后LCA就没写过倍增了... 这道题用可持久化线段树..点x的线段树表示ROOT到x的这条路径上的权值线段树 ----------------------- ...
- bzoj 2588 Spoj 10628. Count on a tree(主席树)
Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...
- bzoj 2588: Spoj 10628. Count on a tree【主席树+倍增】
算是板子,把值离散化,每个点到跟上做主席树,然后查询的时候主席树上用u+v-lca-fa[lca]的值二分 #include<iostream> #include<cstdio> ...
- 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+主席树)
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-可持久化线段树+LCA(点权)(树上的操作) 无语(为什么我的LCA的板子不对)
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 9280 Solved: 2421 ...
- 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) ...
随机推荐
- 持续集成:CruiseControl.NET + VisualSVN.Server
刚换了工作,有需要搭建一套持续集成的平台,做一下总结. 首先是我用到的工具: 上面缺少了Microsoft Fxcop,可以用来做代码校验,不过实际情况暂时还没有用到.主要的需求目前是,使用已发布的稳 ...
- 给ListView设置emptyView
给ListView设置emptyView 版权声明:本文为博主原创文章,未经博主允许不得转载. 使用ListView和GridView时,当列表为空时,默认是不显示任何内容的,这样对用户非常不友好,这 ...
- 计算机程序的思维逻辑 (40) - 剖析HashMap
前面两节介绍了ArrayList和LinkedList,它们的一个共同特点是,查找元素的效率都比较低,都需要逐个进行比较,本节介绍HashMap,它的查找效率则要高的多,HashMap是什么?怎么用? ...
- PHP之提取多维数组指定列的方法
前言:有时候在开发中会遇到这样的问题,我们需要把有规律的多维数组按照纵向(列)取出,有下面的方法可用: 我们将拿下面的数组来处理: $arr = array( '0' => array('id' ...
- 【中文分词】最大熵马尔可夫模型MEMM
Xue & Shen '2003 [2]用两种序列标注模型--MEMM (Maximum Entropy Markov Model)与CRF (Conditional Random Field ...
- 【那些年关于java多态应用】
1.多态:具有表现多种形态的能力的特征 父类: public abstract class Animal { public abstract void Say();} 子类: public class ...
- Asp.Net Core 项目实战之权限管理系统(1) 使用AdminLTE搭建前端
0 Asp.Net Core 项目实战之权限管理系统(0) 无中生有 1 Asp.Net Core 项目实战之权限管理系统(1) 使用AdminLTE搭建前端 2 Asp.Net Core 项目实战之 ...
- h1、h2、h3标签及strong标签对页面seo的影响
今天和大家来聊下h1,h2,h3,strong几个标签,在网页中的使用对页面seo的影响,也阐述了个人的一些想法. 首先简要讲下H标签及strong标签的含义:<h1>.<h2> ...
- C#开发微信门户及应用(6)--微信门户菜单的管理操作
前面几篇继续了我自己对于C#开发微信门户及应用的技术探索和相关的经验总结,继续探索微信API并分享相关的技术,一方面是为了和大家对这方面进行互动沟通,另一方面也是专心做好微信应用的底层技术开发,把基础 ...
- android
配置Activity 的启动模式: 在 AndroidManifest.xml 中配置: <activity android:name=".MainActivity" and ...