[SP10628]Count on a tree
Description:
给定一颗n个点的树,有m个询问,求任意两点路径上点权第k小的点
Hint:
\(n,m<=1e5\)
Solution:
比较水
以每个点到根节点的数的前缀和建主席树
结合树上两点距离求法
每次的\(size=size[u]+size[v]-size[lca]-size[fa[lca]]\)
#include<bits/stdc++.h>
using namespace std;
const int mxn=6e5+5;
struct ed {
int to,nxt;
}t[mxn<<1];
int n,m,s,tot,ans,cnt,hd[mxn],f[mxn][19],ls[mxn<<3],rs[mxn<<3],sum[mxn<<3],a[mxn],b[mxn],rt[mxn],dep[mxn];
inline void add(int u,int v) {
t[++cnt]=(ed){v,hd[u]},hd[u]=cnt;
}
void update(int las,int& p,int l,int r,int val)
{
if(!p) p=++tot;
sum[p]=sum[las]+1;
if(l==r) return ;
int mid=(l+r)>>1;
if(val<=mid) update(ls[las],ls[p],l,mid,val),rs[p]=rs[las];
else update(rs[las],rs[p],mid+1,r,val),ls[p]=ls[las];
}
int query(int las1,int las2,int p1,int p2,int l,int r,int k)
{
if(l==r) return l;
int tp=sum[ls[p1]]+sum[ls[p2]]-sum[ls[las1]]-sum[ls[las2]];
int mid=(l+r)>>1;
if(k<=tp) query(ls[las1],ls[las2],ls[p1],ls[p2],l,mid,k);
else query(rs[las1],rs[las2],rs[p1],rs[p2],mid+1,r,k-tp);
}
void dfs(int u,int fa)
{
f[u][0]=fa; dep[u]=dep[fa]+1;
update(rt[fa],rt[u],1,s,b[u]);
for(int i=hd[u];i;i=t[i].nxt) {
int v=t[i].to;
if(v==fa) continue ;
dfs(v,u);
}
}
int LCA(int x,int y)
{
if(dep[x]<dep[y]) swap(x,y);
for(int i=18;i>=0;--i)
if(dep[f[x][i]]>=dep[y])
x=f[x][i];
if(x==y) return x;
for(int i=18;i>=0;--i)
if(f[x][i]!=f[y][i])
x=f[x][i],y=f[y][i];
return f[x][0];
}
void solve(int u,int v,int k)
{
u^=ans;
int lca=LCA(u,v);
ans=a[query(rt[lca],rt[f[lca][0]],rt[u],rt[v],1,s,k)];
printf("%d\n",ans);
}
int main()
{
scanf("%d%d",&n,&m); int u,v,k;
for(int i=1;i<=n;++i) scanf("%d",&a[i]),b[i]=a[i];
sort(a+1,a+n+1);
s=unique(a+1,a+n+1)-a-1;
for(int i=1;i<=n;++i) b[i]=lower_bound(a+1,a+s+1,b[i])-a;
for(int i=1;i<n;++i) {
scanf("%d%d",&u,&v);
add(u,v); add(v,u);
}
dfs(1,0);
for(int j=1;j<=18;++j)
for(int i=1;i<=n;++i)
f[i][j]=f[f[i][j-1]][j-1];
for(int i=1;i<=m;++i) {
scanf("%d%d%d",&u,&v,&k);
solve(u,v,k);
}
return 0;
}
[SP10628]Count on a tree的更多相关文章
- 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 ...
- 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 ...
- 【BZOJ-2588】Count on a tree 主席树 + 倍增
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 3749 Solved: 873[ ...
- 【BZOJ】【2588】COT(Count On a Tree)
可持久化线段树 maya……树么……转化成序列……所以就写了个树链剖分……然后每个点保存的是从它到根的可持久化线段树. 然后就像序列一样查询……注意是多个左端点和多个右端点,处理方法类似BZOJ 19 ...
- 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 主席树
10628. Count on a tree Problem code: COT You are given a tree with N nodes.The tree nodes are number ...
- 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 + 主席树 )
Orz..跑得还挺快的#10 自从会树链剖分后LCA就没写过倍增了... 这道题用可持久化线段树..点x的线段树表示ROOT到x的这条路径上的权值线段树 ----------------------- ...
- SPOJ Count on a tree
Count on a tree Time Limit:129MS Memory Limit:1572864KB 64bit IO Format:%lld & %llu Subm ...
随机推荐
- Vue router拦截 如果用户并未登录直接跳转到登录界面(最简单的cookie演示)
router.beforeEach(function(to,from,next){ console.log('路由拦截') console.log(to.name) console.log(from. ...
- ActiveSync中的http内容组织
1. POST Request 数据格式 Request-line Request-headers CR/LF Request Body Request-line POST <URI> H ...
- CentOS安装CAS 5.3.4服务端
1.安装jdk1.8 https://www.cnblogs.com/kgdxpr/p/6824093.html 2.安装tomcat8 3.安装maven https://www.cnblogs.c ...
- [转] babel入门基础
背景 babel的官网说babel是下一代的js语法编译器,现在自己也在很多项目中使用了babel,可是自己对babel的认识呢,只停留在从google和别人项目中copy的配置代码上,内心感到很不安 ...
- jquery源码中noConflict(防止$和jQuery的命名冲突)的实现原理
jquery源码中noConflict(防止$和jQuery的命名冲突)的实现原理 最近在看jquery源码分析的视频教学,希望将视频中学到的知识用博客记录下来,更希望对有同样对jquery源码有困惑 ...
- 使用jquery获取父元素或父节点
使用jquery获取父元素或父节点,比较简单,jquery提供了丰富的方法来让我们使用jquery获取父元素或父节点 jquery获取父元素方法比较多,比如parent(),parents(),c ...
- 一起学Hive——创建内部表、外部表、分区表和分桶表及导入数据
Hive本身并不存储数据,而是将数据存储在Hadoop的HDFS中,表名对应HDFS中的目录/文件.根据数据的不同存储方式,将Hive表分为外部表.内部表.分区表和分桶表四种数据模型.每种数据模型各有 ...
- 1000. A+B Problem
Description Calculate a+b Input Two integer a,b (0<=a,b<=10) Output Output a+b Sample Input 1 ...
- angularjs 中通过 $location 进行路由跳转传参
$location.path('/page1').search({id: $scope.id,name:$scope.name}); 带参数跳转页面,在新的页面通过$routeParams接收参数 $ ...
- net core体系-web应用程序-4net core2.0大白话带你入门-10asp.net core session的使用
asp.net core session的使用 Session介绍 本文假设读者已经了解Session的概念和作用,并且在传统的.net framework平台上使用过. Asp.net core ...