[Usaco2018 Feb] New Barns
[题目链接]
https://www.lydsy.com/JudgeOnline/problem.php?id=5192
[算法]
维护树的直径,在树上离一个点最远的点一定是一条直径的端点。
在直径为(x , y)的树上加入一个叶子结点z,则新的直径必然为(x , y) , (x , z) , (y , z)中的一条 , 问题转化为询问树上两点距离 , 倍增即可 , 时间复杂度 :O(MlogN)
[代码]
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + ;
const int MAXLOG = ; int n , total , k;
int depth[MAXN] , x[MAXN] , y[MAXN] , belong[MAXN];
int anc[MAXN][MAXLOG];
char op[]; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
inline int lca(int u,int v)
{
if (depth[u] > depth[v]) swap(u , v);
for (int i = MAXLOG - ; i >= ; i--)
{
if (depth[anc[v][i]] >= depth[u])
v = anc[v][i];
}
if (u == v) return u;
for (int i = MAXLOG - ; i >= ; i--)
{
if (anc[u][i] != anc[v][i])
u = anc[u][i] , v = anc[v][i];
}
return anc[u][];
}
inline int dist(int x,int y)
{
return depth[x] + depth[y] - * depth[lca(x , y)];
}
inline void update(int u)
{
int tmp = belong[u];
int nx , ny , nd;
if (dist(u , x[tmp]) > dist(u , y[tmp]))
{
nd = dist(u , x[tmp]);
nx = u;
ny = x[tmp];
} else
{
nd = dist(u , y[tmp]);
nx = u;
ny = y[tmp];
}
if (nd > dist(x[tmp] , y[tmp]))
{
x[tmp] = nx;
y[tmp] = ny;
}
}
inline int query(int k)
{
int tmp = belong[k];
return max(dist(k , x[tmp]) , dist(k , y[tmp]));
} int main()
{ int Q;
scanf("%d",&Q);
while (Q--)
{
scanf("%s%d",&op , &k);
if (op[] == 'B')
{
++n;
if (k == -)
{
belong[n] = ++total;
anc[n][] = ;
depth[n] = ;
x[total] = y[total] = n;
} else
{
depth[n] = depth[k] + ;
anc[n][] = k;
for (int i = ; i < MAXLOG; i++)
anc[n][i] = anc[anc[n][i - ]][i - ];
belong[n] = belong[k];
}
update(n);
} else printf("%d\n",query(k));
} return ; }
[Usaco2018 Feb] New Barns的更多相关文章
- bzoj5192: [Usaco2018 Feb]New Barns
不想写看zory大佬 #include<cstdio> #include<iostream> #include<cstring> #include<cstdl ...
- BZOJ5196: [Usaco2018 Feb]Taming the Herd(DP暴力)
5196: [Usaco2018 Feb]Taming the Herd Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 78 Solved: 71[ ...
- BZOJ5194: [Usaco2018 Feb]Snow Boots(排序&set)(可线段树优化)
5194: [Usaco2018 Feb]Snow Boots Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 102 Solved: 79[Subm ...
- 线段树||BZOJ5194: [Usaco2018 Feb]Snow Boots||Luogu P4269 [USACO18FEB]Snow Boots G
题面:P4269 [USACO18FEB]Snow Boots G 题解: 把所有砖和靴子排序,然后依次处理每一双靴子,把深度小于等于它的砖块都扔线段树里,问题就转化成了求线段树已有的砖块中最大的砖块 ...
- Bzoj[Usaco2018 Feb]5194 Snow Boots(线段树)
Description 到冬天了,这意味着下雪了!从农舍到牛棚的路上有N块地砖,方便起见编号为1…N,第i块地砖上积了fi英尺的雪 .在Farmer John的农舍的地窖中,总共有B双靴子,编号为1… ...
- bzoj 5195: [Usaco2018 Feb]Directory Traversal【树形dp】
注意到目录是一颗树结构,然后就简单了,预以1为根的处理出dis[u]为以这个点为根,到子树内的目录总长,si为子树内叶子数 第二遍dfs换根即可 #include<iostream> #i ...
- [Usaco2018 Feb]Snow Boots
Description 到冬天了,这意味着下雪了!从农舍到牛棚的路上有N块地砖,方便起见编号为1-N,第i块地砖上积了fi英尺的雪.在Farmer John的农舍的地窖中,总共有B双靴子,编号为1-B ...
- bzoj5194: [Usaco2018 Feb]Snow Boots
还真是.. 就是 一个被不点名批评的垃圾骗分暴力选手被普及难度的省选信心(??)模拟赛艹爆的题解 的t3嘛... #include<cstdio> #include<iostream ...
- BZOJ 5194--[Usaco2018 Feb]Snow Boots(STL)
5194: [Usaco2018 Feb]Snow Boots Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 81 Solved: 61[Submi ...
随机推荐
- HDU1280前m大的数creat at 9:51,3.13,2016
前m大的数 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- nginx配置文件解答
nginx配置文件详解: server { listen 80; servername www.nginx1.com location / { root ...
- 【三分+精度问题】G. Toxophily
https://www.bnuoj.com/v3/contest_show.php?cid=9154#problem/G [题意] 已知人的坐标在(0,0),靶的位置在(x,y),人以速度v射箭并且射 ...
- VS Code 列编辑功能说明
新版本v1.13.1或者附近的版本中的列编辑功能已经调整. 一.多光标插入功能 Alt+鼠标左键,添加多光标输入 二.自由多行选择 Alt键+鼠标左键拖动选择各行的部分内容 三.列选择 Shift+A ...
- php之memcache学习
php之memcache学习 简介: memcache是一个分布式高速缓存系统. 分布式是说可以部署在多台服务器上,实现集群效果: 高速是因为数据都是维护在内存中的: 特点和使用场景: 1.非持久化存 ...
- hashlib-sha摘要算法模块
摘要:hashlib: 摘要算法的模块 用处: 1.查看某两个文件是否完全一致 "abcdefggg" "abcdefhhg" 2.加密认证 把密码加密后写入文 ...
- linux下查看哪个进程占用内存多
1.用top命令 1.top top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器 可以直接使用top命令后,查看%MEM的内容.可以 ...
- Swift初体验之HelloWord+苹果Swift编程语言新手教程【中文版】
AppDelegate.swift : <span style="font-size:24px;"><strong>// // AppDelegate.sw ...
- 【Nginx】I/O多路转接之select、poll、epoll
当需要读两个以上的I/O的时候,如果使用阻塞式的I/O,那么可能长时间的阻塞在一个描述符上面,另外的描述符虽然有数据但是不能读出来,这样实时性不能满足要求,大概的解决方案有以下几种: 1.使用多进程或 ...
- SpringMVC+MyBatis+JMS+JTA(分布式事务)
SpringMVC+MyBatis 相信已经是如今企业开发中经常使用技术了. 由于一些需求,我们须要集成JMS(我使用的是ActiveMQ).大家应该都知道.MQ也能够觉得是一个数据源.数据也是数据源 ...