SPOJ-COT-Count on a tree(树上路径第K小,可持久化线段树)
题意:
求树上A,B两点路径上第K小的数
分析:
同样是可持久化线段树,只是这一次我们用它来维护树上的信息。
我们之前已经知道,可持久化线段树实际上是维护的一个前缀和,而前缀和不一定要出现在一个线性表上。
比如说我们从一棵树的根节点进行DFS,得到根节点到各节点的距离dist[x]——这是一个根-x路径上点与根节点距离的前缀和。
利用这个前缀和,我们可以解决一些树上任意路径的问题,比如在线询问[a,b]点对的距离——答案自然是dist[a]+dist[b]-2*dist[lca(a,b)]。
同理,我们可以利用可持久化线段树来解决树上任意路径的问题。
DFS遍历整棵树,然后在每个节点上建立一棵线段树,某一棵线段树的“前一版本”是位于该节点父亲节点fa的线段树。
利用与之前类似的方法插入点权(排序离散)。那么对于询问[a,b],答案就是root[a]+root[b]-root[lca(a,b)]-root[fa[lca(a,b)]]上的第k大。
- // File Name: cot.cpp
- // Author: Zlbing
- // Created Time: 2013年10月09日 星期三 19时24分55秒
- #include<iostream>
- #include<string>
- #include<algorithm>
- #include<cstdlib>
- #include<cstdio>
- #include<set>
- #include<map>
- #include<vector>
- #include<cstring>
- #include<stack>
- #include<cmath>
- #include<queue>
- using namespace std;
- #define CL(x,v); memset(x,v,sizeof(x));
- #define INF 0x3f3f3f3f
- #define LL long long
- #define REP(i,r,n) for(int i=r;i<=n;i++)
- #define RREP(i,n,r) for(int i=n;i>=r;i--)
- const int MAXN=1e5+;
- const int POW=;
- int num[MAXN],hash[MAXN];
- int ls[MAXN*],rs[MAXN*];
- int sum[MAXN*];
- int root[MAXN];
- vector<int> G[MAXN];
- int d[MAXN];
- int p[MAXN][POW];
- int tot;
- int f[MAXN];
- void build(int l,int r,int& rt)
- {
- rt=++tot;
- sum[rt]=;
- if(l>=r)return;
- int m=(l+r)>>;
- build(l,m,ls[rt]);
- build(m+,r,rs[rt]);
- }
- void update(int last,int p,int l,int r,int &rt)
- {
- rt=++tot;
- ls[rt]=ls[last];
- rs[rt]=rs[last];
- sum[rt]=sum[last]+;
- if(l>=r)return ;
- int m=(l+r)>>;
- if(p<=m)update(ls[last],p,l,m,ls[rt]);
- else update(rs[last],p,m+,r,rs[rt]);
- }
- int query(int left_rt,int right_rt,int lca_rt,int lca_frt,int l,int r,int k)
- {
- if(l>=r)return l;
- int m=(l+r)>>;
- int cnt=sum[ls[right_rt]]+sum[ls[left_rt]]-sum[ls[lca_rt]]-sum[ls[lca_frt]];
- if(k<=cnt)
- return query(ls[left_rt],ls[right_rt],ls[lca_rt],ls[lca_frt],l,m,k);
- else
- return query(rs[left_rt],rs[right_rt],rs[lca_rt],rs[lca_frt],m+,r,k-cnt);
- }
- void dfs(int u,int fa,int cnt)
- {
- f[u]=fa;
- d[u]=d[fa]+;
- p[u][]=fa;
- for(int i=;i<POW;i++)p[u][i]=p[p[u][i-]][i-];
- update(root[fa],num[u],,cnt,root[u]);
- for(int i=;i<(int)G[u].size();i++)
- {
- int v=G[u][i];
- if(v==fa)continue;
- dfs(v,u,cnt);
- }
- }
- int lca(int a,int b)
- {
- if(d[a]>d[b])a^=b,b^=a,a^=b;
- if(d[a]<d[b])
- {
- int del=d[b]-d[a];
- for(int i=;i<POW;i++)
- if(del&(<<i))b=p[b][i];
- }
- if(a!=b)
- {
- for(int i=POW-;i>=;i--)
- {
- if(p[a][i]!=p[b][i])
- {
- a=p[a][i],b=p[b][i];
- }
- }
- a=p[a][],b=p[b][];
- }
- return a;
- }
- int main()
- {
- int n,m;
- while(~scanf("%d%d",&n,&m))
- {
- REP(i,,n)
- {
- G[i].clear();
- }
- CL(d,);
- CL(p,);
- CL(f,);
- REP(i,,n)
- {
- scanf("%d",&num[i]);
- hash[i]=num[i];
- }
- tot=;
- sort(hash+,hash++n);
- int cnt=unique(hash+,hash+n+)-hash-;
- REP(i,,n)
- {
- num[i]=lower_bound(hash+,hash+cnt+,num[i])-hash;
- }
- int a,b,c;
- REP(i,,n-)
- {
- scanf("%d%d",&a,&b);
- G[a].push_back(b);
- G[b].push_back(a);
- }
- build(,cnt,root[]);
- dfs(,,cnt);
- REP(i,,m)
- {
- scanf("%d%d%d",&a,&b,&c);
- int t=lca(a,b);
- int id=query(root[a],root[b],root[t],root[f[t]],,cnt,c);
- printf("%d\n",hash[id]);
- }
- }
- return ;
- }
SPOJ-COT-Count on a tree(树上路径第K小,可持久化线段树)的更多相关文章
- Count on a tree(树上路径第K小)
题目链接:https://www.spoj.com/problems/COT/en/ 题意:求树上A,B两点路径上第K小的数 思路:主席树实际上是维护的一个前缀和,而前缀和不一定要出现在一个线性表上. ...
- spoj COT - Count on a tree (树上第K小 LCA+主席树)
链接: https://www.spoj.com/problems/COT/en/ 思路: 首先看到求两点之前的第k小很容易想到用主席树去写,但是主席树处理的是线性结构,而这道题要求的是树形结构,我们 ...
- Count on a tree 树上区间第K小
Count on a tree 题意:求路径 u到v上的 第k小的权重. 题解:先DFS建数, 然后对于每个节点往上跑出一颗主席树, 然后每次更新. 查询的时候, u, v, k, 找到 z = l ...
- SPOJ COT Count on a tree(树上主席树 + LCA 求点第k小)题解
题意:n个点的树,每个点有权值,问你u~v路径第k小的点的权值是? 思路: 树上主席树就是每个点建一棵权值线段树,具体看JQ博客,LCA用倍增logn求出,具体原理看这里 树上主席树我每个点的存的是点 ...
- SPOJ - COT Count on a tree
地址:http://www.spoj.com/problems/COT/en/ 题目: COT - Count on a tree #tree You are given a tree with N ...
- SPOJ 10628 Count on a tree(Tarjan离线 | RMQ-ST在线求LCA+主席树求树上第K小)
COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to ...
- 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 ...
- spoj COT - Count on a tree(主席树 +lca,树上第K大)
您将获得一个包含N个节点的树.树节点的编号从1到Ñ.每个节点都有一个整数权重. 我们会要求您执行以下操作: uvk:询问从节点u到节点v的路径上的第k个最小权重 输入 在第一行中有两个整数Ñ和中号.( ...
随机推荐
- SharePoint Attachement操作代码
下载文件 如果下载其它类别的文件: SPSecurity.RunWithElevatedPrivileges(].ToString(); swi ...
- jquery自调用匿名函数解析
alert("undefined" in window); (function (window, undefined) { //构造jQuery ...
- 27、Jquery 事件
Jquery 事件 在javascript中事件调用方式为onclick.onmouseover等,在jquery中 使用事件无需写前面的on bind()方法 为元素绑定事件 $("#id ...
- final----这篇文章是我收获很大
final 用于声明属性.方法和类,分别表示属性不可变,方法不可重写,类不可继承. [转]Java final 修饰符知识点总结 final从字面上理解含义为“最后的,最终的”.在Java中也同样表示 ...
- google code 上传源码
在使用google code 的时候 做个备份, git clone https://wushuangzilong@code.google.com/p/maplebanana-proxy/ git c ...
- Object-C 类实现
这篇为Object-C添加方法的后续. 这里我们应该在类的实现(.m)文件中写 #import "Photo.h" @implementation Photo - (NSStrin ...
- iOS程序员的自我修养之道
新技术的了解渠道 WWDC开发者大会视频 官方文档 General -> Guides -> iOS x.x API Diffs 程序员的学习 iOS技术的学习 官当文档 Sample C ...
- window远程连接linux
一.字符界面连接Linux 1.直接使用window自带的telnet. 2.但现在Linux一般都不启用telnet,而是启用ssh.这样的话,window就要安装客户端来访问Linux了.这 ...
- COM简单应用示例
使用com技术开发模式进行的示例. com技术关键部分源码:主要将所有接口都写入到这个文件中 testinterface.h #ifndef TESTINTERFACE_H #define TESTI ...
- WF学习笔记(二)
-DoWhile循环:当[Condition]条件为真时会执行[Body]中的内容, 当[Condition]条件为假时会执行[Body]中的内容一次 -ForEach<T> 循环 :[V ...