题意:给定一棵有n个结点的树,每一个点有一个权值。共同拥有m个询问。对于每一个询问(u,v,k),回答结点u至v之间第k小的点的权值。

思路:主席树+lca。首先指定一个根结点dfs一次并在此过程中建好主席树。对于对于每一个询问,我们仅仅须要考虑四棵树,即T[u], T[v], T[lca(u,v)], 再加上T[fa( lca(u,v) )],fa( lca(u,v) )表示lca(u, v)的父亲结点。

这样一来问题就和线性序列里第k小的数一样了。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<ctime>
#define eps 1e-6
#define LL long long
#define pii (pair<int, int>)
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std; //const int maxn = 100000 + 100;
//const int INF = 0x3f3f3f3f; const int maxn = 100000+10000;
const int M = 2000000;
int n, q, m, tot;
int t[maxn], w[maxn], fa[maxn];
int T[maxn], lson[M], rson[M], c[M]; struct Quest {
int l, r, k;
} quest[maxn]; void Init_hash(int k) {
sort(t, t+k);
m = unique(t, t+k) - t;
} int Hash(int x) {
return lower_bound(t, t+m, x) - t;
} int build(int l, int r) {
int root = tot++;
c[root] = 0;
if(l != r) {
int mid = (l+r) >> 1;
lson[root] = build(l, mid);
rson[root] = build(mid+1, r);
}
return root;
} int Insert(int root, int pos, int val) {
int newroot = tot++, tmp = newroot;
int l = 0, r = m-1;
c[newroot] = c[root] + val;
while(l < r) {
int mid = (l+r)>>1;
if(pos <= mid) {
lson[newroot] = tot++; rson[newroot] = rson[root];
newroot = lson[newroot]; root = lson[root];
r = mid;
}
else {
rson[newroot] = tot++; lson[newroot] = lson[root];
newroot = rson[newroot]; root = rson[root];
l = mid+1;
}
c[newroot] = c[root] + val;
}
return tmp;
} int Query(int l_root, int r_root, int lca, int k) {
int l = 0, r = m -1, lca_root = T[lca], fa_root = fa[lca];
while(l < r) {
int mid = (l+r) >> 1;
int tmp = c[lson[l_root]]+c[lson[r_root]]-c[lson[lca_root]]-c[lson[fa_root]];
if(tmp >= k) {
r = mid;
l_root = lson[l_root]; r_root = lson[r_root]; lca_root = lson[lca_root]; fa_root = lson[fa_root];
}
else {
l = mid + 1;
k -= tmp;
l_root = rson[l_root]; r_root = rson[r_root]; lca_root = rson[lca_root]; fa_root = rson[fa_root];
}
}
return l;
} int pnt[maxn], lca[maxn];
bool vis[maxn];
vector<int> G[maxn], query[maxn], num[maxn];
int find(int x) {
if(x == pnt[x]) return x;
return pnt[x] = find(pnt[x]);
}
void dfs_lca(int u) {
vis[u] = 1; pnt[u] = u;
int sz1 = G[u].size();
for(int i = 0; i < sz1; i++) {
int v = G[u][i];
if(vis[v]) continue;
fa[v] = T[u];
dfs_lca(v);
pnt[v] = u;
}
int sz2 = query[u].size();
for(int i = 0; i < sz2; i++) {
int v = query[u][i];
if(vis[v]) lca[num[u][i]] = find(v);
}
}
void init() {
memset(vis, 0, sizeof(vis));
for(int i = 1; i <= n; i++) {
G[i].clear();
query[i].clear();
num[i].clear();
}
} void dfs_ZXTree(int cur, int fa) {
int sz = G[cur].size();
for(int i = 0; i < sz; i++) {
int u = G[cur][i];
if(u == fa) continue;
T[u] = Insert(T[cur], Hash(w[u]), 1);
dfs_ZXTree(u, cur);
}
} int main() {
//freopen("input.txt", "r", stdin);
while(cin >> n >> q) {
init();
m = 0; tot = 0;
for(int i = 1; i <= n; i++) scanf("%d", &w[i]), t[m++] = w[i];
Init_hash(m);
build(0, m-1);
for(int i = 1; i < n; i++) {
int u, v; scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
T[1] = Insert(T[0], Hash(w[1]), 1);
dfs_ZXTree(1, -1);
for(int i = 0; i < q; i++) {
int l, r, k; scanf("%d%d%d", &l, &r, &k);
quest[i].l = l; quest[i].r = r; quest[i].k = k;
query[l].push_back(r); query[r].push_back(l);
num[l].push_back(i); num[r].push_back(i);
}
fa[1] = T[0];
dfs_lca(1);
for(int i = 0; i < q; i++) {
printf("%d\n", t[Query(T[quest[i].l], T[quest[i].r], lca[i], quest[i].k)]);
}
}
return 0;
}




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

  1. BZOJ 2588: Spoj 10628. Count on a tree( LCA + 主席树 )

    Orz..跑得还挺快的#10 自从会树链剖分后LCA就没写过倍增了... 这道题用可持久化线段树..点x的线段树表示ROOT到x的这条路径上的权值线段树 ----------------------- ...

  2. BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5217  Solved: 1233 ...

  3. bzoj 2588 Spoj 10628. Count on a tree(主席树)

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

  4. bzoj 2588: Spoj 10628. Count on a tree【主席树+倍增】

    算是板子,把值离散化,每个点到跟上做主席树,然后查询的时候主席树上用u+v-lca-fa[lca]的值二分 #include<iostream> #include<cstdio> ...

  5. 【bzoj2588】Spoj 10628. Count on a tree 离散化+主席树

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

  6. [BZOJ2588]Count on a tree(LCA+主席树)

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

  7. 【bzoj2588/P2633】count on a tree —— LCA + 主席树

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

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

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

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

  10. Bzoj 2588 Spoj 10628. Count on a tree(树链剖分LCA+主席树)

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MB Description 给定一棵N个节点的树,每个点 ...

随机推荐

  1. python-day01 pip 在线安装,标识符规则,注释,变量名,类型

    1.python第三方库安装: 在线安装:pip install 库名 pip install 库名 -i 国内源网站地址 离线安装:xxx.tar.gz/rar/zip 解压安装 2.标识符规则: ...

  2. Aspnet_Session

    cmd: aspnet_regsql.exe -ssadd -sstype c -d ZZCasSession -S 192.168.0.3 -U sa -P szhweb2010 <!--会话 ...

  3. HTML5 postMessage 和 localStorage 实现窗口间通信

    LocalStorage(不能跨域) 基本思想:通过localStorage的标准事件storage来实现跨页面通信,即页面A通过写入特定数据触发页面B的storage事件,页面B响应之后再写入数据通 ...

  4. python--6、logging模块

    logging 可用的日志级别: debug 10 info 20 warning 30 error 40 critical 50 logging默认参数: 默认日志级别是warning. 默认情况日 ...

  5. 后端springmvc,前端html5的FormData实现文件断点上传

    前言 最近项目中有使用到文件断点上传,得空便总结总结,顺便记录一下,毕竟“好记性不如烂笔头”. 后端代码: package com.test.controller; import java.io.Bu ...

  6. IIS7部署网站的一些细节问题。

    1.不能在此路径中使用此配置节.如果在父级别上锁定了该节,便会出现这种情况. 这个错误的原因是在 IIS 7中 采用了更安全的 web.config 管理机制,默认情况下会锁住配置项.要取消锁定可以以 ...

  7. Interrupt中断线程

    package com.wistron.swpc.ecs.util; public class WrongWayStopThread extends Thread{ public static voi ...

  8. C# 检测dll的新版本号方法

    FileVersionInfo info = FileVersionInfo.GetVersionInfo(YourFileNameHere);string version = info.FileMa ...

  9. Repeater + 分页控件 AspNetPager 研究

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs ...

  10. WPF动态折线图

    此项目源码下载地址:https://github.com/lizhiqiang0204/WpfDynamicChart 效果图如下: 此项目把折线图制作成了一个控件,在主界面设置好参数直接调用即可,下 ...