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 ...
随机推荐
- javadoc中{@link}与@see的简单使用以及区别
因为公司的测试直接对着java api进行外部服务的测试,故最近对javadoc的要求从0变成了严重依赖,有些时候在javadoc不得不引用其他class,以便更加清晰的说明上下文以及含义,发现指向别 ...
- .net下安装 ZooKeeper.Net
PM> Install-Package ZooKeeper.Net正在尝试解析依赖项“log4net (≥ 1.2.10)”.正在安装“log4net 1.2.10”.已成功安装“log4net ...
- Python学习基础(二)——集合 深浅拷贝 函数
集合 # 集合 ''' 集合是无序不重复的 ''' # 创建列表 l = list((1, 1, 1)) l1 = [1, 1, 1] print(l) print(l1) print("* ...
- ssh连接出现 WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
ssh远程连接的时候出现 WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!连接失败 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ...
- cJSON库的简单介绍及使用
转载:http://www.cnblogs.com/liunianshiwei/p/6087596.html JSON 语法是 JavaScript 对象表示法语法的子集.数据在键/值对中:数据由逗号 ...
- 解决跨域(CORS)问题
为什么会有跨域问题 是因为浏览器的同源策略是对ajax请求进行阻拦了,但是不是所有的请求都给做跨域,像是一般的href属性,a标签什么的都不拦截 解决跨域问题的两种方式 JSONP 推荐参考 CO ...
- 1-1.flutter学习笔记(一)git入门(msysgit图文安装)
1.下载git-for-windows (1)常用的GitHub客户端msysgit,也就是git-for-windows. (2)登录官网 https://git-for-windows.githu ...
- Manjaro 系统添加国内源和安装搜狗输入法
添加中科大源 #打开配置文件 kate /etc/pacman.conf 在文件末尾添加 [archlinuxcn] SigLevel = Optional TrustedOnly Server = ...
- 关于link标签的用法, 不声明rel=stylesheet则无效? 在ff中必须声明rel属性!
void 无效的, 空的; invalid: 无效的, void 和 invalid 在表示无效的时候, 是一样的, 等同的 the treaty (条约) was declared invalid ...
- sublime使用技巧总结
软件右下角可以选择文档语法模式 Ctrl + Shift + P ------------------命令模式sshtml模糊匹配-----语法切换到html模式,同理所得,ss+相应文件名匹配相应模 ...