HRBUST - 2358

思路:dfs序 + 树状数组

代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<pii, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head const int N = 1e5 + ;
vector<int>g[N];
int anc[][N], deep[N], l[N], r[N], bit[N], n, now = ;
bool vis[N];
void add(int x, int a) {
while(x <= n) bit[x] += a, x += x&-x;
}
int query(int x) {
int ans = ;
while(x) ans += bit[x], x -= x&-x;
return ans;
}
void dfs(int u) {
l[u] = now;
for (int v : g[u]) {
anc[][v] = u;
for (int i = ; i < ; i++) anc[i][v] = anc[i-][anc[i-][v]];
now++;
deep[v] = deep[u] + ;
dfs(v);
}
r[u] = now;
}
int lca(int u, int v) {
if(deep[u] < deep[v]) swap(u, v);
for (int i = ; i >= ; i--) if(deep[anc[i][u]] >= deep[v]) u = anc[i][u];
if(u == v) return u;
for (int i = ; i >= ; i--) if(anc[i][u] != anc[i][v]) u = anc[i][u], v = anc[i][v];
return anc[][u];
}
int dis(int u, int v) {
int a = lca(u, v);
return deep[u] - query(l[u]) + deep[v] - query(l[v]) - *(deep[a] - query(l[a]));
}
int main() {
int T, q, p, ty, x, y;
for (int i = ; i < ; i++) anc[i][] = ;
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
for (int i = ; i <= n; i++) deep[i] = , g[i].clear(), vis[i] = false;
for (int i = ; i <= n; i++) {
scanf("%d", &p);
g[p].pb(i);
}
now = ;
dfs();
scanf("%d", &q);
for (int i = ; i <= n; i++) bit[i] = ;
while(q--) {
scanf("%d", &ty);
if(ty == ) {
scanf("%d %d", &x, &y);
if(vis[x] || vis[y]) printf("-1\n");
else printf("%d\n", dis(x, y));
}
else {
scanf("%d", &x);
if(!vis[x]) {
vis[x] = true;
add(l[x], );
add(r[x]+, -);
}
}
}
}
return ;
}

HRBUST - 2358 Magic network的更多相关文章

  1. (转)The Neural Network Zoo

    转自:http://www.asimovinstitute.org/neural-network-zoo/ THE NEURAL NETWORK ZOO POSTED ON SEPTEMBER 14, ...

  2. (转)LSTM NEURAL NETWORK FOR TIME SERIES PREDICTION

    LSTM NEURAL NETWORK FOR TIME SERIES PREDICTION Wed 21st Dec 2016   Neural Networks these days are th ...

  3. MAGIC XPA最新版本Magic xpa 2.4c Release Notes

    New Features, Feature Enhancements and Behavior ChangesSubforms – Behavior Change for Unsupported Ta ...

  4. Magic Quadrant for Security Information and Event Management

    https://www.gartner.com/doc/reprints?id=1-4LC8PAW&ct=171130&st=sb Summary Security and risk ...

  5. 网络远程唤醒 WOL Magic Packet

    Magic Packet Magic Packet白皮书介绍: The basic technical details of Magic Packet Technologyare simple and ...

  6. (zhuan) LSTM Neural Network for Time Series Prediction

    LSTM Neural Network for Time Series Prediction Wed 21st Dec 2016 Neural Networks these days are the ...

  7. 变色龙启动MAC时,错误信息“ntfs_fixup: magic doesn't match:”的解决办法

    如下是变色龙启动的bdmesg,解决办法就是用mac的磁盘管理器,对ntfs分区进行检验修复.需要安装ntfs的驱动支持. 实在不行,就删除调整过大小的分区,重新用Windows的磁盘管理器重新分区. ...

  8. Network Booting

    http://en.wikipedia.org/wiki/Network_booting Network booting Network booting is the process of booti ...

  9. Google Deepmind AI tries it hand at creating Hearthstone and Magic: The Gathering cards

    http://www.techrepublic.com/article/google-deepmind-ai-tries-it-hand-at-creating-hearthstone-magic-t ...

随机推荐

  1. Golang对文件读写操作

    package main import ( "bufio" "fmt" "io" "os" ) //写 func Wri ...

  2. Golang中map的三种声明方式和简单实现增删改查

    package main import ( "fmt" ) func main() { test3 := map[string]string{ "one": & ...

  3. Python爬虫(二)——豆瓣图书决策树构建

    前文参考:  https://www.cnblogs.com/LexMoon/p/douban1.html Matplotlib绘制决策树代码: # coding=utf-8 import matpl ...

  4. 01:MongoDB基础

    1.1 MongoDB简介 1.特点 1. MongoDB的提供了一个面向文档存储,操作起来比较简单和容易. 2. 你可以在MongoDB记录中设置任何属性的索引 (如:FirstName=" ...

  5. 问题:oracle 12c rac数据库服务器的home目录丢失问题解决2018-06-25 18:30

    问题原因:是由于运维粗心,在缩容/home(此目录下挂载了逻辑卷lv_home)时没有先缩小文件系统(resize2fs)也没有备份,导致home数据损坏,重启时系统无法正常启动 解决方案:跳过此ho ...

  6. opencv学习之路(18)、霍夫变换

    一.简介 在图像处理和计算机视觉领域中,如何从当前的图像中提取所需要的特征信息是图像识别的关键所在.在许多应用场合中需要快速准确地检测出直线或者圆.其中一种非常有效的解决问题的方法是霍夫(Hough) ...

  7. 20145315何佳蕾《网络对抗》web基础

    实验步骤 (1).Web前端HTML(1分) 能正常安装.启停Apache.理解HTML,理解表单,理解GET与POST方法,编写一个含有表单的HTML. (2).Web前端javascipt(1分) ...

  8. Java基础语法(下)

    1.数组动态初始化 //数据类型[] 数组名 = new 数据类型[数组长度]; int[] arr = new int[3]; /* * 左边: * int:说明数组中的元素类型是int类型 * [ ...

  9. ScheduledTheadPool线程池的使用

    ScheduledTheadPool线程池的特点在于可以延迟执行任务,也可以周期性执行任务. 创建线程池 ScheduledExecutorService scheduled = Executors. ...

  10. if语句学习

    #print("您好,我叫周星驰") ''' x=1+2+3 print(x*4) print(x**x) a=input("请输入相应的数字a") a=int ...