题目大意:给定一个 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. 分布式架构-Redis 从入门到精通 完整案例 附源码

    导读 篇幅较长,干货十足,阅读需要花点时间,全部手打出来的字,难免出现错别字,敬请谅解.珍惜原创,转载请注明出处,谢谢~! NoSql介绍与Redis介绍 什么是Redis? Redis是用C语言开发 ...

  2. 一篇文章搞懂python2、3编码

    说在前边: 编码问题一直困扰着每一个程序员的编程之路,如果不将它彻底搞清楚,那么你的的这条路一定会走的格外艰辛,尤其是针对使用python的程序员来说,这一问题更加显著, 因为python有两个版本, ...

  3. mac sudo异常

    dsenableroot -d -u <your_admin_username> -p <your_password>  

  4. Python操作 RabbitMQ、Redis、Memcache

    Python操作 RabbitMQ.Redis.Memcache Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数 ...

  5. PLSQL中查到的数据和程序中查询到的不一样

    1.首先看下你的修改或者新增的SQL是否提交.

  6. C++练习 | 模板与泛式编程练习(2)

    #include <iostream> #include <cmath> #include <cstring> #include <string> #i ...

  7. xargs、chattr命令

    一.xargs:将标准输入转化成命令行参数 用法:xargs [OPTION] ... COMMAND INITIAL-ARGS ...使用参数INITIAL-ARGS运行COMMAND,并从输入中读 ...

  8. DataX操作指南

    1.DataX介绍 DataX DataX 是阿里巴巴集团内被广泛使用的离线数据同步工具/平台,实现包括 MySQL.Oracle.SqlServer.Postgre.HDFS.Hive.ADS.HB ...

  9. C# 面向对象6 之前的复习

    复习练习 THIS:调用当前类的构造函数

  10. SQLServer 存储过程详解

    Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用.当存储过程执行一次后,可以将语句缓存中,这样下次执行的时候直接使用缓存中的语句.这样就可以提高存储过程的性能. Ø ...