Code:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#define REP(i,a,n)for(int i=a;i<=n;++i)
#define CLR(d,a)memset(d,a,sizeof(d)); using namespace std; void SetIO(string a){
string in=a+".in";
freopen(in.c_str(),"r",stdin);
} const int maxn=100000+5; int val[maxn], Sorted[maxn], n,edges; int idx[maxn]; void Discrete(){
REP(i,1,n) Sorted[i]=val[i];
sort(Sorted+1,Sorted+1+n);
REP(i,1,n){
val[i]=lower_bound(Sorted+1,Sorted+1+n,val[i])-Sorted;
idx[val[i]]=i;
}
} int head[maxn<<1],to[maxn<<1],nex[maxn<<1]; void add_edge(int u,int v){
nex[++edges]=head[u];
head[u]=edges;
to[edges]=v;
} void Read(){
scanf("%d",&n);
REP(i,1,n) scanf("%d",&val[i]);
REP(i,1,n-1){
int a,b;
scanf("%d%d",&a,&b);
add_edge(a,b);
add_edge(b,a);
}
} int arr[maxn], siz[maxn],nodes,position[maxn]; int dfs(int u,int fa){
siz[u]=1;
arr[++nodes]=u;
position[u]=nodes; for(int v=head[u];v;v=nex[v]){
if(to[v]==fa)continue;
siz[u]+=dfs(to[v],u);
}
return siz[u];
} const int const_Tree=70; int numv[maxn*const_Tree],root[maxn]; struct Chair_Tree{
int cnt_Tree,lson[maxn*const_Tree],rson[maxn*const_Tree]; void build(int l,int r,int &o){
if(l>r)return;
o=++cnt_Tree;
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_Tree;
numv[oo]=numv[o]+1;
lson[oo]=lson[o];
rson[oo]=rson[o];
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 pre,int cur,int k){
if(l==r)return l;
int lsum=numv[lson[cur]]-numv[lson[pre]];
int mid=(l+r)>>1; if(k<=lsum)
return query(l,mid,lson[pre],lson[cur],k);
else
return query(mid+1,r,rson[pre],rson[cur],k-lsum);
}
}Tree; void Build(){
Discrete();
dfs(1,0);
Tree.build(1,n,root[0]); REP(i,1,n){
int u=arr[i];
root[i]=Tree.insert(1,n,root[i-1],val[u]);
}
} void Work(){
int m;
scanf("%d",&m);
REP(i,1,m){
int a,k;
scanf("%d%d",&a,&k);
int root1=root[position[a]-1];
int root2=root[position[a]+siz[a]-1]; int ans=Tree.query(1,n,root1,root2,k);
ans=idx[ans];
printf("%d\n",ans);
}
} int main(){
SetIO("input");
Read();
Build();
Work();
return 0;
}

  

SP1487 PT07J - Query on a tree III 主席树+dfs序的更多相关文章

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

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

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

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

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

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

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

  6. bzoj 1803: Spoj1487 Query on a tree III(主席树)

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

  7. 51 nod 1681 公共祖先 (主席树+dfs序)

    1681 公共祖先 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题   有一个庞大的家族,共n人.已知这n个人的祖辈关系正好形成树形结构(即父亲向儿子连边). 在另 ...

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

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

  9. [ SPOJ PT07J ] Query on a tree III

    \(\\\) Description 其实这题才是正版的 Qtree3...... 给定 \(n\) 个点,以 \(1\) 号节点为根的树,点有点权. \(m\) 次询问 以 \(x\) 为根的子树内 ...

随机推荐

  1. Java之Foreach语句

    Foreach是for语句的特殊简化版本,但任何时候的foreach语句都可以改写成for语句.Foreach语句在遍历数组等方面为程序员提供了很大的方便 语法如下: For(元素变量 x:遍历对象o ...

  2. TESTUSERB 仅能对TESTUSERA 用户下的某些表增删改查、有些表仅能对某些列update,查询TESTUSERB 用户权限,获取批量赋予语句。

    TESTUSERB 仅能对TESTUSERA 用户下的某些表增删改查.有些表仅能对某些列update,查询TESTUSERB 用户权限,获取批量赋予语句. select 'grant '|| PRIV ...

  3. Reflection (computer programming) -反射-自身结构信息

    n computer science, reflection is the ability of a computer program to examine, introspect, and modi ...

  4. ZBrush如何把不同材质赋予同一个模型上

    ZBrush 作为最专业的数字雕刻与绘画软件,能够制作出高质量的3D模型,包括模型的颜色贴图和材质属性.不同材质可以改变照明在表面上的反应,以便模型表现出光泽.凹凸.反射.金属性或透明效果.ZBrus ...

  5. 实验楼—Mysql—查找最爱学的课程

    转载:https://www.shiyanlou.com/challenges/2651 背景 从上节题目构建的课程数据库中提取每个用户最爱学的课程数据. 右边桌面是实验楼的服务器,服务器中的 MyS ...

  6. IOS - [UIDevice currentDevice] name/model/localizedMode/systemName/systemVersion...../userInterfaceIdiom

    + (UIDevice *)currentDevice; @property(nonatomic,readonly,retain) NSString    *name;              // ...

  7. [CodeForces]500B New Year Permutation

    刷水题做几道入门贪心题预热... 找联通块里字典序最小的放到最前面即可.记得写传递闭包 #include <iostream> #include <cstdio> #inclu ...

  8. Android开发进度05

    1,今日:目标:完成后台用户的增删改查 2,昨天:完成登录和注册功能 3,收获:熟练了SQLite操作 4,问题:无

  9. JAVA深克隆与浅克隆1

    复制就是得到一个副本 克隆就是复制一个对象的复本.但一个对象中可能有基本数据类型,如:int,long,float    等,也同时含有非基本数据类型如(数组,集合等)被克隆得到的对象基本类型的值修改 ...

  10. php获取当前url地址的方法小结

    js 获取: top.location.href //顶级窗口的地址 this.location.href //当前窗口的地址 php获取当前url地址: #测试网址: http://localhos ...