SPOJ COT Count on a tree(树上主席树 + LCA 求点第k小)题解
题意:n个点的树,每个点有权值,问你u~v路径第k小的点的权值是?
思路:
树上主席树就是每个点建一棵权值线段树,具体看JQ博客,LCA用倍增logn求出,具体原理看这里
树上主席树我每个点的存的是点u到源点1的权值线段树,那我求点u到v的所有点,显然是 u + v - lca - fa[lca],就是u到1 + v到1 - 多算的lca - 多算的fa[lca]。不能减去两个lca不然少一个点了,
LCA板子:
//LCA
int fa[maxn][];
int dep[maxn];
void lca_dfs(int u, int pre, int d){
dep[u] = d;
fa[u][] = pre;
for(int i = head[u]; i != -; i = edge[i].next)
if(edge[i].v != pre)
lca_dfs(edge[i].v, u, d + );
}
void lca_update(){
for (int i = ; ( << i) <= n; i++)
for(int u = ; u <= n; u++)
fa[u][i] = fa[fa[u][i - ]][i - ];
}
int lca_query(int u, int v){
if(dep[u] < dep[v]) swap(u, v);
int d = dep[u] - dep[v];
for(int i = ; ( << i) <= d; i++) {
if(d & ( << i)) {
u = fa[u][i];
}
}
if(u != v) {
for(int i = (int)log2(n); i >= ; i--) {
if(fa[u][i] != fa[v][i]) {
u = fa[u][i];
v = fa[v][i];
}
}
u = fa[u][];
}
return u;
}
代码:
#include<cmath>
#include<set>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = + ;
const int M = maxn * ;
const ull seed = ;
const int INF = 0x3f3f3f3f;
const int MOD = ;
int n, m;
int root[maxn], a[maxn], tot;
struct Edge{
int v, next;
}edge[maxn << ];
int head[maxn], tol;
void addEdge(int u, int v){
edge[tol].v = v;
edge[tol].next = head[u];
head[u] = tol++;
}
struct node{
int lson, rson;
int sum;
}T[maxn * ];
void init(){
memset(T, , sizeof(T));
memset(root ,, sizeof(root));
memset(head, -, sizeof(head));
tot = tol = ;
}
vector<int> ve;
int getid(int x){
return lower_bound(ve.begin(), ve.end(), x) - ve.begin() + ;
}
void update(int l, int r, int &now, int pre, int v, int pos){
T[++tot] = T[pre], T[tot].sum += v, now = tot;
if(l == r) return;
int m = (l + r) >> ;
if(pos <= m)
update(l, m, T[now].lson, T[pre].lson, v, pos);
else
update(m + , r, T[now].rson, T[pre].rson, v, pos);
}
void build(int now, int pre){
update(, n, root[now], root[pre], , getid(a[now]));
for(int i = head[now]; i != -; i = edge[i].next){
int v = edge[i].v;
if(v == pre) continue;
build(v, now);
}
}
int query(int l, int r, int now, int pre, int lca, int flca, int k){
if(l == r) return l;
int m = (l + r) >> ;
int sum = T[T[now].lson].sum + T[T[pre].lson].sum - T[T[lca].lson].sum - T[T[flca].lson].sum;
if(sum >= k)
return query(l, m, T[now].lson, T[pre].lson, T[lca].lson, T[flca].lson, k);
else
return query(m + , r, T[now].rson, T[pre].rson, T[lca].rson, T[flca].rson, k - sum);
} //LCA
int fa[maxn][];
int dep[maxn];
void lca_dfs(int u, int pre, int d){
dep[u] = d;
fa[u][] = pre;
for(int i = head[u]; i != -; i = edge[i].next)
if(edge[i].v != pre)
lca_dfs(edge[i].v, u, d + );
}
void lca_update(){
for (int i = ; ( << i) <= n; i++)
for(int u = ; u <= n; u++)
fa[u][i] = fa[fa[u][i - ]][i - ];
}
int lca_query(int u, int v){
if(dep[u] < dep[v]) swap(u, v);
int d = dep[u] - dep[v];
for(int i = ; ( << i) <= d; i++) {
if(d & ( << i)) {
u = fa[u][i];
}
}
if(u != v) {
for(int i = (int)log2(n); i >= ; i--) {
if(fa[u][i] != fa[v][i]) {
u = fa[u][i];
v = fa[v][i];
}
}
u = fa[u][];
}
return u;
}
int main(){
init();
ve.clear();
scanf("%d%d", &n, &m);
for(int i = ; i <= n; i++)
scanf("%d", &a[i]), ve.push_back(a[i]);
sort(ve.begin(), ve.end());
ve.erase(unique(ve.begin(), ve.end()), ve.end());
for(int i = ; i <= n - ; i++){
int u, v;
scanf("%d%d", &u, &v);
addEdge(u, v);
addEdge(v, u);
}
lca_dfs(, , );
lca_update();
build(, );
while(m--){
int u, v, k;
scanf("%d%d%d", &u, &v, &k);
int lca = lca_query(u, v);
int ans = query(, n, root[u], root[v], root[lca], root[fa[lca][]], k);
printf("%d\n", ve[ans - ]);
}
return ;
}
SPOJ COT Count on a tree(树上主席树 + LCA 求点第k小)题解的更多相关文章
- 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 ...
- Count on a tree 树上主席树
Count on a tree 树上主席树 给\(n\)个树,每个点有点权,每次询问\(u,v\)路径上第\(k\)小点权,强制在线 求解区间静态第\(k\)小即用主席树. 树上主席树类似于区间上主席 ...
- spoj COT - Count on a tree (树上第K小 LCA+主席树)
链接: https://www.spoj.com/problems/COT/en/ 思路: 首先看到求两点之前的第k小很容易想到用主席树去写,但是主席树处理的是线性结构,而这道题要求的是树形结构,我们 ...
- bzoj 2588 Spoj 10628. Count on a tree(主席树)
Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...
- 【bzoj2588】Spoj 10628. Count on a tree 离散化+主席树
题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始为0,即第一个 ...
- bzoj 2588: Spoj 10628. Count on a tree【主席树+倍增】
算是板子,把值离散化,每个点到跟上做主席树,然后查询的时候主席树上用u+v-lca-fa[lca]的值二分 #include<iostream> #include<cstdio> ...
- [Bzoj2588]Count on a tree(主席树+LCA)
Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...
- 【BZOJ2588】Count On a Tree(主席树)
[BZOJ2588]Count On a Tree(主席树) 题面 题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第 ...
- SPOJ 10628 Count on a tree(Tarjan离线LCA+主席树求树上第K小)
COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to ...
随机推荐
- Xamarin打包
- 有几个PAT
描述 字符串APPAPT中包含了两个单词“PAT”,其中第一个PAT是第2位(P),第4位(A),第6位(T):第二个PAT是第3位(P),第4位(A),第6位(T). 现给定字符串,问一共可以形成多 ...
- Python类__call__()方法
在python中,创建类型的时候定义了__call__()方法,那这个类型创建出来的实例就是可调用的.例def如: class A(object): def __init__(self,name,ag ...
- Cocos Creator 按钮音效封装重写
cc.Button.prototype._onTouchEnded = function (t) { cc.hb.audioMgr.playMusic("click", false ...
- Sublime 个人配置
Sublime 个人配置 用的faltland主题,之后还加了一些自己喜欢的东西. 效果图如下: { "always_show_minimap_viewport": true, & ...
- python小程序--Three(三级菜单)
#!/usr/bin/env python # _*_ coding:utf8 _*_ data = { "山东省":{ "滨州市":{"惠民县&qu ...
- web_app框架
web app 建立在asyncio的基础上,因此用aiohttp写一个基本的app.py import logging; logging.basicConfig(level=logging.INFO ...
- springmvc配置之mvc:annotation-driven
为了简化springmvc配置,spring同时引入了mvc namespace, 配置了 <mvc:annotation-driven/> spring会默认注册a RequestMap ...
- 记使用expo与expoKit分离工程遇到的坑
expoKit是支持expo平台的Objective-C和Java库,比纯RN一个个引入包开发效率会高一些,比如react-native-vector-icons包已经集成在expoKit中了. 假定 ...
- unix socket服务器
只能处理单个消息,一发一收. int loop(void) { struct sockaddr_un client_addr; int server_socket, client_socket; ...