Code:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<iostream> using namespace std; void SetIO(string a){
string in = a + ".in";
freopen(in.c_str(),"r",stdin);
} void debug(){
cout << 233 << endl;
} const int maxn = 100000 + 5; int n, m; int val[maxn]; int Sorted[maxn]; inline void Disperse(){
sort(Sorted + 1, Sorted + 1 + n);
for(int i = 1;i <= n; ++i)
val[i] = lower_bound(Sorted + 1, Sorted + 1 + n, val[i]) - Sorted;
} int head[maxn << 1], to[maxn << 1], nex[maxn << 1], edges; inline void add_edge(int u, int v){
nex[++edges] = head[u];
head[u] = edges;
to[edges] = v;
} inline void Read(){
scanf("%d%d",&n, &m);
for(int i = 1;i <= n; ++i){
scanf("%d",&val[i]), Sorted[i] = val[i];
} for(int i = 1;i < n; ++i){
int a, b;
scanf("%d%d",&a,&b);
add_edge(a,b);
add_edge(b,a);
}
} const int Tree_const = 50; int root[maxn]; struct Chair_Tree{
int cnt_node; int sumv[maxn * Tree_const], lson[maxn * Tree_const], rson[maxn * Tree_const]; void build(int l, int r, int &o){
if(l > r) return ;
o = ++ cnt_node;
if(l == r) return ;
int mid = (l + r) >> 1;
build(l, mid, lson[o]);
build(mid + 1, r, rson[o]);
} int insert(int l, int r, int o, int pos){
int oo = ++cnt_node;
lson[oo] = lson[o];
rson[oo] = rson[o];
sumv[oo] = sumv[o] + 1; if(l == r) return oo; int mid = (l + r) >> 1;
if(pos <= mid) lson[oo] = insert(l, mid, lson[o], pos);
else rson[oo] = insert(mid + 1, r, rson[o], pos);
return oo;
} int query(int l, int r, int u, int v, int lca, int lca_fa, int k){
if(l == r) return l;
int lsum = sumv[lson[u]] + sumv[lson[v]] - sumv[lson[lca]] - sumv[lson[lca_fa]];
int mid = (l + r) >> 1;
if(k <= lsum) return query(l, mid, lson[u], lson[v], lson[lca], lson[lca_fa], k);
else return query(mid + 1, r, rson[u], rson[v], rson[lca], rson[lca_fa], k - lsum);
} }Tree; const int logn = 20; int f[23][maxn]; int dep[maxn]; void dfs(int u, int fa, int depth){ root[u] = Tree.insert(1, n, root[fa], val[u]);
dep[u] = depth;
f[0][u] = fa; for(int v = head[u]; v ; v = nex[v]){
if(to[v] == fa) continue;
dfs(to[v], u, depth + 1);
}
} inline void get_ancester(){
for(int i = 1;i <= logn; ++i){
for(int j = 1;j <= n; ++j)
f[i][j] = f[i - 1][f[i - 1][j]];
}
} inline int get_lca(int a, int b){
if(dep[a] > dep[b]) swap(a,b);
if(dep[a] != dep[b]){
for(int i = logn;i >= 0;--i){
if(dep[f[i][b]] >= dep[a]) b = f[i][b];
}
}
if(a == b) return a;
for(int i = logn;i>=0;--i)
if(f[i][a] != f[i][b]) a = f[i][a], b = f[i][b];
return f[0][a];
} inline void Build(){
Tree.build(1, n, root[0]);
dfs(1, 0, 1);
get_ancester();
} inline void Init(){
Read();
Disperse();
Build();
} inline void Work(){ int lastans = 0; while(m--){
int u, v, k;
scanf("%d%d%d",&u, &v, &k);
// u ^= lastans; int lca = get_lca(u, v); lastans = Tree.query(1, n, root[u], root[v], root[lca], root[f[0][lca]], k);
lastans = Sorted[lastans]; printf("%d\n", lastans);
}
} int main(){
SetIO("input");
Init();
Work();
return 0;
}

  

SP10628 COT - Count on a tree 主席树的更多相关文章

  1. 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 ...

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

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

  3. SPOJ Count on a tree(主席树+LCA)

    一.题目 COT - Count on a tree You are given a tree with N nodes. The tree nodes are numbered from 1 to  ...

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

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

  5. 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 ...

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

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

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

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

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

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

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

    在一棵树上,我们要求点 $(u,v)$ 之间路径的第$k$大数. 对于点 $i$  ,建立 $i$  到根节点的一棵前缀主席树. 简单容斥后不难得出结果为$sumv[u]+sumv[v]−sumv[l ...

随机推荐

  1. ASP版_阿里大于短信API Demo

    阿里大于申请地址:http://www.alidayu.com 阿里大于短信发送Demo: ******index.asp************* <%@LANGUAGE="VBSC ...

  2. 陆、jq基础语法

    一.概述:更加方便的处理html文档.events事件.动画效果和ajax交互等. 1.jq主要功能: (1)访问页面框架的局部. (2)修改页面表现 (3)更改页面的内容 (4)响应事件 (5)为页 ...

  3. Element源码阅读(2)

    一.element中的指令 在input-number组件中, 用到一个v-repeat-click指令, 主要用于input-number组件的重复点击效果.具体实现在directives中的rep ...

  4. BZOJ 2118 墨墨的等式 (同余最短路)

    题目大意:已知B的范围,求a1x1+a2x2+...+anxn==B存在非负正整数解的B的数量,N<=12,ai<=1e5,B<=1e12 同余最短路裸题 思想大概是这样的,我们选定 ...

  5. s5pv210 uboot-2012-10移植(二) 之能够启动进入控制台

    这次我们将从官网下载的最新uboot-2012-10移植到s5pv210开发板上,让其进入控制台,效果如下: 首先,我暂时没采用内核的SPL,这个将在后面给补上,这里的BL1是我自己参考资料写的,我用 ...

  6. UGUI图集管理

    using UnityEngine; using System.Collections; using System.Collections.Generic; //纹理图集加载管理 public cla ...

  7. rpm方式在centos7中安装mysql

    .安装MySQL server 首先下载好mysql的rpm安装包 使用rpm命令安装: rpm -ivh MySQL-server--.glibc23.i386.rpm #rpm -ivh MySQ ...

  8. C# .net IDE Rider入门

    话说史上最强IDE Visual Studio 所向披靡数十载尚无敌手,现在Intellij带着统一IDE界的目标来挑战VS的霸主地位.了解Rider后发现,哎哟亮点多多,还不错哦! Rider是一款 ...

  9. Spring MVC学习总结(7)——Spring MVC整合Ehcache缓存框架

    Ehcache算是当前比较流行的缓存框架,使用缓存可以极大的缓解服务器和数据库的压力,提高访问效率,提高服务器的并发能力.接下来我们看怎么把缓存使用起来. SpringMVC集成Ehcache所需的j ...

  10. HDU 4332 Contest 4

    顶好的一道题.其实,是POJ 2411的升级版.但POJ 2411我用的插头DP来做,一时没想到那道题怎么用状态DP,于是回头看POJ 2411那一道的状态DP,其实也很简单,就是每一行都设一个状态, ...