Count on a tree 【SPOJ - COT】【树上第K小、可持久化线段树(主席树)】

题目链接


  想了好久,一开始想去写树链剖分,但是怎样写第K小只在可持久化线段树中写过,然后就是得去想怎样做到状态的得到了,我们可以考虑从根节点出发,关系不断的递推下去,就可以变成一棵自上而下的可持久化线段树了。

  具体是:我们可以看作是从根节点出发的树,状态也是这样往下推下去,如此建立的线段树,我们查询一段区间的状态可以从LCA的角度去看问题,找到LCA(x, y)然后,我们只要一个LCA节点,然后求出区间X到根节点,以及Y到根节点的关系式来推这个关系,但是千万不要去减两倍LCA的关系,因为那样就会少掉一个节点了,于是,就dfs()往下建树,就是寻找到最后的答案了。

 

  最后,再赋上两组挺有用的测试样例。


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MP(a, b) make_pair(a, b)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 1e5 + 7;
int N, M, a[maxN], b[maxN], diff, root[maxN][20], deep[maxN], cnt, head[maxN];
int order, tree[maxN], lson[maxN*20], rson[maxN*20], siz[maxN*20];
struct Eddge
{
    int nex, to;
    Eddge(int a=-1, int b=0):nex(a), to(b) {}
}edge[maxN<<1];
inline void addEddge(int u, int v)
{
    edge[cnt] = Eddge(head[u], v);
    head[u] = cnt++;
}
void dfs(int u, int pre, int depth)
{
    root[u][0] = pre;
    deep[u] = depth;
    for(int i=head[u]; i!=-1; i=edge[i].nex)
    {
        int v = edge[i].to;
        if(v == pre) continue;
        dfs(v, u, depth + 1);
    }
}
void pre_LCA()
{
    dfs(1, 0, 0);
    for(int j=0; (1<<(j+1))>i) & 1 ) x = root[x][i];
    if(x == y) return x;
    for(int i = log2(1. * N); i>=0; i--)
    {
        if(root[x][i] != root[y][i])
        {
            x = root[x][i];
            y = root[y][i];
        }
    }
    return root[x][0];
}
void build(int &k, int l, int r)
{
    k = ++order;
    siz[k] = 0;
    if(l == r) return;
    int mid = HalF;
    build(lson[k], l, mid);
    build(rson[k], mid + 1, r);
}
void update(int old, int &now, int l, int r, int qx)
{
    now = ++order;
    lson[now] = lson[old]; rson[now] = rson[old];   siz[now] = siz[old] + 1;
    if(l == r) return;
    int mid = HalF;
    if(qx <= mid) update(lson[old], lson[now], l, mid, qx);
    else update(rson[old], rson[now], mid + 1, r, qx);
}
int query(int i, int j, int l, int r, int k, int lca, int fsa)
{
    if(l == r) return l;
    int det = siz[lson[i]] + siz[lson[j]] - siz[lson[lca]] - siz[lson[fsa]];
    int mid = HalF;
    if(k <= det) return query(lson[i], lson[j], l, mid, k, lson[lca], lson[fsa]);
    else return query(rson[i], rson[j], mid + 1, r, k - det, rson[lca], rson[fsa]);
}
void dfs_build(int u, int pre)
{
    int pos = (int)(lower_bound(b + 1, b + diff + 1, a[u]) - b);
    update(tree[pre], tree[u], 1, diff, pos);
    for(int i=head[u]; i!=-1; i=edge[i].nex)
    {
        int v = edge[i].to;
        if(v == pre) continue;
        dfs_build(v, u);
    }
}
inline void init()
{
    memset(head, -1, sizeof(head));
    cnt = order = 0;
    memset(root, -1, sizeof(root));
}
int main()
{
    while(scanf("%d%d", &N, &M)!=EOF)
    {
        init();
        for(int i=1; i<=N; i++)
        {
            scanf("%d", &a[i]);
            b[i] = a[i];
        }
        sort(b + 1, b + N + 1);
        diff = (int)(unique(b + 1, b + N + 1) - b - 1);
        for(int i=1; i

 

你可能感兴趣的:(线段树,数据结构,LCA算法,LCA,可持久化线段树)