SPOJ - COT Count on a tree
地址:http://www.spoj.com/problems/COT/en/
题目:
COT - Count on a tree
You are given a tree with N nodes.The tree nodes are numbered from 1 to N.Each node has an integer weight.
We will ask you to perform the following operation:
- u v k : ask for the kth minimum weight on the path from node u to node v
Input
In the first line there are two integers N and M.(N,M<=100000)
In the second line there are N integers.The ith integer denotes the weight of the ith node.
In the next N-1 lines,each line contains two integers u v,which describes an edge (u,v).
In the next M lines,each line contains three integers u v k,which means an operation asking for the kth minimum weight on the path from node u to node v.
Output
For each operation,print its result.
Example
Input:
8 5
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
2 5 2
2 5 3
2 5 4
7 8 2
Output:
2
8
9
105
7 思路: 这是一道求树上两点uv路径上的第k大权值的题。
求第k大很容易想到主席树,但是以前的主席树的都是线性的,这题却是树型的。
其实只需要把建树的方式改成根据父亲节点建树即可,查询时:sum[u]+sum[v]-sum[lca(u,v)]-sum[fa[lca(u,v)]]。
这还是一道模板题。
#include <bits/stdc++.h> using namespace std; #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=2e6+;
const int mod=1e9+; int tot,ls[K],rs[K],rt[K],sum[K];
int v[K],b[K],up[K][],deep[K],fa[K];
vector<int>mp[K];
//sum[o]记录的是该节点区间内出现的数的个数
//区间指的是将数离散化后的区间
void build(int &o,int l,int r)
{
o=++tot,sum[o]=;
int mid=l+r>>;
if(l!=r)
build(ls[o],l,mid),build(rs[o],mid+,r);
}
void update(int &o,int l,int r,int last,int x)
{
o=++tot,sum[o]=sum[last]+;
ls[o]=ls[last],rs[o]=rs[last];
if(l==r) return ;
int mid=l+r>>;
if(x<=mid) update(ls[o],l,mid,ls[last],x);
else update(rs[o],mid+,r,rs[last],x);
}
int query(int ra,int rb,int rc,int rd,int l,int r,int k)
{
if(l==r) return b[l];
int cnt=sum[ls[ra]]+sum[ls[rb]]-sum[ls[rc]]-sum[ls[rd]],mid=l+r>>;
if(k<=cnt) return query(ls[ra],ls[rb],ls[rc],ls[rd],l,mid,k);
return query(rs[ra],rs[rb],rs[rc],rs[rd],mid+,r,k-cnt);
}
void dfs(int x,int f,int sz)
{
update(rt[x],,sz,rt[f],v[x]);
up[x][]=f,deep[x]=deep[f]+,fa[x]=f;
for(int i=;i<=;i++) up[x][i]=up[up[x][i-]][i-];
for(int i=;i<mp[x].size();i++)
if(mp[x][i]!=f)
dfs(mp[x][i],x,sz);
}
int lca(int x,int y)
{
if(deep[x]<deep[y]) swap(x,y);
for(int i=;i>=;i--) if( deep[up[x][i]]>=deep[y]) x=up[x][i];
if(x==y) return x;
for(int i=;i>=;i--) if(up[x][i]!=up[y][i]) x=up[x][i],y=up[y][i];
return up[x][];
}
int main(void)
{
tot=;
int n,m,sz;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%d",v+i),b[i]=v[i];
for(int i=,x,y;i<n;i++)
scanf("%d%d",&x,&y),mp[x].PB(y),mp[y].PB(x);
sort(b+,b++n);
sz=unique(b+,b++n)-b-;
for(int i=;i<=n;i++)
v[i]=lower_bound(b+,b++sz,v[i])-b;
build(rt[],,sz);
dfs(,,sz);
int x,y,k;
while(m--)
scanf("%d%d%d",&x,&y,&k),printf("%d\n",query(rt[x],rt[y],rt[lca(x,y)],rt[fa[lca(x,y)]],,sz,k));
return ;
}
SPOJ - COT Count on a tree的更多相关文章
- spoj cot: Count on a tree 主席树
10628. Count on a tree Problem code: COT You are given a tree with N nodes.The tree nodes are number ...
- spoj COT - Count on a tree (树上第K小 LCA+主席树)
链接: https://www.spoj.com/problems/COT/en/ 思路: 首先看到求两点之前的第k小很容易想到用主席树去写,但是主席树处理的是线性结构,而这道题要求的是树形结构,我们 ...
- SPOJ 10628. SPOJ COT Count on a tree 可持久化线段树
这题是裸的主席树,每个节点建一棵主席树,再加个lca就可以了. 历尽艰辛,终于A掉了这一题,这般艰辛也显示出了打代码的不熟练. 错误:1.lca倍增的时候i和j写反了,RE了5次,实在要吸取教训 2. ...
- SPOJ COT Count on a tree(树上主席树 + LCA 求点第k小)题解
题意:n个点的树,每个点有权值,问你u~v路径第k小的点的权值是? 思路: 树上主席树就是每个点建一棵权值线段树,具体看JQ博客,LCA用倍增logn求出,具体原理看这里 树上主席树我每个点的存的是点 ...
- spoj COT - Count on a tree(主席树 +lca,树上第K大)
您将获得一个包含N个节点的树.树节点的编号从1到Ñ.每个节点都有一个整数权重. 我们会要求您执行以下操作: uvk:询问从节点u到节点v的路径上的第k个最小权重 输入 在第一行中有两个整数Ñ和中号.( ...
- SPOJ 10628 COT - Count on a tree(在树上建立主席树)(LCA)
COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to ...
- SPOJ 10628 Count on a tree(Tarjan离线LCA+主席树求树上第K小)
COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to ...
- 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 ...
随机推荐
- Spring_day02--log4j介绍_Spring整合web项目演示
log4j介绍 1 通过log4j可以看到程序运行过程中更详细的信息 (1)经常使用log4j查看日志 2 使用 (1)导入log4j的jar包 (2)复制log4j的配置文件,复制到src下面 3 ...
- JZOJ.5306【NOIP2017模拟8.18】棋盘游戏
Description 这个游戏上在一个无限大的棋盘上, 棋盘上只有一颗棋子在位置(x,y)(x,y>=0)棋盘的左下角是(0,0)Amphetamine每次都是第一个移动棋子,然后Amphet ...
- linux安装yaf(ubuntu教程)
$PHP_BIN/phpize 这个是什么 其实这个是一个扩展 apt-get install php5-dev 这样电脑会帮你自己配置了,很简单的下面我们就下载扩展包了http://pecl.php ...
- 666:放苹果(划分dp)
666:放苹果 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示 ...
- jquery如何获取type=hidden的input元素的值?
function setHiddenFields() { var hiddens = $("input:hidden"); $.each(hiddens, function (in ...
- absolute float 比较分析
同一:absolute-float 都脱离了文档流,也就是默认情况下,父盒子计算高度不包括 absolute/float 的元素 下例: 两个 parentDiv,背景色:深海蓝;边框:1px红色实线 ...
- iermu爱耳目
百度推出720P 云直播摄像头 查看直播请点击 直播 互联网网盘新动向实现初见端倪,之前写过一篇关于互联网网盘的思考. 我只是仅仅有想法而已,百度已经实现了一部分. 具体功能参见http://www. ...
- 170321、Spring+Quartz实现定时任务的配置方法
Quartz是Java版开源定时调度器. 核心概念: Job 表示一个工作,要执行的具体内容.此接口中只有一个方法 void execute(JobExecutionContext context): ...
- [linux][shell]负载均衡下多个服务器代码同步方案
说明: 服务器是腾讯的云服务器(腾讯用的是linux container),远程登陆云服务器需要使用代理,在服务器中不能访问外网,所以当时也就没有去想做svn 需求: 1. 把同样的代码同步到不同的服 ...
- LeetCode—Longest Consecutive Sequence
题目描述: Given an unsorted array of integers, find the length of the longest consecutive elements seque ...