SP10628 COT - Count on a tree 主席树
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 主席树的更多相关文章
- 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 ...
- spoj COT - Count on a tree(主席树 +lca,树上第K大)
您将获得一个包含N个节点的树.树节点的编号从1到Ñ.每个节点都有一个整数权重. 我们会要求您执行以下操作: uvk:询问从节点u到节点v的路径上的第k个最小权重 输入 在第一行中有两个整数Ñ和中号.( ...
- 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 ...
- 【BZOJ-2588】Count on a tree 主席树 + 倍增
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 3749 Solved: 873[ ...
- 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 ...
- 【BZOJ2588】Spoj 10628. Count on a tree 主席树+LCA
[BZOJ2588]Spoj 10628. Count on a tree Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lasta ...
- 洛谷P2633/bzoj2588 Count on a tree (主席树)
洛谷P2633/bzoj2588 Count on a tree 题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K ...
- 洛谷P2633 Count on a tree(主席树上树)
题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始为0,即第一个 ...
- 洛谷 P2633 Count on a tree 主席树
在一棵树上,我们要求点 $(u,v)$ 之间路径的第$k$大数. 对于点 $i$ ,建立 $i$ 到根节点的一棵前缀主席树. 简单容斥后不难得出结果为$sumv[u]+sumv[v]−sumv[l ...
随机推荐
- Navicat for Mysql 关于1130错误,无法正常方法解决的解决办法。
本人因为失误操作,不小心将mysql 玩崩了.导致一直报1130错误,无法进入. 看了很多网上的帖子,但是那些办法都行不通.最后通过好友的指点,解决了这个问题. 1.停止MySQL服务,执行net ...
- 「JavaSE 重新出发」05.02 泛型数组列表、包装类
泛型数组列表 ArrayList 是一个采用类型参数(type parameter)的泛型类(generic class). java ArrayList<Employee> staff ...
- 04《UML大战需求分析》之四
在学习完顺序图之后,流程分析的三种图,我已经学习完了我,但是我还需要大量地锻炼,这样才可以更加熟练地掌握几种图的使用和方法.接下来,我学习了用例图,用来描述系统的行为. 虽然是一同学习的,但是对用例图 ...
- table标签 在谷歌和ie浏览器下不同的表现效果
在项目中有了一个这样的需求: 我需要利用vue的模板语法v-for循环生成tr,这个tr是需要双重循环来确定其个数的, 我的实现: 我在tr外面包了一个template标签, 效果: 谷歌浏览器下实现 ...
- html css 样式中100%width 仍有白边解决办法
把 <body >改成<body style="margin=0%">
- 池(Pool)
#1 就是一个资源的集合,用的时候按照你的需要去取,用完了给人家放回去 #2 学编程的时候,老师给我们的解释过池的意思,大概是: 如果你喝水,你可以拿杯子去水龙头接.如果很多人喝水,那就只能排队去接. ...
- javascript编程风格(粗略笔记)
1.空格 紧凑型: project.MyClass = function(arg1, arg2){ 松散型: for( i = 0; i < length; i++ ){ 2.代码行长度 最多8 ...
- 安装laravel项目出错
我的本地是7.0.1,而下载下来的原来是运行在7.0.8版本的,于是执行:composer install 时,报了一堆错.解决办法:删掉composer.lock,重新composer instal ...
- [BZOJ1322]Destroying The Graph
题目大意:有一张有向图,对于每个点,有两种操作:1. 删掉它的所有入边2. 删掉它的所有出边对每个点的每个操作均有不同的价值.求使得图上没有边的最小价值.解题思路:考虑把点拆成入点和出点,然后就是二分 ...
- hdu 1693 插头dp入门
hdu1693 Eat the Trees 题意 在\(n*m\)的矩阵中,有些格子有树,没有树的格子不能到达,找一条或多条回路,吃完所有的树,求有多少种方法. 解法 这是一道插头dp的入门题,只需要 ...