bzoj 2588 Spoj 10628. Count on a tree(主席树)
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
【题意】
求u到v路径上的第k小的数。
【思路】
主席树,dfs序,lca,二分
主席树就是可持久化线段树,即保留历史版本的线段树。
为什么要保留历史版本呢?因为我们要完成对区间的查询,对于一个区间和的询问我们可以利用前缀和的思想来解决,主席树大概也是这个思路。
我们对每一个点建一棵线段树,每一棵线段树以权值为下标,维护区间中点的数目。依次建立每一棵线段树,第i棵线段树是从第i-1棵线段树上插了个权值i而得到的。这样对于区间[l,r]的查询,我们可以通过询问T[r]和T[l-1]的相应区间而得到。
对应到这道题中,我们先离散化权值(不离散化空间还不得飞起来233),然后按照树的形态来建立每一棵线段树,每一棵线段树就是从父节点对应的线段树上拓展来的,相当于上一个版本。这样从祖先到结点路径上的线段树就是逐渐增大且只包括路径上的权值的。
建好主席树后,对于查询(u,v),二分数的大小mid,我们就可以知道“区间”内所有小于mid的数目,为T[u].lc.sum-T[lca].lc.sum+T[v].lc.sum-T[fa[lca]].lc.sum (T[i]为i对应一棵线段树,lc为左儿子,sum为对应的权值数目),如果rank比之小则缩小数的区间,使u,v,lca,fa[lca]进入左儿子,否则修改rank后进入右儿子。
【代码】
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define FOR(a,b,c) for(int a=(b);a<=(c);a++)
using namespace std; typedef long long ll;
const int N = 1e5+;
const int M = *1e6+;
const int D = ; int n,m,tot,sz,ind;
int hash[N],pos[N],num[N],v[N],root[N];
int sum[M],ls[M],rs[M];
vector<int> g[N]; ll read() {
char c=getchar();
ll f=,x=;
while(!isdigit(c)) {
if(c=='-')f=-; c=getchar();
}
while(isdigit(c))
x=x*+c-'', c=getchar();
return x*f;
}
int fa[N],top[N],son[N],siz[N],dep[N];
void dfs1(int u)
{
siz[u]=; son[u]=; num[++ind]=u; pos[u]=ind;
for(int i=;i<g[u].size();i++) {
int v=g[u][i];
if(v!=fa[u]) {
fa[v]=u;
dep[v]=dep[u]+;
dfs1(v);
siz[u]+=siz[v];
if(siz[v]>siz[son[u]]) son[u]=v;
}
}
}
void dfs2(int u,int tp)
{
top[u]=tp;
if(son[u]) dfs2(son[u],tp);
for(int i=;i<g[u].size();i++) {
int v=g[u][i];
if(v!=fa[u] && v!=son[u]) dfs2(v,v);
}
}
int lca(int u,int v)
{
while(top[u]!=top[v]) {
if(dep[top[u]]<dep[top[v]]) swap(u,v);
u=fa[top[u]];
}
return dep[u]>dep[v]? v:u;
}
void update(int l,int r,int x,int &y,int num)
{
y=++sz;
sum[y]=sum[x]+;
if(l==r) return ;
ls[y]=ls[x] ; rs[y]=rs[x];
int mid=(l+r)>>;
if(num<=mid) update(l,mid,ls[x],ls[y],num);
else update(mid+,r,rs[x],rs[y],num);
}
int query(int x,int y,int rank)
{
int a=x,b=y,c=lca(x,y),d=fa[c];
a=root[pos[a]],b=root[pos[b]],c=root[pos[c]],d=root[pos[d]];
int l=,r=tot;
while(l<r) {
int mid=(l+r)>>;
int now=sum[ls[a]]+sum[ls[b]]-sum[ls[c]]-sum[ls[d]];
if(rank<=now) r=mid,a=ls[a],b=ls[b],c=ls[c],d=ls[d];
else l=mid+,rank-=now,a=rs[a],b=rs[b],c=rs[c],d=rs[d];
}
return hash[l];
} int main()
{
//freopen("in.in","r",stdin);
//freopen("out.out","w",stdout);
n=read(),m=read();
FOR(i,,n) {
v[i]=read(); hash[i]=v[i];
}
sort(hash+,hash+n+);
tot=;
FOR(i,,n) if(hash[i]!=hash[i-])
hash[++tot]=hash[i];
FOR(i,,n)
v[i]=lower_bound(hash+,hash+tot+,v[i])-hash;
int x,y,z;
FOR(i,,n-) {
x=read(),y=read();
g[x].push_back(y);
g[y].push_back(x);
}
dfs1(),dfs2(,);
FOR(i,,n) {
int t=num[i];
update(,tot,root[pos[fa[t]]],root[i],v[t]);
}
int lastans=;
FOR(i,,m) {
x=read(),y=read(),z=read();
x^=lastans;
printf("%d",lastans=query(x,y,z));
if(i!=m) puts("");
}
return ;
}
bzoj 2588 Spoj 10628. Count on a tree(主席树)的更多相关文章
- 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
分析:树上第k小,然后我想说的是主席树并不局限于线性表 详细分析请看http://www.cnblogs.com/rausen/p/4006116.html,讲的很好, 然后因为这个熟悉了主席树,真是 ...
- 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 ...
- 【BZOJ2588】Spoj 10628. Count on a tree 主席树+LCA
[BZOJ2588]Spoj 10628. Count on a tree Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lasta ...
- 主席树 || 可持久化线段树 || 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)感觉主席树真的好厉害...在原树上建主席树.即对于原 ...
随机推荐
- MySQL的基本命令
MySQL的基本命令 启动:net start mySql; 进入:mysql -u root -p/mysql -h localhost -u root -p databaseName; 列出数据库 ...
- js 判断文件是否存在(转载)
js 判断文件是否存在(转载) var fso,s=filespec; // filespec="C:/path/myfile.txt"fso=new ActiveXObject ...
- Hibernate关系级别注解
最近在学习Hibernate的相关知识,这一站学习的是Hibernate的注解相关的操作和知识.在这里标注以下为以后查阅和需要帮助的朋友提供便利. 一. 开发环境的搭建: 1. 需要的jar包配置: ...
- DJANGO和UIKIT结合,作一个有进度条的无刷新上传功能
以前作的上传,在糙了,所以在用户体验上改进一下. 同时,结合DJANGO作定位上传. 这其中分两步进行,第一次上传到TMP目录下, 第二次,将TMP下的文件转移到标准目录下. form.py file ...
- request重定向或者是response转发请求后面的代码依然执行
调用response.redirect(),或者request.getRequestDispatcher(loginAddr).forward(request,response);后,后面的代码照样执 ...
- UIActinSheet和UIActionSheetDelegate
UIActinSheet和UIActionSheetDelegate 这个是就那个UIActionSheet对象 一般用来选择类型或者改变界面...还有更多应用 定义如下:UIActionSheet ...
- HeadFirst设计模式之状态模式
一. 1. 2.The State Pattern allows an object to alter its behavior when its internal state changes. Th ...
- Nginx的反相代理, 负载均衡
转自 http://freeloda.blog.51cto.com/2033581/1288553 大纲 一.前言 二.环境准备 三.安装与配置Nginx 四.Nginx之反向代理 五.Nginx之负 ...
- POJ2993——Help Me with the Game(字符串处理+排序)
Help Me with the Game DescriptionYour task is to read a picture of a chessboard position and print i ...
- 企业级 Linux 安全管理实例(1)
公司企业多用Linux服务器,其中涉及到的一些安全管理对于安全运维人员来说是必不可少的应知技能, 以下案例沿着背景->需求->具体要求->操作步骤的流程进行描述,可以加深对安全管理的 ...