洛谷 2633 BZOJ 2588 Spoj 10628. Count on a tree

【题解】
蜜汁强制在线。。。
每个点开一个从它到根的可持久化权值线段树。查询的时候利用差分的思想在树上左右横跳就好了。
#include<cstdio>
#include<algorithm>
#define N 100010
#define rg register
#define ls (a[u].l)
#define rs (a[u].r)
using namespace std;
int n,n2,m,tot,root[N],last[N],dep[N],top[N],hvy[N],fa[N],size[N],val[N],b[N];
struct tree{
int sum,l,r;
}a[N*];
struct edge{
int to,pre;
}e[N<<];
inline int read(){
int k=,f=; char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(''<=c&&c<='')k=k*+c-'',c=getchar();
return k*f;
}
inline int qlca(int x,int y){
int f1=top[x],f2=top[y];
while(f1!=f2){
if(dep[f1]<dep[f2]) swap(x,y),swap(f1,f2);
x=fa[f1]; f1=top[x];
}
return dep[x]<dep[y]?x:y;
}
void update(int &u,int l,int r,int pos){
a[++tot]=a[u]; a[u=tot].sum++;
if(l==r) return;
int mid=(l+r)>>;
if(pos<=mid) update(ls,l,mid,pos);
else update(rs,mid+,r,pos);
}
int query(int u,int v,int lca,int f,int l,int r,int k){
if(l==r) return l;
int tmp=a[ls].sum+a[a[v].l].sum-a[a[lca].l].sum-a[a[f].l].sum,mid=(l+r)>>;
return k<=tmp?query(ls,a[v].l,a[lca].l,a[f].l,l,mid,k):query(rs,a[v].r,a[lca].r,a[f].r,mid+,r,k-tmp);
}
void dfs1(int x){
size[x]=; dep[x]=dep[fa[x]]+;
root[x]=root[fa[x]]; update(root[x],,n2,val[x]);
for(rg int i=last[x],to;i;i=e[i].pre)if((to=e[i].to)!=fa[x]){
fa[to]=x; dfs1(to);
size[x]+=size[to];
if(size[to]>size[hvy[x]]) hvy[x]=to;
}
}
void dfs2(int x,int tp){
top[x]=tp;
if(hvy[x]) dfs2(hvy[x],tp);
for(rg int i=last[x],to;i;i=e[i].pre)
if((to=e[i].to)!=fa[x]&&to!=hvy[x]) dfs2(to,to);
}
int main(){
n=read(); m=read();
for(rg int i=;i<=n;i++) val[i]=b[i]=read();
sort(b+,b++n); n2=unique(b+,b++n)-b-;
for(rg int i=;i<=n;i++) val[i]=lower_bound(b+,b++n2,val[i])-b;
for(rg int i=;i<n;i++){
int u=read(),v=read();
e[++tot]=(edge){v,last[u]}; last[u]=tot;
e[++tot]=(edge){u,last[v]}; last[v]=tot;
}
tot=; dfs1(); dfs2(,);
int last=;
while(m--){
int u=read()^last,v=read(),k=read(),l=qlca(u,v),f=fa[l];
printf("%d\n",last=b[query(root[u],root[v],root[l],root[f],,n2,k)]);
}
return ;
}
洛谷 2633 BZOJ 2588 Spoj 10628. Count on a tree的更多相关文章
- 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 + 主席树 )
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 ...
- 主席树 || 可持久化线段树 || 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 主席树+lca
分析:树上第k小,然后我想说的是主席树并不局限于线性表 详细分析请看http://www.cnblogs.com/rausen/p/4006116.html,讲的很好, 然后因为这个熟悉了主席树,真是 ...
- ●BZOJ 2588 Spoj 10628. Count on a tree
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2588 题解: 主席树,在线,(求LCA)感觉主席树真的好厉害...在原树上建主席树.即对于原 ...
随机推荐
- gitlab https
https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/install/installation.md#using-https https:// ...
- bzoj 1778: [Usaco2010 Hol]Dotp 驱逐猪猡【dp+高斯消元】
算是比较经典的高斯消元应用了 设f[i]为i点答案,那么dp转移为f[u]=Σf[v]*(1-p/q)/d[v],意思是在u点爆炸可以从与u相连的v点转移过来 然后因为所有f都是未知数,高斯消元即可( ...
- python自动化学习笔记11-自动化测试UTP框架
前面基本的unittest及ddt已经学过了,现在我们系统把这些知识结合起来,写一个简单的UTP自动化测试框架: 我们先来建基础目录,首先新建一个项目,项目下建父目录UTP,conf目录,用来存放配置 ...
- [NOIP2004]火星人
Description 人类终于登上了火星的土地并且见到了神秘的火星人.人类和火星人都无法理解对方的语言,但是我们的科学家发明了一种用数字交流的方法.这种交流方法是这样的,首先,火星人把一个非常大的数 ...
- ViewPager(4)用viewpager实现splash view
1,示例 2,代码 SplashMain.java import android.os.Bundle; import android.support.v4.app.Fragment; import a ...
- springmvc中的web.xml配置(包含中文乱码解决)
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http:// ...
- php 页面展示
php 页面展示 复杂逻辑. 段落注释
- WinForm ListBox 控件用法
下面演示如何利用列表控件 ListBox 实现多选与移动选项: using IMS.WinFormClient.UserControls; using System; using System.Col ...
- 258 Add Digits 各位相加
给一个非负整数 num,反复添加所有的数字,直到结果只有一个数字.例如:设定 num = 38,过程就像: 3 + 8 = 11, 1 + 1 = 2. 由于 2 只有1个数字,所以返回它.进阶:你可 ...
- Hadoop Hive概念学习系列之HiveQL编译基础(十)
由客户端提交的HiveQL语句将最终被转换为一个或多个MapReduce任务并提交由Hadoop执行.不包含聚合和连接的简单SELECT语句可以使用一个单独的只包含Map阶段的任务实现.使用GROUP ...