spoj COT - Count on a tree(主席树 +lca,树上第K大)
您将获得一个包含N个节点的树。树节点的编号从1到Ñ。每个节点都有一个整数权重。
我们会要求您执行以下操作:
- uvk:询问从节点u到节点v的路径上的第k个最小权重
输入
在第一行中有两个整数Ñ和中号。(N,M <= 100000)
在第二行中有N个整数。第i个整数表示第i个节点的权重。
在接下来的N-1行中,每行包含两个整数u v,它描述了一个边(u,v)。
在接下来的M行中,每行包含三个整数u v k,这意味着要求从节点u到节点v的路径上的第k个最小权重的操作。
解题思路:
首先对于求第K小的问题 我们可以用主席树搞 ,没有问题,
但是对于一个树形结构,我们需要将其转化为线性,然后需要树剖才能做.
然后考虑链上的第k值怎么维护 ,
发现如果树剖计算的话 维护不了啊
因为(u,v)的路 可能在很多个链上,那么不能对每个求第K值,这样明显是错误的啊,
然后我们知道主席树其实就是维护了一个前缀和
那么我们可以对每一个节点到根节点建立前缀和,就能找任意一个节点到根节点的第K值,
那么根据主席树的性质,我们就能够计算(u,v)的路上的第K值了
只要在查询的时候稍改变一下就行了
cnt = sum[ls[u]]+sum[ls[v]]-sum[ls[lca(u,v)]]-sum[ls[fa[lca(u,v)]]];
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<map>
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+;
typedef long long LL;
int rt[N*], ls[N*], rs[N*], sum[N*];
int fa[*N][], dep[*N], vis[N];
int a[N], b[N], tot, cnt, head[N], len;
struct node
{
int to, next;
} p[*N];
void init()
{
memset(head,-,sizeof(head));
memset(vis,,sizeof(vis));
cnt=;
return ;
}
void add(int u,int v)
{
p[cnt].to=v,p[cnt].next=head[u];head[u]=cnt++;
p[cnt].to=u,p[cnt].next=head[v];head[v]=cnt++;
return ;
}
void build(int &o,int l,int r)
{
o= ++tot,sum[o]=;
if(l==r) return ;
int mid=(l+r)/;
build(ls[o],l,mid);
build(rs[o],mid+,r);
return ;
}
void update(int &o,int l,int r,int last,int p)
{
o= ++tot;
ls[o]=ls[last],rs[o]=rs[last];
sum[o]=sum[last]+;
if(l==r) return ;
int mid=(l+r)/;
if(p<=mid) update(ls[o],l,mid,ls[last],p);
else update(rs[o],mid+,r,rs[last],p);
return ;
}
int query(int ss,int tt,int s1,int t1,int l,int r,int cnt)
{
if(l==r) return l;
int tmp=sum[ls[tt]]+sum[ls[ss]]-sum[ls[s1]]-sum[ls[t1]];
int mid=(l+r)/;
if(tmp>=cnt) return query(ls[ss],ls[tt],ls[s1],ls[t1],l,mid,cnt);
else return query(rs[ss],rs[tt],rs[s1],rs[t1],mid+,r,cnt-tmp);
}
void dfs(int u,int d,int f,int root)
{
vis[u]=,dep[u]=d,fa[u][]=f;
update(rt[u],,len,root,a[u]);
root=rt[u];
for(int i=head[u];i!=-;i=p[i].next)
{
int v=p[i].to;
if(vis[v]) continue;
dfs(v,d+,u,root);
}
return ;
}
void lca(int n)
{
int k=(int)(log(1.0*n)/log(2.0));
for(int i=;i<=k;i++)
{
for(int j=;j<=n;j++)
{
fa[j][i]=fa[fa[j][i-]][i-];
}
}
return ;
}
int get(int x,int y,int n)
{
if(dep[x]<dep[y]) swap(x,y);
int k=(int)(log(1.0*n)/log(2.0));
int d=dep[x]-dep[y];
for(int i=;i<=k;i++)
if((d&(<<i))) x=fa[x][i];
if(x==y) return x;
for(int i=k;i>=;i--)
{
if(fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i];
}
return fa[x][];
} int main()
{
int t, n, q;
scanf("%d %d", &n, &q);
for(int i=; i<=n; i++) scanf("%d", &a[i]), b[i]=a[i];
sort(b+,b+n+);
len=unique(b+,b+n+)-(b+);
tot=;
build(rt[],,len);
for(int i=; i<=n; i++) a[i]=lower_bound(b+,b+len+,a[i])-(b);
init();
for(int i=;i<n-;i++)
{
int x, y;
scanf("%d %d", &x, &y);
add(x,y);
}
dfs(,,,rt[]);
lca(n);
while(q--)
{
int l, r, x;
scanf("%d %d %d", &l, &r, &x);
int pos=get(l,r,n);
printf("%d\n",b[query(rt[l],rt[r],rt[pos],rt[fa[pos][]],,len,x)]);
}
return ;
}
spoj COT - Count on a tree(主席树 +lca,树上第K大)的更多相关文章
- 【BZOJ2588】Spoj 10628. Count on a tree 主席树+LCA
[BZOJ2588]Spoj 10628. Count on a tree Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lasta ...
- 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 ...
- BZOJ 2588: Spoj 10628. Count on a tree 主席树+lca
分析:树上第k小,然后我想说的是主席树并不局限于线性表 详细分析请看http://www.cnblogs.com/rausen/p/4006116.html,讲的很好, 然后因为这个熟悉了主席树,真是 ...
- BZOJ.2588.Count on a tree(主席树 静态树上第k小)
题目链接 /* 序列上的主席树 某点是利用前一个点的根建树 同理 树上的主席树 某个节点可以利用其父节点(is unique)的根建树 排名可以利用树上前缀和求得: 对于(u,v),w=LCA(u,v ...
- 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: 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 ...
- HDU 4729 An Easy Problem for Elfness (主席树,树上第K大)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 题意:给出一个带边权的图.对于每一个询问(S , ...
- SP10628 COT - Count on a tree 主席树
Code: #include<cstdio> #include<cstring> #include<algorithm> #include<string> ...
- [bzoj2588][count on a tree] (主席树+lca)
Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...
随机推荐
- MongoDB中的查询
MongoDB中文文档:http://docs.mongoing.com/manual-zh/contents.html 这里以集合名称为test为例,数据库通过for循环插入一些测试数据,键分别为: ...
- Caused by: java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor
Bitmap bmp =BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); Paint paint = new ...
- FTP批量下载数据文件
包含ftp的命令脚本,建立临时文件. ::服务器连接信息 set username=root set password=root set ip=xxx.xxx.xxx.xxx set RemoteDi ...
- HDU 4348(主席树 标记永久化)
题面一看就是裸的数据结构题,而且一看就知道是主席树... 一共四种操作:1:把区间[l, r]的数都加上d,并且更新时间.2:查询当前时间的区间和.3:查询历史时间的区间和.4:时光倒流到某个时间. ...
- 通过id设置的css属性和通过元素设置的css属性冲突了,优先级哪个高?
---恢复内容开始--- <!DOCTYPE html> <html> <head> <title>div test</title> < ...
- 长城防火墙(GFW)
一.简介 中国防火长城,官方名为金盾工程,是由政府运作的一个互联网审查监控项目.在其管辖互联网内部建立的多套网络审查系统的总称,包括相关行政审查系统.其英文名称Great Firewall of Ch ...
- 多线程学习-基础( 九)线程同步Synchronized关键字
一.线程同步1.synchronized关键字的作用域有二种:(1)某个对象实例内:synchronized aMethod(){}可以防止多个线程同时访问这个对象的synchronized方法(如果 ...
- Python入门:模拟登录(二)或注册之requests处理带token请求
转自http://blog.csdn.net/foryouslgme/article/details/51822209 首先说一下使用Python模拟登录或注册时,对于带token的页面怎么登录注册模 ...
- yzm10的小简介
yzm10,退役OIer,现役ACMer.大学生一枚,从高中开始接触编程(入门pascal...),过程基本自学,蒟蒻一只,在各大比赛划水打酱油..15年水得noip联二后退役,结束OI之旅.也是在1 ...
- Windows 安装 mysql-5.7.12-winx64(CommunityServer) 备忘
下载后文件名 mysql-5.7.12-winx64.msi 以为还是会和以往一样,一路next傻瓜式安装并完成设置,然后启动运行,但实际情况不是,感觉就是一解压过程,然后点击The MySQL Co ...