地址: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
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的更多相关文章

  1. 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 ...

  2. spoj COT - Count on a tree (树上第K小 LCA+主席树)

    链接: https://www.spoj.com/problems/COT/en/ 思路: 首先看到求两点之前的第k小很容易想到用主席树去写,但是主席树处理的是线性结构,而这道题要求的是树形结构,我们 ...

  3. SPOJ 10628. SPOJ COT Count on a tree 可持久化线段树

    这题是裸的主席树,每个节点建一棵主席树,再加个lca就可以了. 历尽艰辛,终于A掉了这一题,这般艰辛也显示出了打代码的不熟练. 错误:1.lca倍增的时候i和j写反了,RE了5次,实在要吸取教训 2. ...

  4. SPOJ COT Count on a tree(树上主席树 + LCA 求点第k小)题解

    题意:n个点的树,每个点有权值,问你u~v路径第k小的点的权值是? 思路: 树上主席树就是每个点建一棵权值线段树,具体看JQ博客,LCA用倍增logn求出,具体原理看这里 树上主席树我每个点的存的是点 ...

  5. spoj COT - Count on a tree(主席树 +lca,树上第K大)

    您将获得一个包含N个节点的树.树节点的编号从1到Ñ.每个节点都有一个整数权重. 我们会要求您执行以下操作: uvk:询问从节点u到节点v的路径上的第k个最小权重 输入 在第一行中有两个整数Ñ和中号.( ...

  6. 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 ...

  7. 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  ...

  8. 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  ...

  9. 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 ...

随机推荐

  1. Servlet与JSP九大内置对象的对应关系

    JSP对象 Servlet中怎样获得 out resp.getWriter request service方法中的req参数 response service方法中的resp参数 session re ...

  2. Java Web项目--使用Servlet生成一个页面

    为了生成一个servlet对应的网页.我们需要新建一个web.xml,其中将会放置servlet的相关信息.web.xml文件放置在WebContent/WEB-INF/目录下.(我们在Eclipe中 ...

  3. 在虚拟机中的搭建Web服务器(CentOS)

    1.制作本地yum源 相关可查看:http://www.cnblogs.com/xiaomingzaixian/p/8424290.html 2.安装JDK 上传上传jdk-7u45-linux-x6 ...

  4. 通过实现一个TableView来理解iOS UI编程

    推荐一篇神作: 通过实现一个TableView来理解iOS UI编程 http://blog.jobbole.com/61101/

  5. Jenkins 持续集成配置

    Jenkins搭建.NET自动编译测试与发布环境 Jenkins之Deploy部署(包括站点和类库项目) * 续篇--TFS+MSbuild+jenkins 实现 持续集成+自动部署到WEB网站 Je ...

  6. 3 differences between Savepoints and Checkpoints in Apache Flink

    https://mp.weixin.qq.com/s/nQOxsZUZSiPi7Sx40mgwsA 20181104 3 differences between Savepoints and Chec ...

  7. windows中根据进程PID查找进程对象过程深入分析

    这里windows和Linxu系列的PID 管理方式有所不同,windows中进程的PID和句柄没有本质区别,根据句柄索引对象和根据PID或者TID查找进程或者线程的步骤也是一样的.   句柄是针对进 ...

  8. Flask视图函数与模板语法

    1.Django中的CBV模式 ​ 2.Flask中的CBV和FBV def auth(func):     def inner(*args, **kwargs):         result =  ...

  9. yarn的使用

    yarn 的安装 npm install -g yarn yarn -version  查看yarn是否安装成功 一.首先需要了解的命令 npm install === yarn —— install ...

  10. 如何用Qt Creator输出helloworld

    0 引言:相比于MFC只要直接在VS上搭建,Qt的配置就相对复杂了,Qt新手上路,老司机绕道,记录下配置Qt的整个过程,直到最终用C++输出“hello world”. 搭建环境:Win10 + qt ...