题目大意:给定一个 N 个点的森林,M 个询问,每次询问对于点 u 来说,有多少个点和 u 有相同的 K 级祖先。

题解:线段树合并适合处理子树贡献的问题。

发现要回答这个询问在点 u 处计算很困难,但是在 u 的 k 级祖先处处理询问很简单,即:问对于 v 子树中深度为 k 的节点的个数。因此,采用将询问离线,对于每个点的询问转化到其祖先节点,最后采用线段树合并即可。

代码如下

#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
using namespace std;
const int maxn=1e5+10; int n,m,ans[maxn];
vector<int> G[maxn];
int f[maxn][21],dep[maxn];
vector<pair<int,int>> q[maxn]; // dep, id void pre(int u,int fa){
dep[u]=dep[fa]+1,f[u][0]=fa;
for(int i=1;i<=20;i++)f[u][i]=f[f[u][i-1]][i-1];
for(auto v:G[u])pre(v,u);
}
int LA(int x,int k){
int t=dep[x]-k;
for(int i=20;~i;i--)if(dep[f[x][i]]>=t)x=f[x][i];
return x;
} struct node{
#define ls(o) t[o].lc
#define rs(o) t[o].rc
int lc,rc,sz;
}t[maxn*20];
int tot,root[maxn];
inline void pushup(int o){
t[o].sz=t[ls(o)].sz+t[rs(o)].sz;
}
void insert(int &o,int l,int r,int pos){
if(!o)o=++tot;
if(l==r){++t[o].sz;return;}
int mid=l+r>>1;
if(pos<=mid)insert(ls(o),l,mid,pos);
else insert(rs(o),mid+1,r,pos);
pushup(o);
}
int merge(int x,int y,int l,int r){
if(!x||!y)return x+y;
if(l==r){t[x].sz+=t[y].sz;return x;}
int mid=l+r>>1;
ls(x)=merge(ls(x),ls(y),l,mid);
rs(x)=merge(rs(x),rs(y),mid+1,r);
return pushup(x),x;
}
int query(int o,int l,int r,int pos){
if(!o)return 0;
if(l==r)return t[o].sz;
int mid=l+r>>1;
if(pos<=mid)return query(ls(o),l,mid,pos);
else return query(rs(o),mid+1,r,pos);
} void dfs(int u){
for(auto v:G[u]){
dfs(v);
root[u]=merge(root[u],root[v],1,n);
}
for(auto p:q[u]){
int d=p.first,id=p.second;
ans[id]=query(root[u],1,n,d)-1;
}
insert(root[u],1,n,dep[u]);
}
void read_and_parse(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&f[i][0]);
if(!f[i][0])continue;
G[f[i][0]].pb(i);
}
for(int i=1;i<=n;i++)if(!f[i][0])pre(i,0);
scanf("%d",&m);
for(int i=1;i<=m;i++){
int v,p;
scanf("%d%d",&v,&p);
int u=LA(v,p);
if(!u)continue;
q[u].pb(mp(dep[v],i));
}
}
void solve(){
for(int i=1;i<=n;i++)if(!f[i][0])dfs(i);
for(int i=1;i<=m;i++)printf("%d ",ans[i]);
}
int main(){
read_and_parse();
solve();
return 0;
}

【CF208E】Blood Cousins的更多相关文章

  1. 【Leetcode_easy】993. Cousins in Binary Tree

    problem 993. Cousins in Binary Tree 参考 1. Leetcode_easy_993. Cousins in Binary Tree; 完

  2. 【leetcode】993. Cousins in Binary Tree

    题目如下: In a binary tree, the root node is at depth 0, and children of each depth k node are at depth  ...

  3. 【LeetCode】993. Cousins in Binary Tree 解题报告(C++ & python)

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

  4. CF208E Blood Cousins

    Blood Cousins 题目描述 小C喜欢研究族谱,这一天小C拿到了一整张族谱. 小C先要定义一下k-祖先. x的1-祖先指的是x的父亲 x的k-祖先指的是x的(k-1)-祖先的父亲 小C接下来要 ...

  5. 【转】Python 面向对象(初级篇)

    [转]Python 面向对象(初级篇) 51CTO同步发布地址:http://3060674.blog.51cto.com/3050674/1689163 概述 面向过程:根据业务逻辑从上到下写垒代码 ...

  6. 蓝牙Bluetooth技术手册规范下载【转】

    蓝牙Bluetooth技术手册规范下载 http://www.crifan.com/summary_bluetooth_specification_download/ [背景] 之前就已经整理和转帖了 ...

  7. 【237】◀▶IEW-Unit02

    Unit 2 Sport I.状语从句在雅思写作中的运用 公式: 主句+状语从句连接词+从句 =状语从句连接词+从句,主句 1. 时间状语从句 I. when, while, as 1. When+A ...

  8. 【C++】从零开始的CS:GO逆向分析3——写出一个透视

    [C++]从零开始的CS:GO逆向分析3--写出一个透视 本篇内容包括: 1. 透视实现的方法介绍 2. 通过进程名获取进程id和进程句柄 3. 通过进程id获取进程中的模块信息(模块大小,模块地址, ...

  9. Python高手之路【六】python基础之字符串格式化

    Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...

随机推荐

  1. Vue路由的跳转方式

    Vue中路由的跳转方式 一.<router-link to=''></router-link> Header.vue <template> <header&g ...

  2. Blender建模与游戏换装(转载文)

    本文转载自https://my.oschina.net/huliqing/blog/880113?hmsr=toutiao.io 如果本文涉及侵权行为,请原作者联系博主邮箱,我将及时进行删除处理 博主 ...

  3. 更改默认浏览器(Windows7)

    更改默认浏览器 第一个方法(最好用): 第一步,先点击左下角WIN 然后点默认程序(画框框的图上) 第二步骤,来到控制面板主页,点击设置程序 第三步骤,左键单击选择要设置为默认浏览器的程序(我用360 ...

  4. python r r+;w w+;a a+;以及加不加b区别

    #以下内容均在正常打开文件的情况下运行 一.列表格 模式 可做操作 若文件不存在 是否覆盖 r 只能读 报错 --- r+ 可读可写 报错 是 w 只能写 创建 是 w+ 可读可写 创建 是 a 只能 ...

  5. 使用 docsify 創建自己的 markdown 文檔系統

    先來看一下我在碼雲上創建的demo: http://lin1270.gitee.io/nicedoc/#/ GIT自己clone一下: https://gitee.com/lin1270/nicedo ...

  6. Asteroid Collision

    We are given an array asteroids of integers representing asteroids in a row. For each asteroid, the ...

  7. windows terminal编译实录

    直接甩个大佬链接吧 https://www.bilibili.com/video/av52032233?t=835 安装过程中如果出问题了,靠搜索引擎解决下,微软或者vs的问题可以用biying搜索 ...

  8. Docker 镜像与容器管理

    镜像与容器简介 Docker的大部分操作都围绕着它的三大核心概念:镜像.容器.仓库而展开.因此,准确把握这三大核心概念对于掌握Docker技术尤为重要,在docker中,我们重点关注的就是镜像和容器了 ...

  9. CentOS7部署Tomcat服务器

    1. 软件 存放路径:/usr/local/src apache-tomcat-9.0.22.tar.gz openjdk-12_linux-x64_bin.tar.gz 2.事先配置 启动后关闭防火 ...

  10. Eclipse使用github并开启命令行

    1. 安装EGit插件 2. 导入git项目 选择Import: 选择“Clone URI” 输入想要导入的git项目地址和用户名密码: 选择代码分支: 一路点击next完成导入github项目即可. ...