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. AS3.0 位图翻转、旋转

    /* * * *-------------------------* * | *** 位图翻转.旋转 *** | * *-------------------------* * * 作 者:fengz ...

  2. Linux 内核错误码

    #define    EPERM         1    /* Operation not permitted */#define    ENOENT        2    /* No such ...

  3. IDEA解决 lombok 编译报错 cannot resolve method set/get 方法

    首先关于lombok的介绍 https://blog.csdn.net/ThinkWon/article/details/101392808 总之,就是通过添加注解的方式,不需要为类编写getter或 ...

  4. windows下搭建nginx负载均衡

    学习笔记,第一次记录避免忘记 首先介绍一下本地环境是windows2008 R2-64位. 1.  到nginx官网上下载最新稳定版的安装包,http://nginx.org/en/download. ...

  5. robot framework 接口测试 http协议post请求json格式

    robot framework 接口测试 http协议post请求json格式 讲解一个基础版本.注意区分url地址和uri地址. rf和jmeter在添加服务器地址也就是ip地址的时候,只能url地 ...

  6. 【MySQL】数据库(分库分表)中间件对比

    分区:对业务透明,分区只不过把存放数据的文件分成了许多小块,例如mysql中的一张表对应三个文件.MYD,MYI,frm. 根据一定的规则把数据文件(MYD)和索引文件(MYI)进行了分割,分区后的表 ...

  7. 【转载】C#使用Trim方法去除字符串前后的所有空格

    在C#语言程序开发过程中,很多时候需要对字符串对象的前后空格进行去除,此时就需要使用到Trim()方法来实现这个功能,Trim()方法可以快速去除字符串前端和后端的所有空格. 例如有个字符:strin ...

  8. CSS3浏览器私有属性

    CSS3的浏览器私有属性前缀是一个浏览器生产商经常使用的一种方式.它暗示该CSS属性或规则尚未成为W3C标准的一部分.因此每种内核的浏览器都只能识别带有自身私有前缀的CSS3属性.我们在书写CSS3代 ...

  9. git推送代码Gogs报401错误

    1.git push 报错:RPC failed; HTTP 401 curl 22 The requested URL returned error: 401 The remote end hung ...

  10. awk 表达式

    awk动作表达式中的算数运算符 awk动作表达式中的算数运算符 案例演示 使用awk计算/etc/services中的空白行数 awk 'BEGIN{sum=0}/^$/{++sum}END{prin ...