bzoj2588: Spoj 10628. Count on a tree(树上第k大)(主席树)
每个节点继承父节点的树,则答案为query(root[x]+root[y]-root[lca(x,y)]-root[fa[lca(x,y)]])
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=;
struct poi{int size,lt,rt;}tree[maxn*];
struct zs{int too,pre;}e[maxn<<];
int n,m,x,y,z,sz,tot,N;
int d[maxn],f[maxn][],last[maxn],root[maxn],v[maxn],b[maxn],num[maxn];
inline void read(int &k)
{
int f=;k=;char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(c<=''&&c>='')k=k*+c-'',c=getchar();
k*=f;
}
inline void add(int x,int y){e[++tot].too=y;e[tot].pre=last[x];last[x]=tot;}
void insert(int &x,int l,int r,int cx)
{
tree[++sz]=tree[x];tree[sz].size++;x=sz;
if(l==r)return;
int mid=(l+r)>>;
if(cx<=mid)insert(tree[x].lt,l,mid,cx);
else insert(tree[x].rt,mid+,r,cx);
}
int query(int a,int b,int c,int d,int l,int r,int k)
{
if(l==r)return l;
int mid=(l+r)>>;
int t1=tree[a].lt,t2=tree[b].lt,t3=tree[c].lt,t4=tree[d].lt;
int tmp=tree[t1].size+tree[t2].size-tree[t3].size-tree[t4].size;
if(tmp>=k)return query(t1,t2,t3,t4,l,mid,k);
return query(tree[a].rt,tree[b].rt,tree[c].rt,tree[d].rt,mid+,r,k-tmp);
}
void dfs(int x,int fa)
{
root[x]=root[fa];insert(root[x],,N,v[x]);
d[x]=d[fa]+;f[x][]=fa;
for(int i=last[x];i;i=e[i].pre)
if(e[i].too!=fa)dfs(e[i].too,x);
}
inline int lca(int x,int y)
{
if(d[x]<d[y])swap(x,y);
for(int i=;i>=;i--)
if(d[f[x][i]]>=d[y])x=f[x][i];
if(x==y)return x;
for(int i=;i>=;i--)
if(f[x][i]!=f[y][i])x=f[x][i],y=f[y][i];
return f[x][];
}
int main()
{
read(n);read(m);
for(int i=;i<=n;i++)read(v[i]),b[i]=v[i];N=n;
sort(b+,b++N);N=unique(b+,b++N)-b-;
for(int i=;i<=n;i++)x=lower_bound(b+,b++N,v[i])-b,num[x]=v[i],v[i]=x;
for(int i=;i<n;i++)
read(x),read(y),add(x,y),add(y,x);
dfs(,);for(int j=;j<;j++)for(int i=;i<=n;i++)f[i][j]=f[f[i][j-]][j-];
int lan=;
for(int i=;i<=m;i++)
{
read(x);read(y);read(z);x^=lan;int fq=lca(x,y);
printf("%d",lan=num[query(root[x],root[y],root[fq],root[f[fq][]],,N,z)]);
if(i!=m)puts("");
}
}
bzoj2588: Spoj 10628. Count on a tree(树上第k大)(主席树)的更多相关文章
- 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 ...
- 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)
Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...
- 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 ...
- Count on a tree(SPOJ COT + 树上第k大 + 主席树 + LCA)
题目链接:https://www.spoj.com/problems/COT/en/ 题目: 题意: 给你一棵有n个节点的树,求节点u到节点v这条链上的第k大. 思路: 我们首先用dfs进行建题目给的 ...
- spoj COT - Count on a tree (树上第K小 LCA+主席树)
链接: https://www.spoj.com/problems/COT/en/ 思路: 首先看到求两点之前的第k小很容易想到用主席树去写,但是主席树处理的是线性结构,而这道题要求的是树形结构,我们 ...
- BZOJ2588: Spoj 10628. Count on a tree
传送门 刚开始看错题以为是dfs序瞎搞.. 后来看清题了开始想用树剖瞎搞... 感觉要滚粗啊.. 对于每个点到根的路径建立线段树,暴力建MLE没跑,上主席树,然后$(x,y)$的路径就可以先求出来$L ...
- 【主席树】bzoj2588 Spoj 10628. Count on a tree
每个点的主席树的root是从其父转移来的.询问的时候用U+V-LCA-FA(LCA)即可. #include<cstdio> #include<algorithm> using ...
- 主席树初探--BZOJ2588: Spoj 10628. Count on a tree
n<=100000的点权树,有m<=100000个询问,每次问两个点间的第k小点权,保证有解,强制在线. 主席上树啦!类似于之前的序列不带修改询问的前缀表示法,现在只要把前缀当成某点到根的 ...
随机推荐
- String、StringBuffer、StringBuilder的区别和解析
1.三个类之间的关系 他们都是通过字符数组来实现的,继承关系 String:字符串常量,不可变类 StringBuffer:字符串变量,可变类,线程安全 StringBuilder:字符串变量,可变类 ...
- IDEA搭载Tomcat使用JSTL连接Oracle数据库
1.在IDEA中,JSTL库添加到WEB-INF/lib下面可以直接在JSP页面上通过 <%@ taglib uri="http://java.sun.com/jsp/jstl/cor ...
- python函数学习之装饰器
装饰器 装饰器的本质是一个python函数,它的作用是在不对原函数做任何修改的同时,给函数添加一定的功能.装饰器的返回值也是一个函数对象. 分类: 1.不带参数的装饰器函数: def wrapper( ...
- opencv-学习笔记(2)
opencv-学习笔记(2) 这章记录了 获取像素点,改变像素点 获取图像的属性(行,列,通道数,数据类型) roi感应区 拆分以及合并图像通道 边缘扩充 opencv获取像素点,改变像素点 ---- ...
- IntelliJ IDEA 2018 for MAC安装及破解
---------------------说在前面-------------------------- IntelliJ IDEA 2018 版本为2018.1.4 教程按照下载安装sdk.破解两部分 ...
- Python3 深浅拷贝
一 定义 在Python中对象的赋值其实就是对象的引用.当创建一个对象,把它赋值给另一个变量的时候,python并没有拷贝这个对象,只是拷贝了这个对象的引用而已. 浅拷贝: 浅拷贝值只拷贝一层,具有自 ...
- Oil Deposits(DFS连通图)
Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...
- Thunder团队第五周 - Scrum会议1
Scrum会议1 小组名称:Thunder 项目名称:i阅app Scrum Master:杨梓瑞 工作照片: 邹双黛在照相,所以图片中没有该同学. 参会成员: 王航:http://www.cnblo ...
- Python 循环语句和运算符
while 循环 while 条件 : //条件为True时,执行while下带有缩进的语句 语句1 语句2 语句3 for循环 for循环可以用来遍历某一对象(遍历:通俗点说,就是把这个循环中的第一 ...
- iOS开发allocWithZone介绍
首先我们知道,我们需要保证单例类只有一个唯一的实例,而平时我们在初始化一个对象的时候, [[Class alloc] init],其实是做了两件事. alloc 给对象分配内存空间,init是对对象的 ...