题解:P12001 在小小的奶龙山里面挖呀挖呀挖

· · 题解

这道题我交题解时四篇题解三篇不低于蓝。点分治,树上莫队,树剖这些怎么可能出现在一道绿上?

这道题可以首先转化一下,变成一道 bitset 按位或然后最后输出 countbit 的问题。现在就要想办通过预处理法加速按位或。具体实现就是假设 p_i 是第 i 小的质数,开 n 个 bitset 叫作 b。如果 p_i \mid a_j 就将 b_j 的第 i 位设置成 1 否则为 0。

我一开始想着直接树上倍增创过去,但是树上倍增的空间复杂度带 \log 导致我的代码喜提 MLE。无奈的我通过看题解想到了重剖,重剖的空间复杂度可以省掉一个 \log,并且由于不带修改,可以实现在线 O(\log n) 单次查询。

树上倍增和树剖的具体的实现非常简单,直接写就完了。这里不做具体讲解。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define N 100010
#define M 50010
#define INF 0x3f3f3f3f
#define lowbit(x) (x&-x)
#define pii pair<int,int>
#define ds bitset<10000>
int p[N],lp,n;bool np[N];// 素数
int q,u,v,pt[N];
int a;ds b[M],c[M];
int dep[M],sze[M],h[M],d[M];// 树剖
int top[M],dfn[M],dfc,ord[M];
namespace segment_tree{
    struct node{int lq,rq,lson,rson;ds val;}pn[N];int lqn = 1;
    void build(const int &x,const int &lq,const int &rq){node &now = pn[x];now.lq = lq,now.rq = rq;
        if(lq == rq){now.val = b[ord[lq]];return ;}int mid = lq + (rq - lq >> 1);now.lson = ++lqn,now.rson = ++lqn;
        build(now.lson,lq,mid),build(now.rson,mid+1,rq);now.val = (pn[now.lson].val | pn[now.rson].val);
    }
    ds query(const int &x,const int &lq,const int &rq){const node &now = pn[x];if(lq <= now.lq && now.rq <= rq)return now.val;
        if(rq <= pn[now.lson].rq)return query(now.lson,lq,rq);
        else if(lq >= pn[now.rson].lq)return query(now.rson,lq,rq);
        return query(now.lson,lq,rq) | query(now.rson,lq,rq);
    }
}using namespace segment_tree;
namespace graph{int head[M],nxt[N],to[N],cnt_edge;
    inline void connect(const int &x,const int &y){++cnt_edge;nxt[cnt_edge] = head[x],to[cnt_edge] = y;head[x] = cnt_edge;}
}using namespace graph;
void init(const int &x,const int &dad){sze[x] = 1;d[x] = dad;dep[x] = dep[dad] + 1;
    for(int edg = head[x];edg;edg = nxt[edg]){const int &v = to[edg];if(v == dad)continue;init(v,x);
        sze[x] += sze[v];if(sze[v] > sze[h[x]])h[x] = v;
    }
}
void dfs(const int &x,const int &dad){dfn[x] = ++dfc,ord[dfc] = x;if(!h[x])return ;
    top[h[x]] = top[x],c[h[x]] = c[x] | b[h[x]];dfs(h[x],x);
    for(int edg = head[x];edg;edg = nxt[edg]){const int &v = to[edg];if(v == dad || v == h[x])continue;
        c[v] = b[v];top[v] = v;dfs(v,x);
    }
}
inline ds solve(int u,int v){ds ans = 0;
    while(top[u] != top[v]){
        if(dep[top[u]] > dep[top[v]])ans |= c[u],u = d[top[u]];
        else ans |= c[v],v = d[top[v]];
    }u = dfn[u],v = dfn[v];if(u > v)swap(u,v);return ans | query(1,u,v);
}

// 主函数
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    n = 100000;// 素数
    for(int i = 2;i <= n;++i){if(!np[i])p[++lp] = i,pt[i] = lp;
        for(int j = 1;i * p[j] <= n && j <= lp;++j){np[i * p[j]] = 1;if(i % p[j] == 0)break;}
    }
    cin >> n >> q;
    for(int i = 1;i <= n;++i){cin >> a;
        for(int j = 1;j <= lp && a != 1;++j){if(a % p[j])continue;b[i][j] = 1;while(a % p[j] == 0)a /= p[j];
            if(pt[a]){b[i][pt[a]] = 1;break;}
        }
    }
    for(int i = 1;i < n;++i)cin >> u >> v,connect(u,v),connect(v,u);
    init(1,0);top[1] = 1,c[1] = b[1];dfs(1,0);build(1,1,n);
    // for(int i = 1;i <= n;++i)printf("[%d]",top[i]);
    while(q--)cin >> u >> v,cout << solve(u,v).count() << '\n';
    // cout << flush,system("pause"); // 交之前一定要把这一行注释掉!!!!!!!
    return 0;
}

后记:看了一眼这题的加强版发现需要树上莫队这种恶心我不会的东西。