【CF208E】Blood Cousins
题目大意:给定一个 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的更多相关文章
- 【Leetcode_easy】993. Cousins in Binary Tree
problem 993. Cousins in Binary Tree 参考 1. Leetcode_easy_993. Cousins in Binary Tree; 完
- 【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 ...
- 【LeetCode】993. Cousins in Binary Tree 解题报告(C++ & python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- CF208E Blood Cousins
Blood Cousins 题目描述 小C喜欢研究族谱,这一天小C拿到了一整张族谱. 小C先要定义一下k-祖先. x的1-祖先指的是x的父亲 x的k-祖先指的是x的(k-1)-祖先的父亲 小C接下来要 ...
- 【转】Python 面向对象(初级篇)
[转]Python 面向对象(初级篇) 51CTO同步发布地址:http://3060674.blog.51cto.com/3050674/1689163 概述 面向过程:根据业务逻辑从上到下写垒代码 ...
- 蓝牙Bluetooth技术手册规范下载【转】
蓝牙Bluetooth技术手册规范下载 http://www.crifan.com/summary_bluetooth_specification_download/ [背景] 之前就已经整理和转帖了 ...
- 【237】◀▶IEW-Unit02
Unit 2 Sport I.状语从句在雅思写作中的运用 公式: 主句+状语从句连接词+从句 =状语从句连接词+从句,主句 1. 时间状语从句 I. when, while, as 1. When+A ...
- 【C++】从零开始的CS:GO逆向分析3——写出一个透视
[C++]从零开始的CS:GO逆向分析3--写出一个透视 本篇内容包括: 1. 透视实现的方法介绍 2. 通过进程名获取进程id和进程句柄 3. 通过进程id获取进程中的模块信息(模块大小,模块地址, ...
- Python高手之路【六】python基础之字符串格式化
Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...
随机推荐
- pytorch神经网络层搭建方法
神经网络层的搭建主要是两种方法,一种是使用类(继承torch.nn.Moudle),一种是使用torch.nn.Sequential来快速搭建. 1)首先我们先加载数据: import torchim ...
- 【Python开发】Lambda表达式使用
lambda只是一个表达式,函数体比def简单很多. lambda的主体是一个表达式,而不是一个代码块.仅仅能在lambda表达式中封装有限的逻辑进去. lambda表达式是起到一个函数速写的作用.允 ...
- C学习笔记-typedef
typedef是一种高级数据特性,它能使某一类型创建自己的名字 typedef unsigned char BYTE; typedef struct man MAN; BYTE b = 0x12; 与 ...
- SQL 批量添加的语法
.--添加一条记录 . insert into tableName(col1,col2,col3) values (val1,val2,val3) .--添加多条记录 . insert into ta ...
- 【python基础学习】---解析多层json,解析xml
1.以豆瓣的API接口为例子,解析返回的json数据 https://api.douban.com/v2/book/1220502 { "rating":{ "max&q ...
- npm install 报 128 错误
[问题描述] 项目执行npm install的时候特别慢,到最后直接返回错误: verbose exit [ 1, true ] [解决方法] 执行以下两条命令: git config --globa ...
- mysql下的sqlmode详解
转自:https://www.cnblogs.com/Zender/p/8270833.html 阅读目录 一,sql_mode值的含义 二,ANSI模式 三,STRICT_TRANS_TABLES模 ...
- Ubuntu - Ubuntu应用记录(2)
1.安装Ubuntu16.04的一种分区分案(240G固态硬盘小例) 1.创建boot分区(引导分区)-> 512M ->逻辑分区->空间起始位置->Ext4日志文件系统-&g ...
- JavaRMI框架
RMI(即Remote Method Invoke 远程方法调用).在Java中,只要一个类extends了java.rmi.Remote接口,即可成为存在于服务器端的远程对象,供客户端访问并提供一 ...
- Java 错误:Constructor call must be the first statement in a constructor
今天用学校里的黑马程序员通Java语法 想到了:在有参构造函数中调用无参构造函数 语法是这样的: class Person{ private int age; public Person() { Sy ...