SP1487 PT07J - Query on a tree III 主席树+dfs序
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序的更多相关文章
- SP1487 PT07J - Query on a tree III (主席树)
SP1487 PT07J - Query on a tree III 题意翻译 你被给定一棵带点权的n个点的有根数,点从1到n编号. 定义查询 query(x,k): 寻找以x为根的k大点的编号(从小 ...
- 【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 ...
- 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 ...
- BZOJ1803Spoj1487 Query on a tree III——主席树
题目大意 给一棵有点权的n个点的有根树,保证任意两点的点权不同,m次询问每次询问x的子树中权值第k大的点. 输入 先输入n,然后每个点点权,再输入n-1行每行两个数x,y代表x和y相连,再输入m,之后 ...
- bzoj 1803: Spoj1487 Query on a tree III(主席树)
题意 你被给定一棵带点权的n个点的有根数,点从1到n编号. 定义查询 query(x,k): 寻找以x为根的k大点的编号(从小到大排序第k个点) 假设没有两个相同的点权. 输入格式: 第一行为整数n, ...
- 51 nod 1681 公共祖先 (主席树+dfs序)
1681 公共祖先 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 有一个庞大的家族,共n人.已知这n个人的祖辈关系正好形成树形结构(即父亲向儿子连边). 在另 ...
- [SPOJ-PT07J] Query on tree III (主席树)
题意翻译 你被给定一棵带点权的n个点的有根数,点从1到n编号. 定义查询 query(x,k): 寻找以x为根的k大点的编号(从小到大排序第k个点) 假设没有两个相同的点权. 输入格式: 第一行为整数 ...
- [ SPOJ PT07J ] Query on a tree III
\(\\\) Description 其实这题才是正版的 Qtree3...... 给定 \(n\) 个点,以 \(1\) 号节点为根的树,点有点权. \(m\) 次询问 以 \(x\) 为根的子树内 ...
随机推荐
- JDBC的总结
JDBC归纳: DriverManger:驱动管理器类 要操作数据库,必须先与数据库创建连接,得到连接对象 public static Connection getConnection(String ...
- scp 命令简明介绍
安全复制(英语:Secure copy,缩写SCP)是指在本地主机与远程主机或者两台远程主机之间基于Secure Shell(SSH)协议安全地传输电脑文件."SCP"通常指安全复 ...
- 杭电 4508 湫湫系列故事——减肥记I【完全背包】
解题思路:因为食物是可以随便吃的,所以是完全背包,卡路里代表消耗,幸福感代表价值,套公式就可以做了. Problem Description 对于吃货来说,过年最幸福的事就是吃了,没有之一! 但是对于 ...
- 聊聊 TCP 中的 KeepAlive 机制
KeepAlive并不是TCP协议规范的一部分,但在几乎所有的TCP/IP协议栈(不管是Linux还是Windows)中,都实现了KeepAlive功能 RFC1122#TCP Keep-Alives ...
- Unity脚本生命周期 图解
简单总结的话就是: Awake():初始化时执行,类似c#中的构造函数 OnEnable() Start() FixUpdate() Update() OnDisable() OnDestory() ...
- 8、Situation-Dependent Combination of Long-Term and Session-Based Preferences in Group Recommendations: An Experimental Analysis ----组推荐中基于长期和会话偏好的情景依赖组合
一.摘要: 背景:会话组推荐系统的一个主要挑战是如何适当地利用群组成员之间的交互引起用户偏好,这可能会偏离用户的长期偏好.长期偏好和群组诱导的偏好之间的相对重要性应该根据具体的群组设置而变化. 本文: ...
- vue项目初始化步骤
项目初始化:() 1. 安装vue-cli : npm install -g vue-cli 2.初始化项目: vue init webpack my-project 3.进入项目: c ...
- python3 继承与组合
什么叫继承? 所谓继承,就是class_A里面的功能从class_B中直接获取,从而节约了代码且使用方便. 什么叫组合? 除了继承,还有一种我们可以实现目的的方式,那就是组合,同样可以节约代码.只不过 ...
- css不定高度实现垂直居中
1.不知道自己高度和父容器高度的情况下, 利用绝对定位只需要以下三行: parentElement{ position:relative; } childElement{ position: abso ...
- ASP.NET-JSON.NET技巧
第一个技巧,字符串转JSON 单条的json数据可以使用JObject.Parse将对象转化成JObject对象,你可以接着使用JsonConvert.SerializeObject方法把这个对象序列 ...