BZOJ.2588.Count on a tree(主席树 静态树上第k小)
/*
序列上的主席树 某点是利用前一个点的根建树
同理 树上的主席树 某个节点可以利用其父节点(is unique)的根建树
排名可以利用树上前缀和求得: 对于(u,v),w=LCA(u,v),u->v这条链的值就是 sum[u->root]+sum[v->root]-sum[w->root]-sum[fa[w]->root](减一个w)
这样like序列上的主席树 用两棵树进行差分,树上的用四棵树即可
每个节点是一棵权值线段树 和给的树是独立的 没什么关系 别想错
*/
#include<cstdio>
#include<cctype>
#include<algorithm>
//#define gc() getchar()
#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
const int N=1e5+5,MAXIN=2e5;
int n,m,cnt,W[N],A[N],ref[N],fa[N],Enum,H[N],to[N<<1],nxt[N<<1],root[N];
char IN[MAXIN],*SS=IN,*TT=IN;
namespace Subdivision//for LCA
{
int dep[N],sz[N],son[N],top[N];
void DFS1(int x)
{
sz[x]=1; int mx=0;
for(int v,i=H[x]; i; i=nxt[i])
if((v=to[i])!=fa[x])
{
dep[v]=dep[x]+1, DFS1(v), sz[x]+=sz[v];
if(mx<sz[v]) mx=sz[v],son[x]=v;
}
}
void DFS2(int x,int tp)
{
top[x]=tp;
if(son[x])
{
DFS2(son[x],tp);
for(int i=H[x]; i; i=nxt[i])
if(to[i]!=fa[x] && to[i]!=son[x])
DFS2(to[i],to[i]);
}
}
int LCA(int u,int v)
{
while(top[u]!=top[v])
{
if(dep[top[u]]<dep[top[v]]) std::swap(u,v);
u=fa[top[u]];
}
return dep[u]>dep[v] ? v : u;
}
}
struct Seg_Tree
{
int tot,sum[N*18],son[N*18][2];
void Insert(int x,int &y,int l,int r,int p/*,int v*/)
{
sum[y=++tot]=sum[x]+1;
if(l<r)
{
int m=l+r>>1;
if(p<=m) son[y][1]=son[x][1],Insert(son[x][0],son[y][0],l,m,p);
else son[y][0]=son[x][0],Insert(son[x][1],son[y][1],m+1,r,p);
}
}
int Query(int faLca,int Lca,int x,int y,int l,int r,int k)
{
if(l==r) return l;
int m=l+r>>1, tmp=sum[son[x][0]]+sum[son[y][0]]-sum[son[Lca][0]]-sum[son[faLca][0]];
if(tmp>=k) return Query(son[faLca][0],son[Lca][0],son[x][0],son[y][0],l,m,k);
else return Query(son[faLca][1],son[Lca][1],son[x][1],son[y][1],m+1,r,k-tmp);
}
}t;
inline int read()
{
int now=0,f=1;register char c=gc();
for(;!isdigit(c);c=gc()) if(c=='-') f=-1;
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now*f;
}
inline void AddEdge(int u,int v)
{
to[++Enum]=v, nxt[Enum]=H[u], H[u]=Enum;
to[++Enum]=u, nxt[Enum]=H[v], H[v]=Enum;
}
void DFS(int x,int f)
{
t.Insert(root[f],root[x],1,cnt,A[x]/*,1*/);
fa[x]=f;
for(int i=H[x]; i; i=nxt[i])
if(to[i]!=fa[x]) DFS(to[i],x);
}
inline int Find(int x)
{
int l=1,r=cnt,m;
while(l<r)
{
if(ref[m=l+r>>1]<x) l=m+1;
else r=m;
}
return l;
}
void Discrete()
{
std::sort(ref+1,ref+1+n);
cnt=1;
for(int i=2; i<=n; ++i)
if(ref[i]!=ref[i-1]) ref[++cnt]=ref[i];
for(int p,i=1; i<=n; ++i)
W[p=Find(A[i])]=A[i], A[i]=p;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("2588.in","r",stdin);
#endif
n=read(),m=read()-1;
for(int i=1; i<=n; ++i) ref[i]=A[i]=read();
for(int u,v,i=1; i<n; ++i) u=read(),v=read(),AddEdge(u,v);
Discrete(), DFS(1,0);
Subdivision::DFS1(1), Subdivision::DFS2(1,1);
int ans=0,u,v,k,w;
while(m--)
{
u=ans^(read()),v=read(),k=read(),w=Subdivision::LCA(u,v);
printf("%d\n",ans=W[t.Query(root[fa[w]],root[w],root[u],root[v],1,cnt,k)]);
}
u=ans^(read()),v=read(),k=read(),w=Subdivision::LCA(u,v);
printf("%d",W[t.Query(root[fa[w]],root[w],root[u],root[v],1,cnt,k)]);
return 0;
}
第二次写(2018.4.4):
//10^5还是需要18*10^5的空间!
#include <cstdio>
#include <cctype>
#include <algorithm>
#define gc() getchar()
const int N=1e5+5;
int n,Q,cnt,ref[N],A[N],H[N],Enum,nxt[N<<1],to[N<<1],root[N],top[N],dep[N],sz[N],son[N],fa[N];
inline int read()
{
int now=0,f=1;register char c=gc();
for(;!isdigit(c);c=gc()) if(c=='-') f=-1;
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now*f;
}
inline void AddEdge(int u,int v)
{
to[++Enum]=v, nxt[Enum]=H[u], H[u]=Enum;
to[++Enum]=u, nxt[Enum]=H[v], H[v]=Enum;
}
namespace T//Seg_Tree
{
#define lson son[x][0]
#define rson son[x][1]
int tot,sum[N*17],son[N*17][2];
void Insert(int x,int &y,int l,int r,int p)
{
sum[y=++tot]=sum[x]+1;
if(l<r){
int m=l+r>>1;
if(p<=m) son[y][1]=rson,Insert(lson,son[y][0],l,m,p);
else son[y][0]=lson,Insert(rson,son[y][1],m+1,r,p);
}
}
int Query(int x,int y,int lca,int falca,int l,int r,int k)
{
if(l==r) return l;
int delta=sum[lson]+sum[son[y][0]]-sum[son[lca][0]]-sum[son[falca][0]];
if(delta<k) return Query(rson,son[y][1],son[lca][1],son[falca][1],(l+r>>1)+1,r,k-delta);
return Query(lson,son[y][0],son[lca][0],son[falca][0],l,l+r>>1,k);
}
}
int Find(int x)
{
int l=1,r=cnt,mid;
while(l<r)
if(ref[mid=l+r>>1]<x) l=mid+1;
else r=mid;
return l;
}
void DFS1(int x)
{
int mx=0; sz[x]=1;
T::Insert(root[fa[x]],root[x],1,cnt,A[x]);
for(int v,i=H[x]; i; i=nxt[i])
if((v=to[i])!=fa[x])
{
fa[v]=x, dep[v]=dep[x]+1, DFS1(v), sz[x]+=sz[v];
if(sz[v]>mx) mx=sz[v],son[x]=v;
}
}
void DFS2(int x,int tp)
{
top[x]=tp;
if(son[x]){
DFS2(son[x],tp);
for(int i=H[x]; i; i=nxt[i])
if(to[i]!=fa[x] && to[i]!=son[x]) DFS2(to[i],to[i]);
}
}
int LCA(int u,int v)
{
while(top[u]!=top[v])
{
if(dep[top[u]]<dep[top[v]]) std::swap(u,v);
u=fa[top[u]];
}
return dep[u]>dep[v]?v:u;
}
int main()
{
n=read(),Q=read();
for(int i=1; i<=n; ++i) ref[i]=A[i]=read();
for(int u,v,i=1; i<n; ++i) u=read(),v=read(),AddEdge(u,v);
std::sort(ref+1,ref+1+n), cnt=1;
for(int i=2; i<=n; ++i) if(ref[i]!=ref[i-1]) ref[++cnt]=ref[i];
for(int i=1; i<=n; ++i) A[i]=Find(A[i]);
DFS1(1), DFS2(1,1);
int res=0,u,v,w,k;
while(Q--)
u=read()^res,v=read(),k=read(),w=LCA(u,v),
printf("%d\n",res=ref[T::Query(root[u],root[v],root[w],root[fa[w]],1,cnt,k)]);
return 0;
}
BZOJ.2588.Count on a tree(主席树 静态树上第k小)的更多相关文章
- spoj COT - Count on a tree(主席树 +lca,树上第K大)
您将获得一个包含N个节点的树.树节点的编号从1到Ñ.每个节点都有一个整数权重. 我们会要求您执行以下操作: uvk:询问从节点u到节点v的路径上的第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 ...
- 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 ...
- 洛谷.3834.[模板]可持久化线段树(主席树 静态区间第k小)
题目链接 //离散化后范围1~cnt不要错 #include<cstdio> #include<cctype> #include<algorithm> //#def ...
- A - 低阶入门膜法 - K-th Number (主席树查询区间第k小)
题目链接:https://cn.vjudge.net/contest/284294#problem/A 题目大意:主席树查询区间第k小. 具体思路:主席树入门. AC代码: #include<i ...
- HDU 5919 - Sequence II (2016CCPC长春) 主席树 (区间第K小+区间不同值个数)
HDU 5919 题意: 动态处理一个序列的区间问题,对于一个给定序列,每次输入区间的左端点和右端点,输出这个区间中:每个数字第一次出现的位子留下, 输出这些位子中最中间的那个,就是(len+1)/2 ...
- 主席树--动态区间第k小
主席树--动态区间第\(k\)小 模板题在这里洛谷2617. 先对几个问题做一个总结: 阅读本文需要有主席树的基础,也就是通过区间kth的模板题. 静态整体kth: sort一下找第k小,时间复杂度\ ...
- BZOJ 2588: Spoj 10628. Count on a tree 主席树+lca
分析:树上第k小,然后我想说的是主席树并不局限于线性表 详细分析请看http://www.cnblogs.com/rausen/p/4006116.html,讲的很好, 然后因为这个熟悉了主席树,真是 ...
- 【BZOJ-2588】Count on a tree 主席树 + 倍增
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 3749 Solved: 873[ ...
随机推荐
- 一步步实现windows版ijkplayer系列文章之二——Ijkplayer播放器源码分析之音视频输出——视频篇
一步步实现windows版ijkplayer系列文章之一--Windows10平台编译ffmpeg 4.0.2,生成ffplay 一步步实现windows版ijkplayer系列文章之二--Ijkpl ...
- Android 动画:你真的会使用插值器与估值器吗?
目录 目录 1. 插值器(Interpolator) 1.1 简介 定义:一个接口 作用:设置 属性值 从初始值过渡到结束值 的变化规律 如匀速.加速 & 减速 等等 即确定了 动画效果变 ...
- Linux VMware tools安装步骤
Linux VMware tools安装步骤: 1.安装环境介绍 #虚拟机版本:VMware-workstation-full-10 #linux分发版本:CentOS-6.4-i386-LiveCD ...
- C++ code:判断字符串相等
如果两个字符串中0和1的个数分别相等,则称该对字符串为相等. 方法一: 由于string类对于读取文件中子串以及比较都很方便,所以,用string类的对象来逐对存放字串,然后将字串分别排序后对其进行比 ...
- MACE(2)-----模型编译
作者:十岁的小男孩 QQ:929994365 无用 本文仅用于学习研究,非商业用途,欢迎大家指出错误一起学习,文章内容翻译自 MACE 官方手册,记录本人阅读与开发过程,力求不失原意,但推荐阅读原文. ...
- poj3061 poj3320 poj2566尺取法基础(一)
poj3061 给定一个序列找出最短的子序列长度,使得其和大于等于S 那么只要用两个下标,区间和小于S时右端点向右移动,区间和大于S时左端点向右移动,在这个过程中更新Min #include < ...
- 步步为营-59-svn简介
说明:版本控制器Svn的使用,安装教程不再多说 VisualSVN-Server--项目经理 TortoiseSVN--右击的时候显示 VisualSVN-- visual studio中使用 1 搭 ...
- hdu 2197 求长度为n的本原串 (快速幂+map)
Problem Description由0和1组成的串中,不能表示为由几个相同的较小的串连接成的串,称为本原串,有多少个长为n(n<=100000000)的本原串?答案mod2008.例如,10 ...
- Delphi数据库数据用文件流方式快速写入Excel文件
在开发数据库应用程序中,经常要将类型相同的数据导出来,放到Excel文件中,利用Excel强大的编辑功能,对数据作进一步的加工处理.这有许多的方法,我们可以使用OLE技术,在Delphi中创建一个自动 ...
- POJ 2229 Sumsets【DP】
题意:把n拆分为2的幂相加的形式,问有多少种拆分方法. 分析:dp,任何dp一定要注意各个状态来源不能有重复情况.根据奇偶分两种情况,如果n是奇数则与n-1的情况相同.如果n是偶数则还可以分为两种情况 ...