主席树初探--BZOJ2588: Spoj 10628. Count on a tree
n<=100000的点权树,有m<=100000个询问,每次问两个点间的第k小点权,保证有解,强制在线。
主席上树啦!类似于之前的序列不带修改询问的前缀表示法,现在只要把前缀当成某点到根的信息即可。然后比如要问x点和y点,z为lca(x,y),w为z的爸爸,那么x,y,z,w四棵线段树一起跑即可。
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
//#include<iostream>
using namespace std; int n,m;
#define maxn 100011
#define maxm 2000011
struct SMT
{
struct Node
{
int son[];
int cnt;
}a[maxm];
int size,n;
void clear(int m) {n=m;size=;a[].cnt=;}
void build(int pre,int &rt,int L,int R,int num)
{
rt=++size;
a[rt].cnt=a[pre].cnt+;
if (L==R) {a[rt].son[]=a[rt].son[]=;return;}
const int mid=(L+R)>>;
if (num<=mid) build(a[pre].son[],a[rt].son[],L,mid,num),a[rt].son[]=a[pre].son[];
else build(a[pre].son[],a[rt].son[],mid+,R,num),a[rt].son[]=a[pre].son[];
}
void build(int pre,int &rt,int num) {build(pre,rt,,n,num);}
// void test(int x,int L,int R)
// {
// const int mid=(L+R)>>1;
// if (a[x].son[0]) test(a[x].son[0],L,mid);
//// cout<<L<<' '<<R<<' '<<a[x].cnt<<endl;
// if (a[x].son[1]) test(a[x].son[1],mid+1,R);
// }
// void test(int x) {test(x,1,n);}
}smt; struct Edge{int to,next;}edge[maxn<<];int first[maxn],le=;
void in(int x,int y) {Edge &e=edge[le];e.to=y;e.next=first[x];first[x]=le++;}
void insert(int x,int y) {in(x,y);in(y,x);} int a[maxn],b[maxn],lb;
int fa[maxn][],dep[maxn],rt[maxn];
void dfs(int x,int f)
{
fa[x][]=f;dep[x]=dep[f]+;
for (int j=;j<=;j++)
fa[x][j]=fa[fa[x][j-]][j-];
smt.build(rt[f],rt[x],a[x]);
for (int i=first[x];i;i=edge[i].next)
{
const Edge &e=edge[i];if (e.to==f) continue;
dfs(e.to,x);
}
}
void pre() {dfs(,);} int lca(int x,int y)
{
if (dep[x]<dep[y]) {int t=x;x=y;y=t;}
for (int i=;i>=;i--) if (dep[fa[x][i]]>=dep[y]) x=fa[x][i];
if (x==y) return x;
for (int i=;i>=;i--) if (fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i];
return fa[x][];
} int main()
{
scanf("%d%d",&n,&m);
for (int i=;i<=n;i++) scanf("%d",&a[i]),b[i]=a[i];
lb=n;sort(b+,b++lb);lb=unique(b+,b++lb)-b-;
for (int i=;i<=n;i++) a[i]=lower_bound(b+,b++lb,a[i])-b; smt.clear(lb);
for (int i=,x,y;i<n;i++)
{
scanf("%d%d",&x,&y);
insert(x,y);
}
pre();
// for (int i=1;i<=n;i++) smt.test(rt[i]),cout<<endl;
int last=;
for (int i=,x,y,K;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&K);
x^=last;
int z=lca(x,y),w=fa[z][],L=,R=lb,tmp;
// cout<<x<<' '<<y<<' '<<z<<' '<<w<<endl;
x=rt[x];y=rt[y];z=rt[z];w=rt[w];
while (L<R)
{
if ((tmp=smt.a[smt.a[x].son[]].cnt+smt.a[smt.a[y].son[]].cnt
-smt.a[smt.a[z].son[]].cnt-smt.a[smt.a[w].son[]].cnt)>=K)
x=smt.a[x].son[],y=smt.a[y].son[],z=smt.a[z].son[],w=smt.a[w].son[],R=(L+R)>>;
else x=smt.a[x].son[],y=smt.a[y].son[],z=smt.a[z].son[],w=smt.a[w].son[],K-=tmp,L=((L+R)>>)+;
// cout<<tmp<<' '<<K<<endl;
}
printf("%d",(last=b[L]));
if (i<m) puts("");
}
return ;
}
主席树初探--BZOJ2588: Spoj 10628. Count on a tree的更多相关文章
- 【主席树】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
传送门 刚开始看错题以为是dfs序瞎搞.. 后来看清题了开始想用树剖瞎搞... 感觉要滚粗啊.. 对于每个点到根的路径建立线段树,暴力建MLE没跑,上主席树,然后$(x,y)$的路径就可以先求出来$L ...
- bzoj2588: Spoj 10628. Count on a tree(树上第k大)(主席树)
每个节点继承父节点的树,则答案为query(root[x]+root[y]-root[lca(x,y)]-root[fa[lca(x,y)]]) #include<iostream> #i ...
- 【BZOJ2588】Spoj 10628. Count on a tree 主席树+LCA
[BZOJ2588]Spoj 10628. Count on a tree Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lasta ...
- 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 ...
- 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+主席树)
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MB Description 给定一棵N个节点的树,每个点 ...
- BZOJ 2588: Spoj 10628. Count on a tree( LCA + 主席树 )
Orz..跑得还挺快的#10 自从会树链剖分后LCA就没写过倍增了... 这道题用可持久化线段树..点x的线段树表示ROOT到x的这条路径上的权值线段树 ----------------------- ...
随机推荐
- Proactive Patching Overview
1.Proactive Patching Overview Between Patch Set releases, Oracle's proactive patching program provid ...
- android开发学习——Socket发送和接收
client -- server发送过程中,涉及的输入流输出流: http://blog.csdn.net/dlwh_123/article/details/35982015 (良心好文) 需 ...
- js的replace函数把"$"替换成成"\$"
var aa = 18$ 转换成 aa = 18\$ aa.replace("\$","\\\$"); 注意JS的replace方法只能替换第一 ...
- 关于jquery获取单选框value属性值为on的问题
当取单选框的value值的时候,前提是要有value这个属性,如果没有value属性那么取出来的就会为on 取value值的常见三种方式为 $("input[name='XXX']:chec ...
- zabbix3.0.4 部署之八 (zabbix3.0.4 报警前端配置)
(如何让报警信息推送----微信.邮件)(邮件与微信一样就不在重复) 创建一个用户 将用户加入administrator组 添加之前设置的报警媒介脚本 设置报警等级 创建动作 配置报警内容 设置报警条 ...
- Windows bat 设置代理
转自tt-0411 @echo off cls color 0A Echo The program is running... Echo Setting the ip and dns... netsh ...
- CLUSTER - 根据一个索引对某个表集簇
SYNOPSIS CLUSTER indexname ON tablename CLUSTER tablename CLUSTER DESCRIPTION 描述 CLUSTER 指示PostgreSQ ...
- WPF学习- AllowDrop 用户控件启用拖放功能
知识点: 创建自定义用户控件(UserControl) 使用户控件成为拖动源 使用户控件成为放置目标 使面板能够接收从用户控件放置的数据 创建项目: 1.新建WPF项目(Wpf-AllowDrop) ...
- web pack 生成本地dist后 本地可以访问 路径由/ 改 ./
config / index.js 里面将 / 改成 ./ 有两个 都改了 反正管用 然后npm run build 如果涉及到字体 css里面不会改 需要手工改成 ../../ 反正一般用到字体也不 ...
- 一个圆的移动 AE教程 速度曲线调节
AE里面速度的曲线调节 最终的小效果 两个关键点: 一:速度曲线调节 编辑速度图标,他的曲线是编辑速度的. 二:节点不要用贝塞尔曲线 编辑值图标,就是圆圈的x值y值的曲线.控制位置移动的. 选择一个节 ...