[题目链接]

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的更多相关文章

  1. bzoj5192: [Usaco2018 Feb]New Barns

    不想写看zory大佬 #include<cstdio> #include<iostream> #include<cstring> #include<cstdl ...

  2. BZOJ5196: [Usaco2018 Feb]Taming the Herd(DP暴力)

    5196: [Usaco2018 Feb]Taming the Herd Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 78  Solved: 71[ ...

  3. BZOJ5194: [Usaco2018 Feb]Snow Boots(排序&set)(可线段树优化)

    5194: [Usaco2018 Feb]Snow Boots Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 102  Solved: 79[Subm ...

  4. 线段树||BZOJ5194: [Usaco2018 Feb]Snow Boots||Luogu P4269 [USACO18FEB]Snow Boots G

    题面:P4269 [USACO18FEB]Snow Boots G 题解: 把所有砖和靴子排序,然后依次处理每一双靴子,把深度小于等于它的砖块都扔线段树里,问题就转化成了求线段树已有的砖块中最大的砖块 ...

  5. Bzoj[Usaco2018 Feb]5194 Snow Boots(线段树)

    Description 到冬天了,这意味着下雪了!从农舍到牛棚的路上有N块地砖,方便起见编号为1…N,第i块地砖上积了fi英尺的雪 .在Farmer John的农舍的地窖中,总共有B双靴子,编号为1… ...

  6. bzoj 5195: [Usaco2018 Feb]Directory Traversal【树形dp】

    注意到目录是一颗树结构,然后就简单了,预以1为根的处理出dis[u]为以这个点为根,到子树内的目录总长,si为子树内叶子数 第二遍dfs换根即可 #include<iostream> #i ...

  7. [Usaco2018 Feb]Snow Boots

    Description 到冬天了,这意味着下雪了!从农舍到牛棚的路上有N块地砖,方便起见编号为1-N,第i块地砖上积了fi英尺的雪.在Farmer John的农舍的地窖中,总共有B双靴子,编号为1-B ...

  8. bzoj5194: [Usaco2018 Feb]Snow Boots

    还真是.. 就是 一个被不点名批评的垃圾骗分暴力选手被普及难度的省选信心(??)模拟赛艹爆的题解 的t3嘛... #include<cstdio> #include<iostream ...

  9. BZOJ 5194--[Usaco2018 Feb]Snow Boots(STL)

    5194: [Usaco2018 Feb]Snow Boots Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 81  Solved: 61[Submi ...

随机推荐

  1. ubuntu使用git的时:Warning: Permanently added the RSA host key for IP address '13.250.177.223' to the list of known hosts.

    1:问题现象: hlp@hlp:~/code/github_code/catch_imooc1$ git push origin master Warning: Permanently added t ...

  2. 2016 Multi-University Training Contest 6 solutions BY UESTC

    A Boring Question \[\sum_{0\leq k_{1},k_{2},\cdots k_{m}\leq n}\prod_{1\leq j< m}\binom{k_{j+1}}{ ...

  3. 【c++】面向对象程序设计之访问控制与继承

    受保护的成员(protected): 1.和私有成员类似,受保护的成员对于类的用户来说是不可访问的 2.和共有成员类似,受保护的成员对于派生类的成员和友元来说是可访问的 3.派生类的友元只能通过派生类 ...

  4. Firefox下td用display控制页面导致页面变形

    Firefox下table的td元素假设使用了display:'block'会使得table变形.原因是block会将对象强制作为块对象呈递,为对象之后加入新行,所以并不适合td,改成display: ...

  5. Linux 将一般的用户加入sudo组is_not_in_the_sudoers_file._This_incident_will_be_reported解决方法

      在一般用户下执行sudo命令提示xxx is not in the sudoers file. This incident will be reported.解决方法:        $where ...

  6. A Single Channel with Multiple Consumers RabbitMQ

    http://stackoverflow.com/questions/30696351/a-single-channel-with-multiple-consumers-rabbitmq up vot ...

  7. freemarker 模板

    1 整体结构 模板(FTL 编程)是由例如以下部分混合而成的: Text 文本:文本会照着原样来输出. Interpolation 插值:这部分的输出会被计算的值来替换.插值由${和}所分隔(或者#{ ...

  8. c# 自定义Base16编码解码

               一.自定义Base16编码原理                  Base16编码跟Base64编码原理上有点不同,当然前面转换是一样的,都是是将输入的字符串根据默认编码转换成一 ...

  9. 【Mongodb教程 第一课 】 MongoDB下载安装

    MongoDB是一个高性能,开源,无模式的文档型数据库,是当前NoSql数据库中比较热门的一种.它在许多场景下可用于替代传统的关系型数据库或键/值存储方式.Mongo使用C++开发.以window平台 ...

  10. 责任链模式的具体应用 ServiceStack.Redis订阅发布服务的调用

    责任链模式的具体应用   1.业务场景 生产车间中使用的条码扫描,往往一把扫描枪需要扫描不同的条码来处理不同的业务逻辑,比如,扫描投入料工位条码.扫描投入料条码.扫描产出工装条码等,每种类型的条码位数 ...