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 ...
随机推荐
- vscode中go插件配置
# 转自:http://www.mamicode.com/info-detail-2436665.html # https://blog.csdn.net/bing2011/article/detai ...
- Feign 发送对象,对象含多个文件
Feign在发送文件时,可以使用Feign-form. 另一种方式,关键就是,要将文件转成Resource,然后使用Spring的MultivalueMap 本次发送的是个对象,对象里含有 文件对象数 ...
- ajax跨域的解决办法
<!DOCTYPE HTML> <html> <head> <meta http-equiv="content-type" content ...
- Codeforces 375D - Tree and Queries(dfs序+莫队)
题目链接:http://codeforces.com/contest/351/problem/D 题目大意:n个数,col[i]对应第i个数的颜色,并给你他们之间的树形关系(以1为根),有m次询问,每 ...
- Kaggle:Titanic: Machine Learning from Disaster
一直想着抓取股票的变化,偶然的机会在看股票数据抓取的博客看到了kaggle,然后看了看里面的题,感觉挺新颖的,就试了试. 题目如图:给了一个train.csv,现在预测test.csv里面的Passa ...
- 四、ansible主机组定义
1.打开hosts文件 vim /etc/ansible/hosts 2.定义一个主机组 [web-server] 192.168.1.1 3.定义多个组(继承) [web:children] web ...
- go中操作json
package main import ( "encoding/json" "fmt" ) type Server struct { ServerName st ...
- C# 6.0 新特性 (四)
原文: 1.http://www.cnblogs.com/BoyceYang/p/3711343.html 2.http://www.cnblogs.com/lhking/p/3660182.html ...
- vue js moment.js 过滤了双休日和法定节假日
源码:注!原创的!!!! <template> <div id="DATE"> <ul class="dateForm" @cha ...
- 微软企业库5.0 学习之路——扩展学习篇、库中的依赖关系注入(重构 Microsoft Enterprise Library)[转]
这篇文章是我在patterns & practices看到的一篇有关EntLib5.0的文章,主要介绍了EntLib5.0的这次的架构变化由来,觉得很不错,大家可以看一下! 在过去几年中,依赖 ...