题意

你被给定一棵带点权的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(主席树)的更多相关文章

  1. 【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 ...

  2. SP1487 PT07J - Query on a tree III (主席树)

    SP1487 PT07J - Query on a tree III 题意翻译 你被给定一棵带点权的n个点的有根数,点从1到n编号. 定义查询 query(x,k): 寻找以x为根的k大点的编号(从小 ...

  3. BZOJ1803Spoj1487 Query on a tree III——主席树

    题目大意 给一棵有点权的n个点的有根树,保证任意两点的点权不同,m次询问每次询问x的子树中权值第k大的点. 输入 先输入n,然后每个点点权,再输入n-1行每行两个数x,y代表x和y相连,再输入m,之后 ...

  4. SP1487 PT07J - Query on a tree III 主席树+dfs序

    Code: #include<iostream> #include<cstdio> #include<algorithm> #include<string&g ...

  5. 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 ...

  6. 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 ...

  7. 【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 ...

  8. [SPOJ-PT07J] Query on tree III (主席树)

    题意翻译 你被给定一棵带点权的n个点的有根数,点从1到n编号. 定义查询 query(x,k): 寻找以x为根的k大点的编号(从小到大排序第k个点) 假设没有两个相同的点权. 输入格式: 第一行为整数 ...

  9. 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 ...

随机推荐

  1. pandas 从入门到遗忘

    读取大文件(内存有限): import pandas as pd reader = pd.read_csv("tap_fun_test.csv", sep=',', iterato ...

  2. Python数据类型(列表和元组)

    1.3 List(列表) 列表由一系列按特定顺序排列的元素组成. 在Python中,用方括号[ ]来表示列表,并用逗号来分隔其中的元素. 1.3.1 访问列表元素 在Python中,第一个列表元素的索 ...

  3. A1067. Sort with Swap(0,*)

    Given any permutation of the numbers {0, 1, 2,..., N-1}, it is easy to sort them in increasing order ...

  4. C# 获得本机IP、端口等信息地址以及服务器IP信息

    大概是这样子,代码也没多少,直接上代码了.. 引入命名空间: using System.Net; namespace IPExample { public partial class Form1 : ...

  5. Django 日志配置按日期滚动

    记录下Django关于日期的配置,以及如何根据日期滚动切割日志的问题. 配置的源码在githun上 https://github.com/blackmatrix7/django-examples/tr ...

  6. CodeForces - 893D 贪心

    http://codeforces.com/problemset/problem/893/D 题意 Recenlty Luba有一张信用卡可用,一开始金额为0,每天早上可以去充任意数量的钱.到了晚上, ...

  7. SpringBoot 核心配置

    1. 入口类和 @SpringBootApplication Spring Boot的项目一般都会有*Application的入口类,入口类中会有main方法,这是一个标准的Java应用程序的入口方法 ...

  8. Hadoop生态圈-通过CDH5.15.1部署spark1.6与spark2.3.0的版本兼容运行

    Hadoop生态圈-通过CDH5.15.1部署spark1.6与spark2.3.0的版本兼容运行 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在我的CDH5.15.1集群中,默 ...

  9. JAVA记录-异常基本概念

    1.异常分类 2.错误和异常的区别 3.try-catch-xxx处理方式 Java Exception:1.Error 2.Runtime Exception 运行时异常3.Exception 4. ...

  10. oracle job入门【原】

    oracle job入门 准备工作 先做一张学生表Table 表STUDENT create table STUDENT ( id INTEGER, name ), age INTEGER, crt_ ...