bzoj 2588: Spoj 10628. Count on a tree【主席树+倍增】
算是板子,把值离散化,每个点到跟上做主席树,然后查询的时候主席树上用u+v-lca-fa[lca]的值二分
#include<iostream>
#include<cstdio>
#include<map>
#include<algorithm>
using namespace std;
const int N=100005;
int n,m,h[N],cnt,tot,la,a[N],ha[N],b[N],has,f[N][30],rt[N],ind,po[N],nu[N],de[N];
map<int,int>mp;
struct qwe
{
int ne,to;
}e[N<<1];
struct zhuxishu
{
int ls,rs,sum;
}t[2200005];
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
void add(int u,int v)
{
cnt++;
e[cnt].ne=h[u];
e[cnt].to=v;
h[u]=cnt;
}
void dfs(int u,int fat)
{
f[u][0]=fat;
nu[++ind]=u;
po[u]=ind;
de[u]=de[fat]+1;
for(int i=h[u];i;i=e[i].ne)
if(e[i].to!=fat)
dfs(e[i].to,u);
}
void update(int l,int r,int pr,int &ro,int w)
{
ro=++tot;
t[ro].sum=t[pr].sum+1;
if(l==r)
return;
t[ro].ls=t[pr].ls;
t[ro].rs=t[pr].rs;
int mid=(l+r)>>1;
if(w<=mid)
update(l,mid,t[pr].ls,t[ro].ls,w);
else
update(mid+1,r,t[pr].rs,t[ro].rs,w);
}
int lca(int x,int y)
{
if(de[x]<de[y])
swap(x,y);
for(int i=16;i>=0;i--)
if((1<<i)&(de[x]-de[y]))
x=f[x][i];
for(int i=16;i>=0;i--)
if(f[x][i]!=f[y][i])
x=f[x][i],y=f[y][i];
return x==y?x:f[x][0];
}
int ques(int x,int y,int k)
{
int a=x,b=y,c=lca(a,b),d=f[c][0];
a=rt[po[a]],b=rt[po[b]],c=rt[po[c]],d=rt[po[d]];
int l=1,r=has;
while(l<r)
{
int mid=(l+r)>>1;
int now=t[t[a].ls].sum+t[t[b].ls].sum-t[t[c].ls].sum-t[t[d].ls].sum;
if(now>=k)
{
r=mid;
a=t[a].ls,b=t[b].ls,c=t[c].ls,d=t[d].ls;
}
else
{
k-=now;
l=mid+1;
a=t[a].rs,b=t[b].rs,c=t[c].rs,d=t[d].rs;
}
}
return ha[l];
}
int main()
{
n=read(),m=read();
for(int i=1;i<=n;i++)
a[i]=read(),b[i]=a[i];
sort(b+1,b+1+n);
for(int i=1;i<=n;i++)
if(i==1||b[i]!=b[i-1])
mp[b[i]]=++has,ha[has]=b[i];
for(int i=1;i<=n;i++)
a[i]=mp[a[i]];
for(int i=1;i<n;i++)
{
int x=read(),y=read();
add(x,y);add(y,x);
}
dfs(1,0);
for(int j=1;j<=16;j++)
for(int i=1;i<=n;i++)
f[i][j]=f[f[i][j-1]][j-1];
for(int i=1;i<=n;i++)
update(1,has,rt[po[f[nu[i]][0]]],rt[i],a[nu[i]]);
for(int i=1;i<=m;i++)
{
int x=read(),y=read(),k=read();
x^=la;
la=ques(x,y,k);
printf("%d",la);
if(i!=m)
puts("");
}
return 0;
}
bzoj 2588: Spoj 10628. Count on a tree【主席树+倍增】的更多相关文章
- 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
分析:树上第k小,然后我想说的是主席树并不局限于线性表 详细分析请看http://www.cnblogs.com/rausen/p/4006116.html,讲的很好, 然后因为这个熟悉了主席树,真是 ...
- 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 + 主席树 )
Orz..跑得还挺快的#10 自从会树链剖分后LCA就没写过倍增了... 这道题用可持久化线段树..点x的线段树表示ROOT到x的这条路径上的权值线段树 ----------------------- ...
- 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 (可持久化线段树)
Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 7669 Solved: 1894[Submi ...
- 【BZOJ2588】Spoj 10628. Count on a tree 主席树+LCA
[BZOJ2588]Spoj 10628. Count on a tree Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lasta ...
- 主席树 || 可持久化线段树 || LCA || BZOJ 2588: Spoj 10628. Count on a tree || Luogu P2633 Count on a tree
题面: Count on a tree 题解: 主席树维护每个节点到根节点的权值出现次数,大体和主席树典型做法差不多,对于询问(X,Y),答案要计算ans(X)+ans(Y)-ans(LCA(X,Y) ...
- ●BZOJ 2588 Spoj 10628. Count on a tree
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2588 题解: 主席树,在线,(求LCA)感觉主席树真的好厉害...在原树上建主席树.即对于原 ...
随机推荐
- ViewPagerIndicator
https://github.com/eltld/ViewPagerIndicator
- xcode编译 debug版或release 版
编译debug版本或release 版本 在Run和Stop按钮的右边有一个工程名 点击工程名,选择Manage Schemes 选择Edit... 左侧选择Run ProjectName.app 右 ...
- bash_profile打不开怎么办,用nano .bash_profile打开
I’ve spent years curating a collection of Mac bash aliases and shortcuts to make my life easier. My ...
- 三种方法打印 main函数的返回地址的值(old EIP)(用途,你懂得!)
这里能够简单的改动随意函数的返回地址.能够做到自己定义EIP的指向,就可以运行当前进程空间的随意指令,这里仅仅是让大家更清楚栈帧结构,没有涉及跨进程的inline HOOK 等,后面会陆续讲下读取随意 ...
- LeetCode(27)题解:Remove Element
https://leetcode.com/problems/remove-element/ Given an array and a value, remove all instances of th ...
- HDU 6040 Hints of sd0061 nth_element函数
Hints of sd0061 Problem Description sd0061, the legend of Beihang University ACM-ICPC Team, retired ...
- HBase数据压缩编码探索
摘要: 本文主要介绍了hbase对数据压缩,编码的支持,以及云hbase在社区基础上对数据压缩率和访问速度上了进行的改进. 前言 你可曾遇到这种需求,只有几百qps的冷数据缓存,却因为存储水位要浪费几 ...
- 线程安全 对StringBuilder抛出ArrayIndexOutOfBoundsException的探究
对StringBuilder抛出ArrayIndexOutOfBoundsException的探究 - CSDN博客 https://blog.csdn.net/liu_005/article/det ...
- em和i , b和Strong 的区别
这两对标签最大区别就是一个给搜索引擎看的,一个是给用户看的. b标签和strong标签给我们的主观感受都是加粗,但对搜索引擎来说b标签和普通的文字并没有什么区别,而strong标签却是起强调作用的. ...
- Hadoop MapReduce两种架构 以及 YARN
一.MRv1 Master - Slave 模式 存在JobTracker单点失败的问题,在YARN得到了解决. 主要包含4部分:JobTracker,TaskTracker,Task,Client ...