SPOJ 10628. SPOJ COT Count on a tree 可持久化线段树
这题是裸的主席树,每个节点建一棵主席树,再加个lca就可以了。
历尽艰辛,终于A掉了这一题,这般艰辛也显示出了打代码的不熟练。
错误:1、lca倍增的时候i和j写反了,RE了5次,实在要吸取教训
2、主席树插入操作的时候,如果插入到的那个点(叶节点)原来有值,而没有加上,导致了WA
以下是历尽艰辛的代码,还很长。
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <map> using namespace std; const int maxn = ;
int n, m, w[maxn];
int head[maxn], label;
struct Edge
{
int v, next;
Edge (int v = , int next = ):
v(v), next(next) {}
}e[maxn*];
int depth[maxn], f[maxn][], root[maxn], temp[maxn], s_num;
queue <int> q;
map <int, int> id;
struct Tree
{
int sum[maxn*], ls[maxn*], rs[maxn*], cnt;
Tree ()
{
sum[] = , cnt = ;
}
void PushUp(int rt)
{
sum[rt] = sum[ls[rt]]+sum[rs[rt]];
}
void insert(int las_rt, int rt, int l, int r, int p, int d)
{
if (l == r)
{
sum[rt] = sum[las_rt]+d;
return ;
}
int mid = (l+r)>>;
if (p <= mid)
{
ls[rt] = ++cnt, rs[rt] = rs[las_rt];
insert(ls[las_rt], ls[rt], l, mid, p, d);
}
else
{
ls[rt] = ls[las_rt], rs[rt] = ++cnt;
insert(rs[las_rt], rs[rt], mid+, r, p, d);
}
PushUp(rt);
}
int query(int u_rt, int v_rt, int lca_rt, int lca_pos, int l, int r, int k)
{
if (l == r)
return l;
int mid = (l+r)>>;
int s_l = sum[ls[u_rt]]+sum[ls[v_rt]]-*sum[ls[lca_rt]]+(lca_pos >= l && lca_pos <= mid);
if (k <= s_l)
return query(ls[u_rt], ls[v_rt], ls[lca_rt], lca_pos, l, mid, k);
else
return query(rs[u_rt], rs[v_rt], rs[lca_rt], lca_pos, mid+, r, k-s_l);
}
}T; void ins(int u, int v)
{
e[++label] = Edge(v, head[u]);
head[u] = label;
} void in()
{
scanf("%d %d", &n, &m);
for (int i = ; i <= n; ++i)
scanf("%d", &w[i]);
for (int i = ; i <= n; ++i)
head[i] = -;
label = -;
for (int i = ; i < n; ++i)
{
int u, v;
scanf("%d %d", &u, &v);
ins(u, v), ins(v, u);
}
} void Build_lca()
{
for (int i = ; i <= ; ++i)
for (int j = ; j <= n; ++j)
f[j][i] = -;
q.push();
depth[] = ;
f[][] = -;
while (!q.empty())
{
int u = q.front();
for (int i = head[u]; i != -; i = e[i].next)
{
int v = e[i].v;
if (v == f[u][])
continue ;
f[v][] = u;
depth[v] = depth[u]+;
q.push(v);
}
q.pop();
}
for (int i = ; i <= ; ++i)
for (int j = ; j <= n; ++j)
{
if (f[j][i-] == -)
continue ;
f[j][i] = f[f[j][i-]][i-];
}
} void Hash_a()
{
for (int i = ; i <= n; ++i)
temp[i] = w[i];
sort(temp+, temp+n+);
s_num = ;
for (int i = ; i <= n; ++i)
if (temp[i] != temp[i-] || i == )
{
temp[++s_num] = temp[i];
id[temp[i]] = s_num;
}
} void dfs(int u)
{
root[u] = ++T.cnt;
T.insert(u == ? : root[f[u][]], root[u], , s_num, id[w[u]], );
for (int i = head[u]; i != -; i = e[i].next)
{
int v = e[i].v;
if (v == f[u][])
continue ;
dfs(v);
}
} void Build_tree()
{
Hash_a();
dfs();/*
for (int i = 1; i <= n; ++i)
printf("%d %d\n", i, T.sum[root[i]]);*/
} void prepare()
{
Build_lca();
Build_tree();
} int lca(int u, int v)
{
if (depth[u] < depth[v])
swap(u, v);
for (int i = ; i >= ; --i)
{
if (f[u][i] == -)
continue ;
if (depth[f[u][i]] >= depth[v])
{
u = f[u][i];
if (depth[u] == depth[v])
break ;
}
}
if (u == v)
return u;
for (int i = ; i >= ; --i)
{
if (f[u][i] == -)
continue ;
if (f[u][i] != f[v][i])
{
u = f[u][i];
v = f[v][i];
}
}
return f[u][];
} void work()
{
prepare();
while (m --)
{
int u, v, k;
scanf("%d %d %d", &u, &v, &k);
int t = lca(u, v);
int pos = T.query(root[u], root[v], root[t], id[w[t]], , s_num, k);
printf("%d\n", temp[pos]);
}
} int main()
{
in();
work();
return ;
}
SPOJ 10628. SPOJ COT Count on a tree 可持久化线段树的更多相关文章
- BZOJ - 2588 Spoj 10628. Count on a tree (可持久化线段树+LCA/树链剖分)
题目链接 第一种方法,dfs序上建可持久化线段树,然后询问的时候把两点之间的所有树链扒出来做差. #include<bits/stdc++.h> using namespace std; ...
- SPOJ COT Count on a tree(树上主席树 + LCA 求点第k小)题解
题意:n个点的树,每个点有权值,问你u~v路径第k小的点的权值是? 思路: 树上主席树就是每个点建一棵权值线段树,具体看JQ博客,LCA用倍增logn求出,具体原理看这里 树上主席树我每个点的存的是点 ...
- SPOJ 10628 COT - Count on a tree(在树上建立主席树)(LCA)
COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to ...
- SPOJ - COT Count on a tree
地址:http://www.spoj.com/problems/COT/en/ 题目: COT - Count on a tree #tree You are given a tree with N ...
- 主席树 || 可持久化线段树 || LCA || BZOJ 2588: Spoj 10628. Count on a tree || Luogu P2633 Count on a tree
题面: Count on a tree 题解: 主席树维护每个节点到根节点的权值出现次数,大体和主席树典型做法差不多,对于询问(X,Y),答案要计算ans(X)+ans(Y)-ans(LCA(X,Y) ...
- BZOJ 2588: Spoj 10628. Count on a tree-可持久化线段树+LCA(点权)(树上的操作) 无语(为什么我的LCA的板子不对)
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 9280 Solved: 2421 ...
- 主席树[可持久化线段树](hdu 2665 Kth number、SP 10628 Count on a tree、ZOJ 2112 Dynamic Rankings、codeforces 813E Army Creation、codeforces960F:Pathwalks )
在今天三黑(恶意评分刷上去的那种)两紫的智推中,突然出现了P3834 [模板]可持久化线段树 1(主席树)就突然有了不详的预感2333 果然...然后我gg了!被大佬虐了! hdu 2665 Kth ...
- 「SP10628 COT - Count on a tree」
主席树的综合运用题. 前置芝士 可持久化线段树:其实就是主席树了. LCA:最近公共祖先,本题需要在\(\log_2N\)及以内的时间复杂度内解决这个问题. 具体做法 主席树维护每个点到根节点这一条链 ...
- SPOJ Meteors - 可持久化线段树 - 二分法
Byteotian Interstellar Union (BIU) has recently discovered a new planet in a nearby galaxy. The plan ...
随机推荐
- 33、re的match和search区别?
1.match()函数只检测RE是不是在string的开始位置匹配,search()会扫描整个string查找匹配:2.也就是说match()只有在0位置匹配成功的话才有返回,如果不是开始位置匹配成功 ...
- python中赋值、浅拷贝、深拷贝详解(转)
一.赋值 >>> a = [1, 2, 3]>>> b = a>>> print(id(a), id(b), sep='\n')139701469 ...
- (总结)MySQL自带的性能压力测试工具mysqlslap详解
PS:今天一同事问我有木有比较靠谱的mysql压力测试工具可用.其实mysql自带就有一个叫mysqlslap的压力测试工具,还是模拟的不错的.下面举例说说.mysqlslap是从5.1.4版开始的一 ...
- 20165301 预备作业三:Linux安装及命令入门
预备作业三:Linux安装及命令入门 VirtualBox虚拟机的安装 在进行安装之前,原本以为有了娄老师的安装教程会是一件很容易的事情.万万没想到,在自己实际动手操作中,还是遇到了许多困难.通过与同 ...
- Construct Binary Tree from Inorder and Postorder Traversal (&&Preorder and Inorder Traversal )——数据结构和算法的基本问题
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...
- css 资料链接
https://tink.gitbooks.io/fe-collections/content/ch03-css/float.html https://css-tricks.com/almanac/p ...
- Mariadb 10.2中的json使用及应用场景思考
-- 创建示例表DROP TABLE IF EXISTS `t_base_user`;CREATE TABLE `t_base_user` ( `USER_ID` char(36) CHARACT ...
- Asp.net vNext 学习之路(一)
概述 asp.net vNext 也叫 asp.net 5.0,意思是微软推出的下一个版本的asp.net.可以说是微软对asp.net的一个比较重大的重新设计, asp.net vNext是一 个比 ...
- 记录自己在 cmd 中执行 jar 文件遇到的一些错误
记录自己在 cmd 中执行 jar 文件遇到的一些错误 场景: 请求接口,解析接口返回的 JSON 字符串并插入到我们的数据库里面. 情况: 项目在 eclipse 中正常运行,打成 jar 包后在 ...
- ref:Java安全之反序列化漏洞分析(简单-朴实)
ref:https://mp.weixin.qq.com/s?__biz=MzIzMzgxOTQ5NA==&mid=2247484200&idx=1&sn=8f3201f44e ...