HRBUST - 2358 Magic network
思路: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的更多相关文章
- (转)The Neural Network Zoo
转自:http://www.asimovinstitute.org/neural-network-zoo/ THE NEURAL NETWORK ZOO POSTED ON SEPTEMBER 14, ...
- (转)LSTM NEURAL NETWORK FOR TIME SERIES PREDICTION
LSTM NEURAL NETWORK FOR TIME SERIES PREDICTION Wed 21st Dec 2016 Neural Networks these days are th ...
- MAGIC XPA最新版本Magic xpa 2.4c Release Notes
New Features, Feature Enhancements and Behavior ChangesSubforms – Behavior Change for Unsupported Ta ...
- 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 ...
- 网络远程唤醒 WOL Magic Packet
Magic Packet Magic Packet白皮书介绍: The basic technical details of Magic Packet Technologyare simple and ...
- (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 ...
- 变色龙启动MAC时,错误信息“ntfs_fixup: magic doesn't match:”的解决办法
如下是变色龙启动的bdmesg,解决办法就是用mac的磁盘管理器,对ntfs分区进行检验修复.需要安装ntfs的驱动支持. 实在不行,就删除调整过大小的分区,重新用Windows的磁盘管理器重新分区. ...
- Network Booting
http://en.wikipedia.org/wiki/Network_booting Network booting Network booting is the process of booti ...
- 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 ...
随机推荐
- 【题解】Luogu P1648 看守
原题传送门:P1648 看守 这题目让求得的是d维( d <=4 )空间中n个点( 2 <= N <= 1000000 )之间最大的哈曼顿距离 模拟,emm,能拿30分,不错 因为d ...
- node.js 初学(二)—— 搭建注册/登录服务器
node.js 初学(二)—— 搭建注册/登录服务器 理论上来说,代码实现在理论和实际上是一样的.但实际上来说,他们不是 做一个最简单的用户注册登录功能 1.接口定义: 注册:/user?act=re ...
- form 表单中 button 按钮用 return false 阻止默认刷新踩过的一个小坑
今天在写一个button按钮的事件时,明明在点击事件的最后写了 return false,可是点击按钮页面仍然会自动刷新 最后看到刷新前 控制台有一抹红色一闪而过,由于速度很快,之前一直都注意到 后来 ...
- Activity之Serializable
Student.java package cn.itcast.wh08.multiactivity.domain; import java.io.Serializable; public class ...
- 给自己的程序添加BugReport
转载:https://www.easyicon.net/(免费icon) 转载:https://www.codeproject.com/Articles/5260/XCrashReport-Excep ...
- 【MonkeyRunner环境搭建】
一.配置MonkeyRunner环境变量 1.首先下载一个AndroidSDK,在SDK的目录中的tools文件夹中,直接带有MonkeyRunner 2.打开MonkeyRunner的方式: |-- ...
- topcoder srm 689 div1 -3
1.给出一个$2*n$的矩阵,只包含小写字母.重新排列各个元素使得任意两个相邻的元素不相同? 思路:按照每种字符的数量降序排序,然后从多到少依次放每一种.放的时候一上一下交错放置. #include ...
- topcoder srm 706 div1
1.给定一个迷宫,点号表示不可行,井号表示可行.现在可以改变其中的一些井号的位置.问最少改变多少个井号可以使得从左上角到右下角存在路径. 思路:设高为$n$,宽为$m$,若井号的个数$S$小于$n+m ...
- AndroidO Treble架构下Hal进程启动及HIDL服务注册过程【转】
本文转载自:https://blog.csdn.net/yangwen123/article/details/79854267 通过前面对Treble架构的介绍,我们知道,Android Framew ...
- Linux内核中的netlink是什么?
答: 是一种内核与用户应用间的双向数据传输方式,用户态使用传统的socketAPI即可使用netlink提供的功能,但是内核态需要使用专门的api来使用netlink.