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. P3图片导致iOS9.3以下崩溃问题

    如果你刚刚升级了Xcode8,而你的项目的Deployment Target是iOS 9.3以下,运行iOS8的时候过了几十秒后crash到main函数,出现EXC_BAD_ACCESS,或者崩溃到i ...

  2. 初学C#,总结一下.sln和.csproj的区别

    1.sln:solusion 解决方案 csproj:c sharp project C#项目 csproj文件大家应该不会陌生,那就是C#项目文件的扩展名,它是“C Sharp Project”的缩 ...

  3. CDR X7 限时3折618年中大促,是时候出手了!

    力量与激情.胜利与喜悦,这些自带饱满情绪的词,即将“刷屏”这个夏天!32支球队,28个不眠夜,你将与谁度过? 一场视觉上的饕餮盛宴即将开始! 小编也是个疯狂足球迷,虽然中国队无缘今年的俄罗斯世界杯,但 ...

  4. linux防火墙查看状态firewall、iptable

    一.iptables防火墙1.基本操作 # 查看防火墙状态 service iptables status # 停止防火墙 service iptables stop # 启动防火墙 service ...

  5. PHP实现的毫秒定时器,同时解决进程不重复堆积

    定时器任务,在WEB应用比较常见,如何使用PHP实现定时器任务,大致有两种方案:1)使用Crontab命令,写一个shell脚本,在脚本中调用PHP文件,然后定期执行该脚本:2)配合使用ignore_ ...

  6. u-boot for tiny210 ver1.0(by liukun321咕唧咕唧)

     新版本下载: 下面的链接提供了较新版本的源码 ver4.0源码下载:u-boot for tiny210 ver4.0 ver3.1源码下载: u-boot for tiny210 ver3.1 v ...

  7. STM32 HAL库 IIC 协议库函数

    /* 第1个参数为I2C操作句柄 第2个参数为从机设备地址 第3个参数为从机寄存器地址 第4个参数为从机寄存器地址长度 第5个参数为发送的数据的起始地址 第6个参数为传输数据的大小 第7个参数为操作超 ...

  8. C#窗体间的跳转传值

    1.开发平台VS2012 2.需求:从一个窗体跳转到另一个窗体,并传递参数,接收返回值. 3.案列如图: 4.代码如下: 登陆窗体: //当点击注册按钮 private void button2_Cl ...

  9. [Python Test] Use pytest fixtures to reduce duplicated code across unit tests

    In this lesson, you will learn how to implement pytest fixtures. Many unit tests have the same resou ...

  10. CC2540/CC2541 : Set the Peripheral Being Advertising while It is Being Connected

    There is possible to set your CC254X be scanable when it is in connection. But, based on my test,the ...