题意:

给出两棵树,每棵树的节点都有一个权值。

同一棵树上的节点的权值互不相同,不同树上节点的权值可以相同。

要求回答如下询问:

  • \(u_1 \, v_1 \, u_2 \, v_2\):询问第一棵树的路径\(u_1 \to v_1\)的节点权值 与 第一棵树的路径\(u_2 \to v_2\)的节点权值的交集的大小。

分析:

通用的思路是这样的:首先解决线性区间上的问题,然后用树链剖分来将树上问题转化为线性问题。

考虑两个序列之间求交集:给出两个序列\(S_1, S_2\),\(S_1\)有\(n_1\)个元素\(a_1, a_2, \cdots , a_{n_1}\),\(S_2\)有\(n_2\)个元素\(b_1, b_2, \cdots , b_{n_2}\)。

每次询问\(S_1\)的子区间\([l_1,r_1]\)和\(S_2\)的子区间\(l_2,r_2\)的交集的大小。

首先定义一个函数\(f\)把\(a_1 \sim a_{n_1}\)映射为\(1 \sim n_1\)

同样地,如果\(b_i\)在\(S_1\)中出现另\(b_i=f(b_i)\),否则另\(b_i=0\)

这样就将问题转化为求\(S_2\)的子区间\([l_2,r_2]\)中值在\([l_1, r_1]\)范围中元素的个数。

因此可以用线段树来解决。

回到本问题,先把第一棵树剖分,路径\(u_1 \to v_1\)就变成若干个连续的区间。

再对第二棵树建一棵主席树,维护的是根节点到当前节点对应区间的元素的个数。

对于每个区间,查询一次在这个区间内路径\(u_2 \to v_2\)上在这个区间内的元素的个数。

处理每次询问的复杂度为\(O(log^2n)\)

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#define MP make_pair
#define F first
#define S second
using namespace std; typedef pair<int, int> PII;
const int maxn = 100000 + 10; struct Edge
{
int v, nxt;
Edge() {}
Edge(int v, int nxt): v(v), nxt(nxt) {}
}; struct Tree
{
int n, w[maxn];
int ecnt, head[maxn];
Edge edges[maxn];
int fa[maxn], dep[maxn], sz[maxn], son[maxn]; void init() { ecnt = 0; memset(head, -1, sizeof(head)); } void AddEdge(int u, int v) {
edges[ecnt] = Edge(v, head[u]);
head[u] = ecnt++;
} bool read() {
if(scanf("%d", &n) != 1) return false;
init();
for(int u = 2; u <= n; u++) {
scanf("%d", fa + u);
AddEdge(fa[u], u);
}
for(int i = 1; i <= n; i++) scanf("%d", w + i);
return true;
} void dfs(int u) {
sz[u] = 1; son[u] = 0;
for(int i = head[u]; ~i; i = edges[i].nxt) {
int v = edges[i].v;
dep[v] = dep[u] + 1;
dfs(v);
sz[u] += sz[v];
if(sz[v] > sz[son[u]]) son[u] = v;
}
}
}; Tree t1, t2; int n1, n2;
int x[maxn]; //Heavy Light Decomposition
int tot;
int id[maxn], pos[maxn], top[maxn]; void dfs2(int u, int tp) {
id[u] = ++tot;
int p = lower_bound(x + 1, x + 1 + n1, t1.w[u]) - x;
pos[p] = tot;
top[u] = tp;
if(!t1.son[u]) return;
dfs2(t1.son[u], tp);
for(int i = t1.head[u]; ~i; i = t1.edges[i].nxt) {
int v = t1.edges[i].v;
if(v == t1.son[u]) continue;
dfs2(v, v);
}
} vector<PII> inter; void getIntervals(int u, int v) {
inter.clear();
while(top[u] != top[v]) {
if(t1.dep[top[u]] < t1.dep[top[v]]) swap(u, v);
inter.push_back(MP(id[top[u]], id[u]));
u = t1.fa[top[u]];
}
if(t1.dep[u] > t1.dep[v]) swap(u, v);
inter.push_back(MP(id[u], id[v]));
} //Least Common Ancestor
int anc[maxn][20]; void preprocess() {
memset(anc, 0, sizeof(anc));
for(int i = 1; i <= n2; i++) anc[i][0] = t2.fa[i];
for(int j = 1; (1 << j) < n2; j++)
for(int i = 1; i <= n2; i++) if(anc[i][j-1])
anc[i][j] = anc[anc[i][j-1]][j-1];
} int LCA(int u, int v) {
int log;
if(t2.dep[u] < t2.dep[v]) swap(u, v);
for(log = 0; (1 << log) < t2.dep[u]; log++);
for(int i = log; i >= 0; i--)
if(t2.dep[u] - (1<<i) >= t2.dep[v]) u = anc[u][i];
if(u == v) return u;
for(int i = log; i >= 0; i--)
if(anc[u][i] && anc[u][i] != anc[v][i])
u = anc[u][i], v = anc[v][i];
return t2.fa[u];
} //Functional Segment Tree
const int maxnode = maxn << 5; int sz, root[maxn];
int lch[maxnode], rch[maxnode], sum[maxnode]; int update(int pre, int L, int R, int p) {
int rt = ++sz;
sum[rt] = sum[pre] + 1;
if(L < R) {
int M = (L + R) / 2;
if(p <= M) { rch[rt] = rch[pre]; lch[rt] = update(lch[pre], L, M, p); }
else { lch[rt] = lch[pre]; rch[rt] = update(rch[pre], M+1, R, p); }
}
return rt;
} void build(int u, int p) {
if(!t2.w[u]) root[u] = root[p];
else root[u] = update(root[p], 1, n1, t2.w[u]);
for(int i = t2.head[u]; ~i; i = t2.edges[i].nxt) {
int v = t2.edges[i].v;
build(v, u);
}
} int query(int u, int v, int lca, int L, int R, int qL, int qR) {
if(qL <= L && R <= qR) { return sum[u] + sum[v] - sum[lca] * 2; }
int ans = 0;
int M = (L + R) / 2;
if(qL <= M) ans += query(lch[u], lch[v], lch[lca], L, M, qL, qR);
if(qR > M) ans += query(rch[u], rch[v], rch[lca], M+1, R, qL, qR);
return ans;
} int main()
{
while(t1.read()) {
t2.read();
n1 = t1.n; n2 = t2.n;
for(int i = 1; i <= n1; i++) x[i] = t1.w[i];
sort(x + 1, x + 1 + n1); t1.dfs(1); t2.dfs(1);
preprocess();
tot = 0; dfs2(1, 1); for(int i = 1; i <= n2; i++) {
int p = lower_bound(x + 1, x + 1 + n1, t2.w[i]) - x;
if(p < 1 || p > n2 || x[p] != t2.w[i]) t2.w[i] = 0;
else t2.w[i] = pos[p];
} sz = 1;
build(1, 0); int q; scanf("%d", &q);
while(q--) {
int u1, v1, u2, v2;
scanf("%d%d%d%d", &u1, &v1, &u2, &v2);
getIntervals(u1, v1);
int ans = 0;
int lca = LCA(u2, v2);
for(PII a : inter) {
if(a.F <= t2.w[lca] && t2.w[lca] <= a.S) ans++;
ans += query(root[u2], root[v2], root[lca], 1, n1, a.F, a.S);
}
printf("%d\n", ans);
}
} return 0;
}

HDU 5111 Alexandra and Two Trees 树链剖分 + 主席树的更多相关文章

  1. dfs序+主席树 或者 树链剖分+主席树(没写) 或者 线段树套线段树 或者 线段树套splay 或者 线段树套树状数组 bzoj 4448

    4448: [Scoi2015]情报传递 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 588  Solved: 308[Submit][Status ...

  2. Codechef FIBTREE 树链剖分 主席树 LCA 二次剩余 快速幂

    原文链接https://www.cnblogs.com/zhouzhendong/p/CC-FIBTREE.html 题目传送门 - CC-FIBTREE 题意 给定一个有 $n$ 个节点,初始点权都 ...

  3. BZOJ1146 [CTSC2008]网络管理Network 树链剖分 主席树 树状数组

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1146 题意概括 在一棵树上,每一个点一个权值. 有两种操作: 1.单点修改 2.询问两点之间的树链 ...

  4. bzoj 4448 [Scoi2015]情报传递 (树链剖分+主席树)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=4448 题面: Description 奈特公司是一个巨大的情报公司,它有着庞大的情报网络 ...

  5. BZOJ 4448: [Scoi2015]情报传递 树链剖分 主席树

    4448: [Scoi2015]情报传递 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4448 Description 奈特公司是一个巨 ...

  6. [GDOI2016][树链剖分+主席树]疯狂动物城

    题面 Description Nick 是只在动物城以坑蒙拐骗为生的狐狸,儿时受到偏见的伤害,放弃了自己的理想.他被兔子 Judy 设下圈套,被迫与她合作查案,而卷入意想不到的阴谋,历尽艰险后成为搭档 ...

  7. BZOJ3531 SDOI2014 旅行 - 树链剖分,主席树

    题意:给定一棵树,树上每个点有权值和类型.支持:修改某个点的类型:修改某个点的权值:询问某条链上某个类型的点的和/最大值.点数/类型数/询问数<=100000. 分析: 树链剖分,对每个类型的点 ...

  8. 5.15 牛客挑战赛40 E 小V和gcd树 树链剖分 主席树 树状数组 根号分治

    LINK:小V和gcd树 时限是8s 所以当时好多nq的暴力都能跑过. 考虑每次询问暴力 跳父亲 这样是nq的 4e8左右 随便过. 不过每次跳到某个点的时候需要得到边权 如果直接暴力gcd的话 nq ...

  9. BZOJ4012 HNOI2015开店(树链剖分+主席树)

    考虑这样一个问题:一棵树初始全是白点,有两种操作:把一个点染黑:询问某点到所有黑点的距离之和. 注意到树上两点x和y的距离为depth[x]+depth[y]-depth[lca(x,y)]*2.要求 ...

随机推荐

  1. AJPFX关于JDK,JRE,JVM的区别与联系

    很多朋友可能跟我一样,对JDK,JRE,JVM这三者的联系与区别,一直都是模模糊糊的. 今天我们来整理下三者的关系. JDK : Java Development ToolKit(Java开发工具包) ...

  2. springboot集成shiro实现身份认证

    github地址:https://github.com/peterowang/shiro pom文件 <dependencies> <dependency> <group ...

  3. 外观模式及php实现

    外观模式: 外观模式(Facade Pattern):外部与一个子系统的通信必须通过一个统一的外观对象进行,为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更 ...

  4. 字典(dict),增删改查,嵌套

    一丶字典 dict 用{}来表示  键值对数据  {key:value}  唯一性 键 都必须是可哈希的 不可变的数据类型就可以当做字典中的键 值 没有任何限制 二丶字典的增删改查 1.增 dic[k ...

  5. Google Chrome 浏览器的备用(离线)安装程序

    Google Chrome 浏览器的备用(离线)安装程序(适用于 Windows) 如果您在使用 http://www.google.com/chrome 上的标准安装程序下载 Chrome 浏览器时 ...

  6. JFinal-美女图爬虫-一个不正经的爬虫代码

    去年我做了一个项目,大量使用爬虫抓取数据,使用JFinal+JSoup组合,抓取数据,数据清洗筛选,最终保存到数据库里,结构化. 今天,我发布一个不正经的爬虫项目,如果你对JSoup做爬虫感兴趣,可以 ...

  7. Nginx和Apache服务器上配置反向代理

    在实际项目过程中,由于网站要用到一个在线编辑器(个性化的在线编辑软件),需要跨域进行通信!由于跨域通信较多,所以当时就想到在网站服务器上代理编辑软件的请求! 这就是“反向代理”的实际需求! 一.Ngi ...

  8. ADO.Net——防止SQL注入攻击

    规避SQL注入 如果不规避,在黑窗口里面输入内容时利用拼接语句可以对数据进行攻击 如:输入Code值 p001' union select * from Info where '1'='1 //这样可 ...

  9. [Python] - 使用chardet检查网页编码格式时发现的问题

    最近在使用chardet检查网页编码格式时发现如下问题: 用urllib打开网页再检查编码格式和用urllib2打开网页检查编码格式结果不一样,所以urllib2打开可能导致问题,需要关注. 查看了相 ...

  10. jsp四大作用域之Session

    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...