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\) 为根的子树内 ...
随机推荐
- DB2导出表结构、表数据小结
一.DB2命令行导出数据库全库表结构 ① Win+R进入到DB2安装目录的BIN目录下,执行命令:DB2CMD,进入到DB2 CLP窗口. 命令:DB2CMD ② 创建一个data文件夹 命令:MKD ...
- RAP开发入门-运行第一个HelloWorld(二)
环境搭建好了之后我们就可以照惯例运行第一个helloworld程序了. (ps:这里钉几个资料吧 官网开发指导:http://help.eclipse.org/indigo/index.jsp?top ...
- Linux中删除特殊符号文件名文件
Linux 系统下的文件名长度最多可到256个字符.通常情况下,文件名的字符包括:字母.数字.“.”(点).“_”(下划线)和“-”(连字符). Linux 允许在文件名中使用除上述符号之外的其它符号 ...
- node——underscore的使用
我在做新闻页面时,需要将之前存好点的data.json里的数据显示在首页上,而首页的每条新闻数据不能直接写定在上面,所以我们要将data里面的数据传递进去.我们需要使用underscore的templ ...
- 小程序全局状态管理,在页面中获取globalData和使用globalSetData
GitHub: https://github.com/WozHuang/mp-extend 主要目标 微信小程序官方没有提供类似vuex.redux全局状态管理的解决方案,但是在一个完整的项目中各组件 ...
- Windows Vista 安装和使用指导 - 停止支持后的几条建议
简介 曾经被广大网民吐槽的Windows Vista现在已经淡出了人们的视线,但仍有一些朋友想要体验一下这个操作系统.Windows Vista是Windows发展路线上的里程碑,相比之前的Windo ...
- pytorch 5 classification 分类
import torch from torch.autograd import Variable import torch.nn.functional as F import matplotlib.p ...
- [luogu] P3089 [USACO13NOV]POGO的牛Pogo-Cow
P3089 [USACO13NOV]POGO的牛Pogo-Cow 题目描述 In an ill-conceived attempt to enhance the mobility of his pri ...
- [terry笔记]Oracle SQL 优化之sql tuning advisor (STA)
前言:经常可以碰到优化sql的需求,开发人员直接扔过来一个SQL让DBA优化,然后怎么办? 当然,经验丰富的DBA可以从各种方向下手,有时通过建立正确索引即可获得很好的优化效果,但是那些复杂SQL错综 ...
- 【转载】黑客内核:编写属于你的第一个Linux内核模块
黑客内核:编写属于你的第一个Linux内核模块