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)感觉主席树真的好厉害...在原树上建主席树.即对于原 ...
随机推荐
- Js学习第十天----函数
函数 什么是函数?函数是由事件驱动的或者当他被调用时运行的可反复使用代码块.预计没明确,个人觉得函数就是能完毕一个功能的代码块. 看个案例: <!DOCTYPE html> <htm ...
- 子组件跟随父组件re-render
想象一下这种场景,一个父组件下面一大堆子组件.然后呢,这个父组件re-render.是不是下面的子组件都得跟着re-render.可是很多子组件里面是冤枉的啊!!很多子组件的props 和 state ...
- flex中dispatchEvent的用法(自定义事件) .
Evevt和EventDispatcher类在as3的事件机制中是很重要的角色,dispatchEvent()是EventDispatcher类的一个事件发送方法,它可以发送出Event类或其子类的实 ...
- 实现多线程的方式Runnable
package com.thread.runnable; /** * 实现多线程的方式有继承Thread类和实现Runnable接口两种方式 * 哪种方式更好呢?实现的方式由于继承的方式. * 原因: ...
- SpringBoot学习之@SpringBootApplication注解
下面是我们经常见到SpringBoot启动类代码: @SpringBootApplicationpublic class DemoApplication extends SpringBootServl ...
- crm操作约会实体
using System; using Microsoft.Xrm.Sdk; using Microsoft.Crm.Sdk.Messages; using Microsoft ...
- HDU 2108 Shape of HDU (判断是不是凸多边形 叉乘)
Shape of HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- 【前端】怎样成长为一名优秀的前端project师---
浅谈本人的经验.也算是与大家交流吧,本人眼下也是从事前端的工作,时间并不长,说的不好,请见谅. 首先,前端project师必须得掌握HTML.CSS和JavaScript. 仅仅懂当中一个或两个还不行 ...
- VS2013带来的"新特性"
VS2013除了引入"Bootstrap"库以外,最大的变化就是.net Framework 4.5下面的都不支持了.也就是说,假设不把.net库升级成.net Framework ...
- Apache Qpid CPP的编译与安装
单机Broker部署(windows/linux) 在Windows/Linux上部署QPID Broker的方法. Windows 需要预先准备的文件和程序 qpid-cpp-0.32.tar.gz ...