「SP10628 COT - Count on a tree」
主席树的综合运用题.
前置芝士
具体做法
主席树维护每个点到根节点这一条链上不同树出现的次数,然后发现这个东西是可以相减的,于是这条链上每个数出现的次数就变成了\(sum[u]+sum[v]-2*sum[LCA(u,v)]\).然后就可以发现这个是错的,如果按这个式子计算最后的链上就没有LCA位置的值了,所以在范围包含\(val[LCA(u,v)]\)时需要加一,然后正常去找kth就好了.
代码
#include<bits/stdc++.h>
#define REP(i,first,last) for(int i=first;i<=last;++i)
#define DOW(i,first,last) for(int i=first;i>=last;--i)
using namespace std;
const int maxN=2e5+7;
int N,M,point[maxN];
int root[maxN];
int sor[maxN];
map<int,int>Hash;
int val[maxN];
//一下为主席树部分
int point_cnt=0;
struct Tree//对于树的一个结构体
{
int sum,lson,rson;
}tree[maxN*32];
//主席树标准define
#define LSON tree[now].lson
#define RSON tree[now].rson
#define MIDDLE ((left+right)>>1)
#define LEFT LSON,left,MIDDLE
#define RIGHT RSON,MIDDLE+1,right
#define NEW_LSON tree[new_tree].lson
#define NEW_RSON tree[new_tree].rson
void PushUp(int now)//合并信息
{
tree[now].sum=tree[LSON].sum+tree[RSON].sum;
}
void UpData(int num,int &new_tree,int now,int left=1,int right=N)
//修改并产生一个新版本到new_tree中
{
if(num<left||right<num)
{
new_tree=now;
return;
}
new_tree=++point_cnt;
if(left==right)
{
tree[new_tree].sum=tree[now].sum+1;
return;
}
UpData(num,NEW_LSON,LEFT);
UpData(num,NEW_RSON,RIGHT);
PushUp(new_tree);
}
int QueryKth(int k,int LCA_num,int tree1/*相对于式子中的u*/,int tree2/*相当于式子中的v*/,int tree3/*相当于式子中的LCA(u,v)*/,int left=1,int right=N)
{
if(left==right)
{
return left;
}
int sum=tree[tree[tree1].lson].sum
+tree[tree[tree2].lson].sum
-2*tree[tree[tree3].lson].sum;//计算出这条链上在left~right中的数出现的次数
if(left<=LCA_num&&LCA_num<=MIDDLE)//如果LCA位置的数在当前的范围内就加一
{
sum++;
}
if(sum>=k)//左子树查询kth
{
return
QueryKth(k,LCA_num,
tree[tree1].lson,
tree[tree2].lson,
tree[tree3].lson,
left,MIDDLE);
}
return//右子树查询
QueryKth(k-sum,LCA_num,
tree[tree1].rson,
tree[tree2].rson,
tree[tree3].rson,
MIDDLE+1,right);
}
//一下为链式前向星部分(非主要内容,不多讲)
int head[maxN];
int edge_cnt=0;
struct Edge
{
int to,next;
}edge[maxN];
#define FOR(now) for(int i_edge=head[now];i_edge;i_edge=edge[i_edge].next)
#define TO edge[i_edge].to
void AddEdge(int form,int to)
{
edge[++edge_cnt].to=to;
edge[edge_cnt].next=head[form];
head[form]=edge_cnt;
}
//一下为倍增LCA部分(非主要内容,不多讲)
int kth_father[maxN][25];
bool visit[maxN];
int deep[maxN],father[maxN];
void DFS(int now)
{
UpData(Hash[point[now]],root[now],root[father[now]]);//在DFS的过程中把主席树建好
deep[now]=deep[father[now]]+1;
kth_father[now][0]=father[now];
REP(i,0,22)
{
kth_father[now][i+1]=kth_father[kth_father[now][i]][i];
}
FOR(now)
{
if(father[now]!=TO)
{
father[TO]=now;
DFS(TO);
}
}
}
int LCA(int x,int y)//LCA核心代码
{
if(deep[x]<deep[y])
{
swap(x,y);
}
DOW(i,22,0)
{
if(deep[kth_father[x][i]]>=deep[y])
{
x=kth_father[x][i];
}
if(x==y)
{
return x;
}
}
DOW(i,22,0)
{
if(kth_father[x][i]!=kth_father[y][i])
{
x=kth_father[x][i];
y=kth_father[y][i];
}
}
return father[x];
}
int main()
{
scanf("%d%d",&N,&M);
REP(i,1,N)
{
scanf("%d",&point[i]);
sor[i]=point[i];
}
int fa,son;
REP(i,1,N-1)
{
scanf("%d%d",&fa,&son);
AddEdge(fa,son);
AddEdge(son,fa);
}
sort(sor+1,sor+1+N);//离散化
sor[0]=-114514;
int cnt_num=0;
REP(i,1,N)
{
if(sor[i]!=sor[i-1])
{
Hash[sor[i]]=++cnt_num;
val[cnt_num]=sor[i];
}
}
N=cnt_num;
DFS(1);//DFS,建树+倍增LCA预处理
int u,v,k;
int point_LCA;
REP(i,1,M)
{
scanf("%d%d%d",&u,&v,&k);
point_LCA=LCA(u,v);//找到LCA
printf("%d\n",
val[QueryKth(k,Hash[point[point_LCA]],
root[u],root[v],root[point_LCA])]
);//直接查找kth
}
return 0;
}
「SP10628 COT - Count on a tree」的更多相关文章
- SP10628 COT - Count on a tree 主席树
Code: #include<cstdio> #include<cstring> #include<algorithm> #include<string> ...
- 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 ...
- 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 主席树
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 (树上第K小 LCA+主席树)
链接: https://www.spoj.com/problems/COT/en/ 思路: 首先看到求两点之前的第k小很容易想到用主席树去写,但是主席树处理的是线性结构,而这道题要求的是树形结构,我们 ...
- SPOJ 10628. SPOJ COT Count on a tree 可持久化线段树
这题是裸的主席树,每个节点建一棵主席树,再加个lca就可以了. 历尽艰辛,终于A掉了这一题,这般艰辛也显示出了打代码的不熟练. 错误:1.lca倍增的时候i和j写反了,RE了5次,实在要吸取教训 2. ...
- SPOJ COT Count on a tree(树上主席树 + LCA 求点第k小)题解
题意:n个点的树,每个点有权值,问你u~v路径第k小的点的权值是? 思路: 树上主席树就是每个点建一棵权值线段树,具体看JQ博客,LCA用倍增logn求出,具体原理看这里 树上主席树我每个点的存的是点 ...
- spoj COT - Count on a tree(主席树 +lca,树上第K大)
您将获得一个包含N个节点的树.树节点的编号从1到Ñ.每个节点都有一个整数权重. 我们会要求您执行以下操作: uvk:询问从节点u到节点v的路径上的第k个最小权重 输入 在第一行中有两个整数Ñ和中号.( ...
- 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 ...
随机推荐
- selenium 使用close和quit关闭driver的不同点
Driver.Quit()与Driver.Close()的不同:Driver.Quit(): Quit this dirver, closing every associated windows;Dr ...
- 微信支付之H5支付
HoJe男孩子你要加油阿 前言准备材料H5支付请求的参数返回结果统一下单回调接口用到的工具类886 . 前言 大家好,分享是快乐的,也见证了个人成长历程,文章大多都是工作经验总结以及平时学习积累,基于 ...
- js判断json对象是否包含key
if(json.hasOwnProperty("KEY")){ }
- python调用c++开发的动态库
此处列举一下python调用Windows端动态库. # *- coding=utf-8 -* import ctypes from ctypes import * import os objdll ...
- linux中利用fstab实现磁盘分区自动挂载
如何格式化磁盘.给磁盘分区以及挂载,参考我的另一篇博客: https://www.cnblogs.com/mediocreWorld/p/11123786.html 博客中有一个格式化分区的命令: m ...
- 【CSS属性#1】
" 目录 一.宽和高 二.字体属性 1. 文字字体 font-famlly 2. 字体大小 font-size 3. 字重(粗细) font-weight 4. 文本颜色 color 三.字 ...
- HeroM2连击技能设置和DB完整数据
连击技能设置: M2\选项\功能设置\技能魔法\通用技能\连击技能 魔法DB: 81;倚天辟地;0;55;5;10;10;5;6;6;99;15;5;15;10;15;15;60;; 300;万剑归宗 ...
- MySQL8.0.11安装后,使用CMD无法启动mysql服务
首先,先把mysql的bin路径添加到系统环境变量 这样做可以,直接进入CMD后执行mysql服务,不需要进入mysql的bin文件路径去执行. 第一步:在MySQL的安装文件的bin目录(例如:C: ...
- Python - int()
参考 https://docs.python.org/3/library/functions.html?highlight=int#int If x is not a number or if bas ...
- 11. 搭建一个完整的K8S集群
11. 搭建一个完整的Kubernetes集群 1. kubectl的命令遵循分类的原则(重点) 语法1: kubectl 动作 类 具体的对象 例如: """ kube ...