一、题目

  

COT - Count on a tree

You are given a tree with N nodes. The tree nodes are numbered from 1 to N. Each node has an integer weight.

We will ask you to perform the following operation:

  • u v k : ask for the kth minimum weight on the path from node u to node v

Input

In the first line there are two integers N and M. (N, M <= 100000)

In the second line there are N integers. The ith integer denotes the weight of the ith node.

In the next N-1 lines, each line contains two integers u v, which describes an edge (u, v).

In the next M lines, each line contains three integers u v k, which means an operation asking for the kth minimum weight on the path from node u to node v.

Output

For each operation, print its result.

Example

Input:
8 5
105 2 9 3 8 5 7 7
1 2
1 3
1 4
3 5
3 6
3 7
4 82 5 12 5 22 5 32 5 47 8 2 
Output:
2891057 

题目链接:http://www.spoj.com/problems/COT/二、思路  原理其实和一维线性表序列的一样。只不过这是在树上操作。  在一维线性表序列中,扫描序列中每一个离散化后的数字,每一次新建的权值线段树都是基于前一次的。而在树中,对每一个子节点的数字,新建的线段树都是基于父节点的线段树的。
***************************未完待续……  
***************************
 三、源代码
#pragma GCC optimize(2)
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define MAXN 111111
LL n, q, m;
/*************树部分*******************/
typedef struct {
    int to, next;
} Edge;
Edge tree[MAXN * ];
int head[MAXN], ecnt;

void add(int from, int to) {
    tree[ecnt].to = to;
    tree[ecnt].next = head[from];
    head[from] = ecnt++;
}
/*************树部分*******************/
/*************LCA部分*******************/
], depth[MAXN];
void dfs4lca(int r, int par, int d) {
    parent[r][] = par, depth[r] = d;
    ; i = tree[i].next) {
        int to = tree[i].to;
        if(to != par)
            dfs4lca(to, r, d + );
    }
}
void init4lca() {
    dfs4lca(, -, );
    ; k < ; ++k) {
        ; v <= n; ++v) {
            )
                parent[v][k + ] = -;
            else
                parent[v][k + ] = parent[parent[v][k]][k];
        }
    }
}
int lca(int a, int b) {
    if(depth[a] < depth[b])
        swap(a, b);
    ; i < ; ++i) {
        )
            a = parent[a][i];
    }
    if(a == b)
        return a;

    ; i >= ; --i) {
        if(parent[a][i] != parent[b][i]) {
            a = parent[a][i];
            b = parent[b][i];
        }
    }
    ];
}
/*************LCA部分*******************/
/*************主席树部分*******************/
struct {
    int lch, rch, cnt;
} nodes[MAXN * ];
int root[MAXN], ncnt;

, int r = m) {
    if(!nroot) {
        nroot = ++ncnt;
        nodes[nroot].cnt = nodes[proot].cnt + ;
    }
    if(l == r)
        return;
    ;
    if(val <= mid) {
        nodes[nroot].rch = nodes[proot].rch;
        update(nodes[nroot].lch, nodes[proot].lch, val, l, mid);
    } else {
        nodes[nroot].lch = nodes[proot].lch;
        update(nodes[nroot].rch, nodes[proot].rch, val, mid + , r);
    }
}
, int r = m) {
    if(l == r)
        return l;
    int cnt = nodes[nodes[uroot].lch].cnt + nodes[nodes[vroot].lch].cnt
              - nodes[nodes[lcaroot].lch].cnt - nodes[nodes[lcafroot].lch].cnt;
    ;
    if(k <= cnt)
        return query(nodes[uroot].lch, nodes[vroot].lch, nodes[lcaroot].lch, nodes[lcafroot].lch, k, l, mid);
    else
        , r);
}
/*************主席树部分*******************/
/*************离散化部分*******************/
LL weight[MAXN], buf[MAXN], mp[MAXN];
int nwt[MAXN];
void discrete() {
    memcpy(buf + , weight + , ]) * n);
    sort(buf + , buf + n + );
    m = unique(buf + , buf + n + ) - buf - ;
    ; i <= n; ++i) {
        nwt[i] = lower_bound(buf + , buf + m + , weight[i]) - buf;
        mp[nwt[i]] = weight[i];
    }
}
/*************离散化部分*******************/
template <class T> inline void read(T &x) {
    int t;
    bool flag = false;
    ')) ;
    if(t == '-')
        flag = true, t = getchar();
    x = t - ';
    ')
        x = x *  + t - ';
    if(flag)
        x = -x;
}

void init() {
    memset(head, -, sizeof(head));
    ecnt = ;
    //nodes = 0, root = 0, ncnt = 0;
}

void dfs4tree(int r, int par) {
    ; i = tree[i].next) {
        int to = tree[i].to;
        if(to != par) {
            update(root[to], root[r], nwt[to]);
            dfs4tree(to, r);
        }
    }
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif // ONLINE_JUDGE
    init();
    LL a, b, u, v, k, wt, uvlca, ans;
    scanf("%lld%lld", &n, &q);
    ; i <= n; ++i) {
        read(weight[i]);
    }
    ; i < n; ++i) {
        read(a), read(b);
        add(a, b);
        add(b, a);
    }
    init4lca();
    discrete();

    add(, );//添加一条虚边。
    dfs4tree(, -);

    while(q--) {
        read(u), read(v), read(k);
        uvlca = LL(lca(u, v));
        ans = mp[query(root[u], root[v], root[uvlca], root[parent[uvlca][]], int(k))];
        printf("%lld\n", ans);
    }
    ;
}

 

SPOJ Count on a tree(主席树+LCA)的更多相关文章

  1. 【BZOJ2588】Spoj 10628. Count on a tree 主席树+LCA

    [BZOJ2588]Spoj 10628. Count on a tree Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lasta ...

  2. BZOJ 2588: Spoj 10628. Count on a tree 主席树+lca

    分析:树上第k小,然后我想说的是主席树并不局限于线性表 详细分析请看http://www.cnblogs.com/rausen/p/4006116.html,讲的很好, 然后因为这个熟悉了主席树,真是 ...

  3. spoj COT - Count on a tree(主席树 +lca,树上第K大)

    您将获得一个包含N个节点的树.树节点的编号从1到Ñ.每个节点都有一个整数权重. 我们会要求您执行以下操作: uvk:询问从节点u到节点v的路径上的第k个最小权重 输入 在第一行中有两个整数Ñ和中号.( ...

  4. [bzoj2588][count on a tree] (主席树+lca)

    Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...

  5. 洛谷P2633/bzoj2588 Count on a tree (主席树)

    洛谷P2633/bzoj2588 Count on a tree 题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K ...

  6. Bzoj 2588: Spoj 10628. Count on a tree 主席树,离散化,可持久,倍增LCA

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2588 2588: Spoj 10628. Count on a tree Time Limit ...

  7. spoj cot: Count on a tree 主席树

    10628. Count on a tree Problem code: COT You are given a tree with N nodes.The tree nodes are number ...

  8. 【BZOJ-2588】Count on a tree 主席树 + 倍增

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 3749  Solved: 873[ ...

  9. 洛谷P2633 Count on a tree(主席树上树)

    题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始为0,即第一个 ...

随机推荐

  1. springboot2 统一返回结果

    统一返回结果是说,不用在controller层,new一个对象,或用工厂创建这个对象,再返回这个对象,而是这个Action该返回什么就返回什么, 我们再通过mvc的流程,再对返回对象做进一步的封装,以 ...

  2. 安全之路:Web渗透技术及实战案例解析(第2版)

    安全之路:Web渗透技术及实战案例解析(第2版)

  3. 一次完整的HTTP事务是怎样一个过程

    当我们在浏览器的地址栏输入 www.linux178.com,然后回车,回车这一瞬间到看到页面到底发生了什么呢? 以下过程仅是个人理解: 域名解析 --> 发起TCP的3次握手 --> 建 ...

  4. Command(命令)

    意图: 将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化:对请求排队或记录请求日志,以及支持可撤消的操作. 适用性: 抽象出待执行的动作以参数化某对象,你可用过程语言中的回调(call ...

  5. 教你上传代码到码云(与github一样)

    以下所有操作都在命令行进行 1 git 配置 git config --global user.name “用户名” git config --global user.email “邮箱”2 生成公钥 ...

  6. JSP 文件上传

    JSP 文件上传 JSP可以通过HTML的form表单上传文件到服务器. 文件类型可以是文本文件.二进制文件.图像文件等其他任何文档. 创建文件上传表单 接下来我们使用HTML标签来创建文件上传表单, ...

  7. MVVM特点、源(数据)与目标(如:控件等)的映射

    数据(源,viewMode表示)与控件(目标)的完全映射, 在绑定之后,通过操作数据,改变控件显示效果.显示内容.状态等.

  8. IOS-快速集成检查更新

    一直以为Appstore有了检查版本是否更新的机制,我们在APP上做这个更新功能会被拒,但是也有看到一些APP也是做了这个更新功能的.因为在网上没有找到完全正确的方法能获取到iTunes里的数据的,于 ...

  9. hdu2883

    题解: 网络流 用一个离散化 代码: #include<cstdio> #include<cstring> #include<algorithm> using na ...

  10. grub2 详解

    grub2详解(翻译和整理官方手册)   分类: Linux 基础篇,Linux 杂项   本文原创地址在博客园:https://www.cnblogs.com/f-ck-need-u/archive ...