Count on a tree

题目描述

给定一棵\(N\)个节点的树,每个点有一个权值,对于\(M\)个询问\((u,v,k)\),你需要回答\(u\) \(xor\) \(lastans\)和\(v\)这两个节点间第\(K\)小的点权。其中\(lastans\)是上一个询问的答案,初始为\(0\),即第一个询问的u是明文。

输入输出格式

输入格式:

第一行两个整数\(N,M\)。

第二行有\(N\)个整数,其中第\(i\)个整数表示点\(i\)的权值。

后面\(N-1\)行每行两个整数\((x,y)\),表示点\(x\)到点\(y\)有一条边。

最后\(M\)行每行两个整数\((u,v,k)\),表示一组询问。

输出格式:

\(M\)行,表示每个询问的答案。


一看是无修改的第\(k\)值查询,我们可以用可持久化降维。

就是把序列上的第\(k\)值扩展到了树上。

我们考虑一条树上路径可以被怎么表示

这样类比,假设树上每个点有点权,则树上路径点权之和可以被树的前缀和数组这样表示

\(len(u,v)=dis[u]+dis[v]-dis[lca(u,v)]-dis[father(lca[u,v])]\)

然而前缀和其实就是一维的可持久化,我们把\(dis\)数组类比成主席树加加减减就好了


Code:

#include <cstdio>
#include <algorithm>
#define ls ch[now][0]
#define rs ch[now][1]
const int N=100010;
int Next[N<<1],to[N<<1],head[N],cnt;
void add(int u,int v)
{
Next[++cnt]=head[u];to[cnt]=v;head[u]=cnt;
}
int sum[N*25],ch[N*25][2],tot,n,m,n_;
void updata(int now)
{
sum[now]=sum[ls]+sum[rs];
}
int rebuild(int las,int l,int r,int pos)
{
int now=++tot;
if(l==r)
{
sum[now]=sum[las]+1;
return now;
}
int mid=l+r>>1;
if(pos<=mid)
{
ls=rebuild(ch[las][0],l,mid,pos);
rs=ch[las][1];
}
else
{
ls=ch[las][0];
rs=rebuild(ch[las][1],mid+1,r,pos);
}
updata(now);
return now;
}
int ha[N],loc[N],root[N];
int query(int u,int v,int lca,int lcaf,int l,int r,int k)
{
if(l==r) return ha[l];
int s=sum[ch[u][0]]+sum[ch[v][0]]-sum[ch[lca][0]]-sum[ch[lcaf][0]];
int mid=l+r>>1;
if(k<=s) return query(ch[u][0],ch[v][0],ch[lca][0],ch[lcaf][0],l,mid,k);
else return query(ch[u][1],ch[v][1],ch[lca][1],ch[lcaf][1],mid+1,r,k-s);
}
int top[N],dfn[N],f[N],dep[N],ws[N],siz[N],time;
void dfs1(int now)
{
root[now]=rebuild(root[f[now]],1,n,loc[now]);
siz[now]++;
for(int i=head[now];i;i=Next[i])
{
int v=to[i];
if(v!=f[now])
{
f[v]=now;
dep[v]=dep[now]+1;
dfs1(v);
siz[now]+=siz[v];
if(siz[ws[now]]<siz[v])
ws[now]=v;
}
}
}
void dfs2(int now,int anc)
{
dfn[now]=++time;
top[now]=anc;
if(ws[now]) dfs2(ws[now],anc);
for(int i=head[now];i;i=Next[i])
if(!dfn[to[i]])
dfs2(to[i],to[i]);
}
int LCA(int x,int y)
{
while(top[x]!=top[y])
{
if(dep[top[x]]>dep[top[y]])
x=f[top[x]];
else
y=f[top[y]];
}
return dep[x]<dep[y]?x:y;
}
std::pair <int,int > node[N];
void init()
{
scanf("%d%d",&n_,&m);
for(int d,i=1;i<=n_;i++)
{
scanf("%d",&d);
node[i]=std::make_pair(d,i);
}
std::sort(node+1,node+1+n_);
for(int i=1;i<=n_;i++)
{
if(node[i].first!=node[i-1].first) n++;
ha[n]=node[i].first;
loc[node[i].second]=n;
}
for(int u,v,i=1;i<n_;i++)
{
scanf("%d%d",&u,&v);
add(u,v),add(v,u);
}
dfs1(1);
dfs2(1,1);
}
void work()
{
for(int u,v,lca,k,lastans=0,i=1;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&k);
u^=lastans;
lca=LCA(u,v);
printf("%d\n",lastans=query(root[u],root[v],root[lca],root[f[lca]],1,n,k));
}
}
int main()
{
init();
work();
return 0;
}

2018.7.31

bzoj 2588 Count on a tree 解题报告的更多相关文章

  1. BZOJ 2588 Count on a tree (COT) 是持久的段树

    标题效果:两棵树之间的首次查询k大点的权利. 思维:树木覆盖树,事实上,它是正常的树木覆盖了持久段树. 由于使用权值段树可以寻求区间k大,然后应用到持久段树思想,间隔可以做减法.详见代码. CODE: ...

  2. BZOJ.2588.Count on a tree(主席树 静态树上第k小)

    题目链接 /* 序列上的主席树 某点是利用前一个点的根建树 同理 树上的主席树 某个节点可以利用其父节点(is unique)的根建树 排名可以利用树上前缀和求得: 对于(u,v),w=LCA(u,v ...

  3. bzoj 2588 Count on a tree

    Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...

  4. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  5. 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)

    [LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...

  6. 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)

    [LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  7. 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)

    [LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

  8. 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)

    [LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...

随机推荐

  1. Siki_Unity_1-4_C#编程(零基础)

    1-4 C#编程(零基础) 任务1:第一章课程资料 任务2:简介 任务3:安装设置IDE工具 Unity内置IDE:MonoDevelop 推荐Visual Studio 下载/安装 VS Commu ...

  2. 安迪的第一个字典 (Andy's First Dictionary,UVa10815)

    题目描述: #include<iostream> #include<string> #include<set> #include<sstream> us ...

  3. CSP201312-2:ISBN号码

    引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...

  4. JDK源码分析:Short.java

    Short是基本数据类型short的包装类. 1)声明部: public final class Short extends Number implements Comparable<Short ...

  5. centos 6.5 启动时卡在进度条位置无法进入系统解决办法。

    今天公司服务器因突然断电导致phddns 花生壳 启动失败,一直卡在启动进度条页面. 解决办法 1.按F5查看卡在什么位置, 2.查看解决方法:程序卡住的情况下,直接备份资料后,卸载程序重启就可以了. ...

  6. [leetcode-662-Maximum Width of Binary Tree]

    Given a binary tree, write a function to get the maximum width of the given tree. The width of a tre ...

  7. HTML5文档的各个组成部分分类

    <!DOCTYPE html><!--声明文档结构类型--> <html lang="zh-cn"><!--声明文档文字区域--> ...

  8. 衡量失业:失业率(Unemployment Rate)

    定义 劳动力 = 就业人数 + 失业人数 失业率 = (失业人数 / 劳动力) * % 劳动力参与率 = (劳动力 / 成年人口) * %

  9. 二叉搜索树(BST)---python实现

    github:代码实现 本文算法均使用python3实现 1. 二叉搜索树定义   二叉搜索树(Binary Search Tree),又名二叉排序树(Binary Sort Tree).   二叉搜 ...

  10. python爬虫 妹子图片网

    代码如下 #coding=utf-8 import os import re import urllib from time import sleep import requests from lxm ...