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 ...
随机推荐
- runtime.Gosched()的作用分析
untime.Gosched()用于让出CPU时间片.这就像跑接力赛,A跑了一会碰到代码runtime.Gosched()就把接力棒交给B了,A歇着了,B继续跑 测试代码: package main ...
- 【题解】Luogu P4391 [BOI2009]Radio Transmission 无线传输
原题传送门 这题需要用到kmp匹配 推导发现: 设循环节的长度为x,那么kmp数组前x个都是0,后面kmp[x+n]=n 先求出kmp数组 答案实际就是len-kmp[len] #include &l ...
- linux判断日志文件大小进行清理
脚本写了一个死循环,根据nohup产生的日志多大, 这里表示日志超过500M之后清理, 具体数字可自定义 睡眠数可自定义 #!/usr/bin/bash while true do s=`du -k ...
- C# 发送16进制串口数据
一个困扰两天的问题:需要通过串口向设备发送的数据:0A010 7e 08 00 11 00 00 7e 76 7f我先将每个16进制字符转换成10进制,再将其转换成ASCII码对应的字符. /// & ...
- QT5下的caffe项目属性
TEMPLATE = app CONFIG += console c++11 CONFIG -= app_bundle CONFIG -= qt SOURCES += /home/aimhabo/ca ...
- topcoder srm 693 div1 -3
1.给出一个$n$个顶点的无向带权图.其中顶点$i,i+1$之间存在边,$i,i+2$之间存在边.而且仅有这些边.现在删掉其中的一些边,剩下的边满足图仍然是2联通的情况下使得权值和最小? 思路:其实就 ...
- was清除项目缓存数据
步骤 彻底清理删除后的项目缓存 删除${WAS_HOME}\AppServer\profiles\AppSrv01\config\cells\xxxxxNode01Cell/applications/ ...
- cygwin下如何编译安装minicom?
1. 安装依赖的软件和库 apt-cyg install autoconf automake make libncurses-devel (apt-cyg工具的安装方法在此) 2. 获取源码 wget ...
- 设计模式总结(转自CS-Notes)
转载地址:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F ...
- size_t和unsigned int区别
size_t和unsigned int有所不同,size_t的取值range是目标平台下最大可能的数组尺寸,一些平台下size_t的范围小于int的正数范围,又或者大于unsigned int.最典型 ...