bzoj 1803: Spoj1487 Query on a tree III(主席树)
题意
你被给定一棵带点权的n个点的有根数,点从1到n编号。
定义查询 query(x,k): 寻找以x为根的k大点的编号(从小到大排序第k个点)
假设没有两个相同的点权。
输入格式: 第一行为整数n,第二行为点权,接下来n-1行为树边,接下来一行为整数m,下面m行为两个整数x,k,代表query(x,k)
输出格式: m行,输出每次查询的结果。
题解
先一遍dfs,然后建个主席树,带上去直接跑一跑就好了
我忘了注意dfs序的位置和原来的编号……结果调了半天啥都调不出来……
//minamoto
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define getc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[<<],*p1=buf,*p2=buf;
inline int read(){
#define num ch-'0'
char ch;bool flag=;int res;
while(!isdigit(ch=getc()))
(ch=='-')&&(flag=true);
for(res=num;isdigit(ch=getc());res=res*+num);
(flag)&&(res=-res);
#undef num
return res;
}
char obuf[<<],*o=obuf;
inline void print(int x){
if(x>) print(x/);
*o++=x%+;
}
const int N=,M=N*;
int sum[M],L[M],R[M],rt[N];
int ver[N<<],Next[N<<],head[N];
int ls[N],rs[N],a[N],b[N],id[N],pos[N];
int n,m,cnt,tot,q;
void update(int last,int &now,int l,int r,int x){
sum[now=++cnt]=sum[last]+;
if(l==r) return;
int mid=(l+r)>>;
if(x<=mid) R[now]=R[last],update(L[last],L[now],l,mid,x);
else L[now]=L[last],update(R[last],R[now],mid+,r,x);
}
int query(int u,int v,int l,int r,int k){
if(l>=r) return l;
int x=sum[L[v]]-sum[L[u]];
int mid=(l+r)>>;
if(x>=k) return query(L[u],L[v],l,mid,k);
else return query(R[u],R[v],mid+,r,k-x);
}
inline void add(int u,int v){
ver[++tot]=v,Next[tot]=head[u],head[u]=tot;
ver[++tot]=u,Next[tot]=head[v],head[v]=tot;
}
void dfs(int u,int fa){
a[ls[u]=++m]=b[u],id[m]=u;
for(int i=head[u];i;i=Next[i])
if(ver[i]!=fa) dfs(ver[i],u);
rs[u]=m;
}
int main(){
//freopen("testdata.in","r",stdin);
n=read();
for(int i=;i<=n;++i) b[i]=read();
for(int i=;i<n;++i){
int u,v;
u=read(),v=read();
add(u,v);
}
dfs(,);
sort(b+,b++m);
for(int i=;i<=n;++i){
int k=lower_bound(b+,b++m,a[i])-b;
update(rt[i-],rt[i],,m,k);
pos[k]=id[i];
}
q=read();
while(q--){
int u=read(),k=read();
int ans=pos[query(rt[ls[u]-],rt[rs[u]],,m,k)];
print(ans),*o++='\n';
}
fwrite(obuf,o-obuf,,stdout);
return ;
}
bzoj 1803: Spoj1487 Query on a tree III(主席树)的更多相关文章
- 【BZOJ1803】Spoj1487 Query on a tree III 主席树+DFS序
[BZOJ1803]Spoj1487 Query on a tree III Description You are given a node-labeled rooted tree with n n ...
- SP1487 PT07J - Query on a tree III (主席树)
SP1487 PT07J - Query on a tree III 题意翻译 你被给定一棵带点权的n个点的有根数,点从1到n编号. 定义查询 query(x,k): 寻找以x为根的k大点的编号(从小 ...
- BZOJ1803Spoj1487 Query on a tree III——主席树
题目大意 给一棵有点权的n个点的有根树,保证任意两点的点权不同,m次询问每次询问x的子树中权值第k大的点. 输入 先输入n,然后每个点点权,再输入n-1行每行两个数x,y代表x和y相连,再输入m,之后 ...
- SP1487 PT07J - Query on a tree III 主席树+dfs序
Code: #include<iostream> #include<cstdio> #include<algorithm> #include<string&g ...
- BZOJ_1803_Spoj1487 Query on a tree III_主席树+dfs序
BZOJ_1803_Spoj1487 Query on a tree III_主席树 Description You are given a node-labeled rooted tree with ...
- SPOJ PT07J - Query on a tree III(划分树)
PT07J - Query on a tree III #tree You are given a node-labeled rooted tree with n nodes. Define the ...
- 【bzoj1803】Spoj1487 Query on a tree III DFS序+主席树
题目描述 You are given a node-labeled rooted tree with n nodes. Define the query (x, k): Find the node w ...
- [SPOJ-PT07J] Query on tree III (主席树)
题意翻译 你被给定一棵带点权的n个点的有根数,点从1到n编号. 定义查询 query(x,k): 寻找以x为根的k大点的编号(从小到大排序第k个点) 假设没有两个相同的点权. 输入格式: 第一行为整数 ...
- bzoj1803: Spoj1487 Query on a tree III
Description You are given a node-labeled rooted tree with n nodes. Define the query (x, k): Find the ...
随机推荐
- 软Raid5制作
以raid5为例: 1.添加4块磁盘要求:容量.转速.接口一样的硬盘. 2.创建分区并修改ID[root@localhost ~]# fdisk /dev/sdb[root@localhost ~]# ...
- matplotlib 28原则
记下各个简易模板,方便ctrl+c和ctrl+v 子图: import numpy as np import matplotlib.pyplot as plt x = np.array(range(1 ...
- 应用程序发生异常 unknown software exception (0xc00000fd)... - 栈溢出(Stack overflow)
今天在写程序的时候,弹出这样的提示对话框: 应用程序发生异常 unknown software exception (0xc00000fd): 相关代码是这样,在一个函数中读取一个csv文件,先根据这 ...
- mysql自定义函数与过程中写法的注意事项
BEGIN #Routine body goes here... /* update szzx_goods_common set gc_id=i where gc_name=(SELECT gc_na ...
- hibernate中复合主键的使用
转: https://blog.csdn.net/shutingwang/article/details/6627730 https://blog.csdn.net/lmy86263/article/ ...
- Ansible playbook 批量修改服务器密码 先普通后root用户
fsckzy Ansible playbook 批量修改服务器密码 客户的需求:修改所有服务器密码,密码规则为Rfv5%+主机名后3位 背景:服务器有CentOS6.7,SuSE9.10.11,r ...
- SQL Server sp_executesql介绍和使用
execute相信大家都用的用熟了,简写为exec,除了用来执行存储过程,一般都用来执行动态Sql sp_executesql,sql2005中引入的新的系统存储过程,也是用来处理动态sql的, 如: ...
- 关于表单----html杂记
前言:总结了一些关于表单的东西,发下内心的感慨,以前感觉自己什么都会,今天竟然连最基本的表单的东西都忘了,好丢人, 学习的过程中,切勿眼高手低,一定要做好自己的笔记,然后多写代码,多想为什么,我比较笨 ...
- mysql5.6下载及安装(超级详细)
经过本人的实验,验证这些东西都是OK的 博客原文地址:http://www.cnblogs.com/Percy_Lee/p/5282187.html
- MySQL中的时态(日期/时间)数据类型
时态类型的取值范围 mysql> create table t (dt datetime,d date,t time); Query OK, 0 rows affected (0.30 sec) ...