题目大意

  给一棵有点权的n个点的有根树,保证任意两点的点权不同,m次询问每次询问x的子树中权值第k大的点。

输入

  先输入n,然后每个点点权,再输入n-1行每行两个数x,y代表x和y相连,再输入m,之后m次询问,每行两个数x,k。

  主席树,随便找一个点为根,再dfs出树的dfs序,按dfs序建每一时刻主席树,利用主席树查询x子树区间第k大。

#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
inline char _read()
{
static char buf[100000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int read()
{
int x=0,f=1;char ch=_read();
while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=_read();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=_read();}
return x*f;
}
int n,m;
int x,y;
int tot;
int num;
int cnt;
int q[100010];
int s[100010];
int t[100010];
int v[100010];
int to[200010];
int ls[4000010];
int rs[4000010];
int next[200010];
int head[100010];
int root[100010];
int sum[4000010];
map<int,int>b;
void add(int x,int y)
{
tot++;
next[tot]=head[x];
head[x]=tot;
to[tot]=y;
}
void dfs(int x,int fa)
{
s[x]=++num;
q[num]=x;
for(int i=head[x];i;i=next[i])
{
if(to[i]!=fa)
{
dfs(to[i],x);
}
}
t[x]=num;
}
int updata(int pre,int l,int r,int v)
{
int rt=++cnt;
if(l==r)
{
sum[rt]=sum[pre]+1;
return rt;
}
ls[rt]=ls[pre];
rs[rt]=rs[pre];
sum[rt]=sum[pre]+1;
int mid=(l+r)>>1;
if(v<=mid)
{
ls[rt]=updata(ls[pre],l,mid,v);
}
else
{
rs[rt]=updata(rs[pre],mid+1,r,v);
}
return rt;
}
int query(int x,int y,int l,int r,int k)
{
if(l==r)
{
return b[l];
}
int val=sum[ls[y]]-sum[ls[x]];
int mid=(l+r)>>1;
if(val<k)
{
return query(rs[x],rs[y],mid+1,r,k-val);
}
else
{
return query(ls[x],ls[y],l,mid,k);
}
}
int main()
{
n=read();
for(int i=1;i<=n;i++)
{
v[i]=read();
b[v[i]]=i;
}
for(int i=1;i<n;i++)
{
x=read();
y=read();
add(x,y);
add(y,x);
}
dfs(1,0);
for(int i=1;i<=num;i++)
{
root[i]=updata(root[i-1],0,1e9,v[q[i]]);
}
m=read();
for(int i=1;i<=m;i++)
{
x=read();
y=read();
printf("%d\n",query(root[s[x]-1],root[t[x]],0,1e9,y));
}
}

BZOJ1803Spoj1487 Query on a tree III——主席树的更多相关文章

  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(主席树)

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

  4. SP1487 PT07J - Query on a tree III 主席树+dfs序

    Code: #include<iostream> #include<cstdio> #include<algorithm> #include<string&g ...

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

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

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

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

  8. 「SPOJ1487」Query on a tree III

    「SPOJ1487」Query on a tree III 传送门 把树的 \(\text{dfs}\) 序抠出来,子树的节点的编号位于一段连续区间,然后直接上建主席树区间第 \(k\) 大即可. 参 ...

  9. 【BZOJ2588】Count On a Tree(主席树)

    [BZOJ2588]Count On a Tree(主席树) 题面 题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第 ...

随机推荐

  1. Luogu4040 AHOI/JSOI2014 宅男计划 贪心、二分、三分

    传送门 仍然对"为什么这个函数单峰"的问题毫无理解 首先,对于保质期又低.价格又贵的食物,我们显然不需要购买它.所以如果设\(pri_i\)表示保质期不小于\(i\)的所有食品中价 ...

  2. 【SQL】MaxComputer中调试与问题排查技巧小结

    1.分段调试 面对长的SQL,出错时一般直接看定位的行号,有时候不出错但是没数据时,应该尝试分段调试,很长的SQL嵌套很多的子查询时,一个一个子查询进行分别调试,看哪一步子查询出了问题,层层推进 2. ...

  3. Ionic App中嵌入外部网页的问题

    在app中不可避免的要引用第三方的页面,那么在Ionic中是如何实现呢? 1.设计引用外部页面的html框架页面,分3部分,表头有2个按钮,中间是引用的页面,底部隐藏分享相关按钮,具体页面如下: &l ...

  4. maven 通过 pom.xml 指定java编译版本

    <!-- 给maven项目指定编译版本 --> <plugin> <groupId>org.apache.maven.plugins</groupId> ...

  5. Zookeeper Windows版的服务安装和管理工具

    以前研究过负载均衡,最近正在项目上实施(从来没做过小项目以上级别的东西,哈).然后遇到了多个一模一样但是同时运行的服务.不同服务但依赖同相同的配置数据(前端网页服务:Nginx+IIS+nodejs. ...

  6. item 7:当创建对象的时候,区分()和{}的使用

    本文翻译自modern effective C++,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 从不同的角度来看,在C++11中,对象初始化拥有多种语法选择,这体 ...

  7. C_数据结构_递归实现累加

    # include <stdio.h> long sum(int n) { //用递归实现: ) ; else ) + n; /* 用for循环实现: long s = 0; int i; ...

  8. Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)-D- Array Restoration

    我们知道不满足的肯定是两边大中间小的,这样就用RMQ查询两个相同等值的区间内部最小值即可,注意边界条件 #include<bits/stdc++.h> #define x first #d ...

  9. 修改sga_max_size大小后重启数据库报 ORA-00851

    http://blog.itpub.net/30150152/viewspace-1449898/

  10. beta阶段测试基本概况对应机型硬件信息

    机型测试概况 测试结果 测试终端数 品牌分布分析 系统分布分析 分辨率分布 未执行 1 联想 4.0.3 480*800 安装失败 1 联想 4.2.1 480*854 通过 119 华为, 三星, ...