[Luogu] 仓鼠找sugar

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

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

#include <bits/stdc++.h>

using namespace std;
const int N = 1e5 + 10;

#define gc getchar()
#define lson jd << 1
#define rson jd << 1 | 1

struct Node_1{
    int v, nxt;
}G[N << 1];
struct Node_2{
    int fa, son, siz, deep, topp, tree;
}P[N];
struct Node_3{
    int l, r, f, ff;
}T[N << 2];
int n, Ti, head[N], now = 1, tim, imp_clock;
bool ans, answer;

inline int read(){
    int x = 0; char c = gc;
    while(c < '0' || c > '9') c = gc;
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', 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 = 1;
    for(int i = head[u]; ~ i; i = G[i].nxt){
        int v = G[i].v;
        if(v != fa){
            dfs_find_son(v, u, dep + 1);
            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) >> 1;
    if(x <= mid) Sec_G(l, mid, lson, x, y, g);
    if(y > mid) Sec_G(mid + 1, 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(1, n, 1, 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(1, n, 1, 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 = 1;
        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) >> 1;
    if(x <= mid) Sec_pd(l, mid, lson, x, y);
    if(y > mid) Sec_pd(mid + 1, 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 = 0;
        Sec_pd(1, n, 1, P[tp1].tree, P[x].tree);
        if(ans) return 1;
        x = P[tp1].fa;
        tp1 = P[x].topp;
    }
    ans = 0;
    if(P[x].deep > P[y].deep) swap(x, y);
    Sec_pd(1, n, 1, P[x].tree, P[y].tree);
    if(ans) return 1;
    return 0;
}

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 = 1; i <= n; i ++) head[i] = -1;
    for(int i = 1; i < n; i ++){
        int u = read(), v = read();
        add(u, v); add(v, u);
    }
    dfs_find_son(1, 0, 1); 
    dfs_to_un(1, 1);
    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 0;
}
/*
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
*/
原文地址:https://www.cnblogs.com/shandongs1/p/8011223.html