https://www.luogu.org/problemnew/show/3398

树剖练习题,两个懒标记,搜索时序为全局懒标记

#include <bits/stdc++.h>

using namespace std;
const int N = 1e5 + ; #define gc getchar()
#define lson jd << 1
#define rson jd << 1 | 1 struct Node_1{
int v, nxt;
}G[N << ];
struct Node_2{
int fa, son, siz, deep, topp, tree;
}P[N];
struct Node_3{
int l, r, f, ff;
}T[N << ];
int n, Ti, head[N], now = , tim, imp_clock;
bool ans, answer; inline int read(){
int x = ; char c = gc;
while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc;
return x;
} inline void add(int u, int v){
G[now].v = v;
G[now].nxt = head[u];
head[u] = now ++;
} void dfs_find_son(int u, int fa, int dep){
P[u].fa = fa;
P[u].deep = dep;
P[u].siz = ;
for(int i = head[u]; ~ i; i = G[i].nxt){
int v = G[i].v;
if(v != fa){
dfs_find_son(v, u, dep + );
P[u].siz += P[v].siz;
if(P[v].siz > P[P[u].son].siz) P[u].son = v;
}
}
} void dfs_to_un(int u, int tp){
P[u].topp = tp;
P[u].tree = ++ tim;
if(!P[u].son) return ;
dfs_to_un(P[u].son, tp);
for(int i = head[u]; ~ i; i = G[i].nxt){
int v = G[i].v;
if(v != P[u].fa && v != P[u].son) dfs_to_un(v, v);
}
} void Sec_G(int l, int r, int jd, int x, int y, int g){
if(x <= l && r <= y) {
T[jd].f = g;
T[jd].ff = g;
return ;
}
int mid = (l + r) >> ;
if(x <= mid) Sec_G(l, mid, lson, x, y, g);
if(y > mid) Sec_G(mid + , r, rson, x, y, g);
if(T[lson].f == g || T[rson].f == g) T[jd].f = g; } void Sec_G_imp(int x, int y){
++ imp_clock;
int tp1 = P[x].topp, tp2 = P[y].topp;
while(tp1 != tp2){
if(P[tp1].deep < P[tp2].deep) swap(x, y), swap(tp1, tp2);
Sec_G(, n, , P[tp1].tree, P[x].tree, imp_clock);
x = P[tp1].fa;
tp1 = P[x].topp;
}
if(P[x].deep > P[y].deep) swap(x, y);
Sec_G(, n, , P[x].tree, P[y].tree, imp_clock);
} void Sec_pd(int l, int r, int jd, int x, int y){
if(x <= l && r <= y){
if(T[jd].f == imp_clock) ans = ;
return ;
}
if(T[jd].ff == imp_clock && T[jd].f == imp_clock){
T[lson].f = T[rson].f = T[jd].f;
T[lson].ff = T[rson].ff = imp_clock;
}
int mid = (l + r) >> ;
if(x <= mid) Sec_pd(l, mid, lson, x, y);
if(y > mid) Sec_pd(mid + , r, rson, x, y);
} bool Sec_pd_imp(int x, int y){
int tp1 = P[x].topp, tp2 = P[y].topp;
while(tp1 != tp2){
if(P[tp1].deep < P[tp2].deep) swap(x, y), swap(tp1, tp2);
ans = ;
Sec_pd(, n, , P[tp1].tree, P[x].tree);
if(ans) return ;
x = P[tp1].fa;
tp1 = P[x].topp;
}
ans = ;
if(P[x].deep > P[y].deep) swap(x, y);
Sec_pd(, n, , P[x].tree, P[y].tree);
if(ans) return ;
return ;
} inline void work(int x_1, int y_1, int x_2, int y_2){
Sec_G_imp(x_1, y_1);
answer = Sec_pd_imp(x_2, y_2);
if(answer) puts("Y");
else puts("N");
return ;
} int main()
{
n = read(); Ti = read();
for(int i = ; i <= n; i ++) head[i] = -;
for(int i = ; i < n; i ++){
int u = read(), v = read();
add(u, v); add(v, u);
}
dfs_find_son(, , );
dfs_to_un(, );
while(Ti --){
int x_1 = read(), y_1 = read(), x_2 = read(), y_2 = read();
work(x_1, y_1, x_2, y_2);
}
return ;
}
/*
5 5
2 5
4 2
1 3
1 4
5 1 5 1
2 2 1 4
4 1 3 4
3 1 1 5
3 5 1 4
*/

[Luogu] 仓鼠找sugar的更多相关文章

  1. Luogu P3412 仓鼠找$sugar$ $II$

    Luogu P3412 仓鼠找\(sugar\) \(II\) 题目大意: 给定一棵\(n\)个点的树, 仓鼠每次移动都会等概率选择一个与当前点相邻的点,并移动到此点. 现在随机生成一个起点.一个终点 ...

  2. [Luogu 3398] 仓鼠找sugar

    [Luogu 3398] 仓鼠找sugar 又是 LCA- 前两天死活写不过的一个题今天终于顺手切了. 思路嘛参考了一楼题解. 就是说,对于 a, b, c, d 四个点, 令 x = LCA(a, ...

  3. luogu P3412 仓鼠找sugar II 期望 树形dp

    LINK:仓鼠找sugar II 以前做过类似的期望题目 加上最后的树形dp不算太难 还是可以推出来的. 容易发现 当固定起点和终点的时候 可以先固定根 这样就不用分到底是正着走还是倒着走了. 1为根 ...

  4. 洛谷P3398 仓鼠找sugar [LCA]

    题目传送门 仓鼠找sugar 题目描述 小仓鼠的和他的基(mei)友(zi)sugar住在地下洞穴中,每个节点的编号为1~n.地下洞穴是一个树形结构.这一天小仓鼠打算从从他的卧室(a)到餐厅(b),而 ...

  5. P3398 仓鼠找sugar

    P3398 仓鼠找sugar 题目描述 小仓鼠的和他的基(mei)友(zi)sugar住在地下洞穴中,每个节点的编号为1~n.地下洞穴是一个树形结构.这一天小仓鼠打算从从他的卧室(a)到餐厅(b),而 ...

  6. 【Luogu3398】仓鼠找sugar(树链剖分)

    [Luogu3398]仓鼠找sugar(树链剖分) 题面 题目描述 小仓鼠的和他的基(mei)友(zi)sugar住在地下洞穴中,每个节点的编号为1~n.地下洞穴是一个树形结构.这一天小仓鼠打算从从他 ...

  7. 【洛谷】【lca+结论】P3398 仓鼠找sugar

    [题目描述:] 小仓鼠的和他的基(mei)友(zi)sugar住在地下洞穴中,每个节点的编号为1~n.地下洞穴是一个树形结构.这一天小仓鼠打算从从他的卧室(a)到餐厅(b),而他的基友同时要从他的卧室 ...

  8. 洛谷 P3398 仓鼠找sugar 解题报告

    P3398 仓鼠找sugar 题目描述 小仓鼠的和他的基(mei)友(zi)sugar住在地下洞穴中,每个节点的编号为1~n.地下洞穴是一个树形结构.这一天小仓鼠打算从从他的卧室(a)到餐厅(b),而 ...

  9. 【树链剖分/倍增模板】【洛谷】3398:仓鼠找sugar

    P3398 仓鼠找sugar 题目描述 小仓鼠的和他的基(mei)友(zi)sugar住在地下洞穴中,每个节点的编号为1~n.地下洞穴是一个树形结构.这一天小仓鼠打算从从他的卧室(a)到餐厅(b),而 ...

随机推荐

  1. 使用Docker搭建svn服务器教程

    svn简介 SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS.互联网上很多版本控制服务已从CVS迁移到Subv ...

  2. Python遗传和进化算法框架(一)Geatpy快速入门

    https://blog.csdn.net/qq_33353186/article/details/82014986 Geatpy是一个高性能的Python遗传算法库以及开放式进化算法框架,由华南理工 ...

  3. Bigdecimal除法异常

    1.异常信息摘要(详细请见文末): java.lang.ArithmeticException: Non-terminating decimal expansion; no exact represe ...

  4. 炫酷的可视化工具包——cufflinks

    前言 学过Python数据分析的朋友都知道,在可视化的工具中,有很多优秀的三方库,比如matplotlib,seaborn,plotly,Boken,pyecharts等等.这些可视化库都有自己的特点 ...

  5. restTemplate源码解析(目录)

    restTemplate是spring实现的,基于restful风格的http请求模板.使用restTemplate可以简化请求操作的复杂性,同时规范了代码风格.本系列文章,将根据以下目录顺序,从源码 ...

  6. h5 点击ios键盘完成 出现键盘大小的白块

    document.addEventListener('focusout', function (e) { window.scrollTo() }) 源文件链接 https://blog.csdn.ne ...

  7. laravel模型关联评论

    用户模型 public function show(Post $post,LogManager $log){ $post->load("comments"); //这种方式是 ...

  8. Android笔记(二十六) Android中的广播——BroadcastReceiver

    为了方便进行系统级别的消息通知,Android有一套类似广播的消息机制,每个应用程序都可以对自己感兴趣的广播进行注册,这样该程序就只会接收自己所关心的广播内容,这些广播可能是来自于系统,也可能是来自于 ...

  9. 【年度盘点】10个热门Python练习项目

    这10个项目,约有270000人学习过. 今天给大家盘点一下实验楼最热门的10个Python练习项目.第二名是<200 行 Python 代码实现 2048>,第三名是<Python ...

  10. 基于CentOS构建企业镜像站

    参考:How to Setup Local HTTP Yum Repository on CentOS 7 实验环境 CentOS7 1804 步骤一:安装Nginx Web Server 最小化安装 ...