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. iOS开发-测量APP启动耗时

    冷启动 冷启动就是App被kill掉以后一切从头开始启动的过程. 热启动 当用户按下home键的时候,iOS的App并不会马上被kill掉,还会继续存活若干时间.理想情况下,用户点击App的图标再次回 ...

  2. nsrunloop与模式

    scrollview的模式切换:退出原来的模式使用新模式. 2018-04-18 18:16:41.208113+0800 CEMonitor[5014:410604] kCFRunLoopDefau ...

  3. 使用CablleStatement调用存储过程

    /** * 使用CablleStatement调用存储过程 * @author APPle * */ public class Demo1 { /** * 调用带有输入参数的存储过程 * CALL p ...

  4. Eclipse中使用GIT提交文件至本地

    GIT提交文件至本地: 1.  右击项目——Team——Commit…: 2.在弹出的Commit Changes框中——选择要提交的文件——填写提交说明——点击Commit,即可提交至本地.

  5. echart的tooltip自定义换行

    自定义换行,内容很长的时候 tooltip : { trigger: 'axis', axisPointer : { // 坐标轴指示器,坐标轴触发有效 type : 'shadow' // 默认为直 ...

  6. JS 日历

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...

  7. 字体样式(.ttf/.woff)文件的配置引入

    在引入前端框架部分功能时,有时需要配置字体样式.可以这样配置:在 .ttf的同级目录下,创建icon.css文件,写入: @font-face {font-family: "element- ...

  8. js 事件监听,执行某操作

    <script language=javascript> var ie; var firefox; if (document.all) ie = true; else ie = false ...

  9. S5PV210 三个Camera Interface/CAMIF/FIMC的区别

    S5PV210有三个CAMIF单元,分别为CAMIF0 CAMIF1和CAMIF2.对应着驱动中的fimc0, fimc1, fimc2.在三星datasheet和驱动代码中CAMIF和FIMC(Fu ...

  10. HDU 4302 Contest 1

    维护两个优先队列即可.要注意,当出现蛋糕的位置刚好在狗的位置时,存在右边. 注意输出大小写... #include <iostream> #include <queue> #i ...