[题目链接]

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. 虫食算(codevs 1064)

    题目描述 Description 所谓虫食算,就是原先的算式中有一部分被虫子啃掉了,需要我们根据剩下的数字来判定被啃掉的字母.来看一个简单的例子: 43#9865#045    +    8468#6 ...

  2. 【POJ3264】Balanced Lineup(RMQ)

    题意:每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛 ...

  3. python-web apache mod_python 模块的安装

    安装apache 下载mod_python 编译安装 测试 下载mod_python,下载地址:mod_python 在GitHub 上面, 下载之后:目录结构如下: 安装依赖: #查找可安装的依赖 ...

  4. PHP中的魔术方法【转载】

    __construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep, __wakeup, _ ...

  5. T2627 村村通 codevs

    http://codevs.cn/problem/2627/  时间限制: 1 s  空间限制: 32000 KB  题目等级 : 黄金 Gold 题目描述 Description 农民约翰被选为他们 ...

  6. 数据库中的DDL/DML/DCL解释(转)

    DDL is Data Definition Language statements. Some examples:数据定义语言,用于定义和管理 SQL 数据库中的所有对象的语言 1.CREATE - ...

  7. Design Pattern Visitor 訪问者设计模式

    訪问者设计模式是已经有了一组Person对象了,然后不同的訪问者訪问这组对象.会有不同效果. 这些訪问者实际上就是一个能够让Person对象组运行的动作行为等. 至于这些Person对象是怎样运行这些 ...

  8. Ribbon简介

    Ribbon简介

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

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

  10. VB6 如何连接MYSQL数据库

    1 从官网下载MYSQL的ODBC,选择与自己操作系统对应的版本(前提是你安装了MYSQL) http://dev.mysql.com/downloads/connector/odbc/   2 安装 ...