BZOJ2588:Count on a tree(主席树)
Description
Input
Output
M行,表示每个询问的答案。最后一个询问不输出换行符
Sample Input
105 2 9 3 8 5 7 7
1 2
1 3
1 4
3 5
3 6
3 7
4 8
2 5 1
0 5 2
10 5 3
11 5 4
110 8 2
Sample Output
8
9
105
7
HINT
Solution
一开始看到这个题以为是点分,然后发现没法做。
现在做主席树的时候做到这个题了
就想在DFS序上搞事情……就像链剖一样……然后GG了
偷看了一眼题解的第一行发现用Root[i]表示路径[1,i]情况
然后就没什么思维难度了……
对于路径[u,v]的离散化后的值域情况
我们可以用segt[v]+segt[u]-segt[lca]-segt[father[lca]]来计算
然后就是求第k大的模板了
建树的时候有点小技巧(见代码)
Code
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define N (100000+100)
using namespace std;
struct node{int sum,lson,rson;}Segt[N*];
struct node1{int to,next;}edge[N*];
int a[N],b[N],n,m,Root[N],segt_num,u,v,x,y,k,num,lastans;
int head[N],num_edge,Father[N],f[N][],Depth[N]; void add(int u,int v)
{
edge[++num_edge].to=v;
edge[num_edge].next=head[u];
head[u]=num_edge;
} int Build(int l,int r)
{
int node=++segt_num;
if (l==r) return node;
int mid=(l+r)>>;
Segt[node].lson=Build(l,mid);
Segt[node].rson=Build(mid+,r);
return node;
} int Update(int pre,int l,int r,int x)
{
int node=++segt_num;
Segt[node].sum=Segt[pre].sum+;
Segt[node].lson=Segt[pre].lson;
Segt[node].rson=Segt[pre].rson;
if (l==r) return node;
int mid=(l+r)>>;
if (x<=mid) Segt[node].lson=Update(Segt[node].lson,l,mid,x);
else Segt[node].rson=Update(Segt[node].rson,mid+,r,x);
return node;
} void Dfs1(int x)
{
Depth[x]=Depth[Father[x]]+;
for (int i=head[x];i;i=edge[i].next)
if (edge[i].to!=Father[x])
{
Father[edge[i].to]=f[edge[i].to][]=x;
Dfs1(edge[i].to);
}
} void Dfs2(int x)
{
int t=lower_bound(b+,b+num+,a[x])-b;
Root[x]=Update(Root[Father[x]],,num,t);//因为这里父亲的主席树已经建立好了,所以这个点的主席树就可以建立了
for (int i=head[x];i;i=edge[i].next)
if (edge[i].to!=Father[x])
Dfs2(edge[i].to);
} int Query(int u,int v,int lca,int flca,int l,int r,int k)
{
if (l==r) return b[l];
int mid=(l+r)>>,x=Segt[Segt[u].lson].sum+Segt[Segt[v].lson].sum-Segt[Segt[lca].lson].sum-Segt[Segt[flca].lson].sum;
if (k<=x) return Query(Segt[u].lson,Segt[v].lson,Segt[lca].lson,Segt[flca].lson,l,mid,k);
else return Query(Segt[u].rson,Segt[v].rson,Segt[lca].rson,Segt[flca].rson,mid+,r,k-x);
} int LCA(int x,int y)
{
if (Depth[x]<Depth[y]) swap(x,y);
for (int i=;i>=;--i)
if (Depth[f[x][i]]>=Depth[y])
x=f[x][i];
if (x==y) return y;
for (int i=;i>=;--i)
if (f[x][i]!=f[y][i])
x=f[x][i],y=f[y][i];
return Father[x];
} int main()
{
scanf("%d%d",&n,&m);
for (int i=;i<=n;++i)
scanf("%d",&a[i]),b[i]=a[i];
sort(b+,b+n+);
num=unique(b+,b+n+)-b-;
Root[]=Build(,num);//多一个0点方便处理,作为1的父亲
add(,); add(,);
for (int i=;i<=n-;++i)
{
scanf("%d%d",&u,&v);
add(u,v); add(v,u);
}
Dfs1();//以1为根建树
for (int i=;i<=;++i)
for (int j=;j<=n;++j)
f[j][i]=f[f[j][i-]][i-];
Dfs2();//建立主席树
for (int i=;i<=m;++i)
{
scanf("%d%d%d",&x,&y,&k);
x^=lastans;
int lca=LCA(x,y);
printf("%d\n",lastans=Query(Root[x],Root[y],Root[lca],Root[Father[lca]],,num,k));
}
}
BZOJ2588:Count on a tree(主席树)的更多相关文章
- 洛谷P2633/bzoj2588 Count on a tree (主席树)
洛谷P2633/bzoj2588 Count on a tree 题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K ...
- [bzoj2588][count on a tree] (主席树+lca)
Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...
- 【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】Count on a tree 主席树 + 倍增
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 3749 Solved: 873[ ...
- 【bzoj2588】Count on a tree 主席树
这题给人开了个新思路. 原本构造一个序列的主席树,是这个位置用上个位置的信息来省空间,树上的主席树是继承父亲的信息来省空间. 此题若带修改怎么办? 若对某个点的权值做修改,则这个点的子树都会受影响,想 ...
- spoj cot: Count on a tree 主席树
10628. Count on a tree Problem code: COT You are given a tree with N nodes.The tree nodes are number ...
- 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 ...
- 洛谷P2633 Count on a tree(主席树上树)
题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始为0,即第一个 ...
- SPOJ Count on a tree(主席树+LCA)
一.题目 COT - Count on a tree You are given a tree with N nodes. The tree nodes are numbered from 1 to ...
随机推荐
- 三、hive JavaAPI示例
在上文中https://www.cnblogs.com/lay2017/p/9973370.html 我们通过hive shell去操作hive,本文我们以Java代码的示例去对hive执行加载数据和 ...
- php and js to facebook登陆 最佳实践
Facebook Login Flow & Best Practices Best practice for Facebook login flow with the JavaScript S ...
- java中程序上线报错: tomcat中java.lang.OutOfMemoryError: PermGen space
在程序测试没问题之后,上线试运行,在运行的过程中某个功能一点击就报如下错,然后重启服务器就好了,一会又是如此,解决方法如下(亲测) PermGen space的全称是Permanent Generat ...
- 代码积累-Common
新建Common类库 /// <summary> /// string的扩展 /// </summary> public static class StringExt { // ...
- Idea 快捷键大全【转】
IntelliJ Idea 常用快捷键列表 Ctrl+Shift + Enter,语句完成“!”,否定完成,输入表达式时按 “!”键Ctrl+E,最近的文件Ctrl+Shift+E,最近更改的文件Sh ...
- The parameter to the method is the basic data type
package method.invocation; public class TheParameterToTheMethodIsTheBasicDataType { public static vo ...
- IntelliJ Idea编译报错:javacTask: 源发行版 1.8 需要目标发行版 1.8
解决办法: 1.Project Settings-Modules,选择项目,选择language level 8 2.选中项目,右击选择Maven-->Reimport, 再次编译. 3.Fil ...
- 转载:Windows下三分钟搭建Shadowoscks服务器端
Windows下三分钟搭建Shadowoscks服务器端 之前在V2EX上有人问为啥没人做个在Windows上一键运行Shadowsocks服务器端的程序,我只想说……这是因为没人关注我的libQtS ...
- 使用button的open-type="getUserInfo"引导用户进行授权
https://blog.csdn.net/weixin_39602178/article/details/80295684 一. 前言 小程序官方文档,上面说明 > wx.getUserInf ...
- 企业级Nginx增加日志选项
日志介绍 目的:将用户的访问信息记录到指定的文件中由ngx_http_log_module模块负责 访问日志参数: access_log:指定日志文件的路径和使用何种日志格式记录日志 log_form ...