题解:主席树&DFS序。

PS:为什么我一开始Wa了N发 是因为有一个左区间我写成[L,M+1]了。。。。。。。。。。。。。。。。。。。。。。。。。。

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#define PAU putchar(' ')
#define ENT putchar('\n')
using namespace std;
const int maxn=+,maxnode=+,maxn2=+;
int ls[maxnode],rs[maxnode],s[maxnode],A[maxn],root[maxn],p[maxn],si[maxn],so[maxn],siz[maxn],cz=,tot=;
struct tedge{int x,y,next;}adj[maxn2];int ms=,fch[maxn];
void addedge(int u,int v){
adj[++ms]=(tedge){u,v,fch[u]};fch[u]=ms;
adj[++ms]=(tedge){v,u,fch[v]};fch[v]=ms;
return;
}
void build(int x,int&y,int L,int R,int pos){
s[y=++tot]=s[x]+;if(L==R) return;
int M=L+R>>; if(pos<=M) rs[y]=rs[x],build(ls[x],ls[y],L,M,pos);
else ls[y]=ls[x],build(rs[x],rs[y],M+,R,pos);
}
int query(int x,int y,int L,int R,int k){
if(L==R) return L;int M=L+R>>,kth=s[ls[y]]-s[ls[x]];
if(k<=kth) return query(ls[x],ls[y],L,M,k);
else return query(rs[x],rs[y],M+,R,k-kth);
}
void dfs(int u,int fa){
si[u]=++cz;p[cz]=u;siz[u]=;
for(int i=fch[u];i;i=adj[i].next){
int v=adj[i].y;
if(v!=fa) dfs(v,u),siz[u]+=siz[v];
} so[u]=cz;return;
}
inline int read(){
int x=,sig=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')sig=-;ch=getchar();}
while(isdigit(ch))x=*x+ch-'',ch=getchar();
return x*=sig;
}
inline void write(int x){
if(x==){putchar('');return;}if(x<)putchar('-'),x=-x;
int len=,buf[];while(x)buf[len++]=x%,x/=;
for(int i=len-;i>=;i--)putchar(buf[i]+'');return;
}
int n,Q;
void init(){
n=read();Q=read();
for(int i=;i<n;i++) addedge(read(),read());
for(int i=;i<=n;i++) A[i]=read();
dfs(,);
for(int i=;i<=n;i++) build(root[i-],root[i],,n,A[p[i]]);
return;
}
void work(){
int x,k;
while(Q--){
x=read();k=read();
if(siz[x]<k) puts("-1");
else write(query(root[si[x]-],root[so[x]],,n,k)),ENT;
}
return;
}
void print(){
return;
}
int main(){init();work();print();return ;}

搜索

复制

COJ 1011 WZJ的数据结构(十一)树上k大的更多相关文章

  1. COJ 0979 WZJ的数据结构(负二十一)

    WZJ的数据结构(负二十一) 难度级别:C: 运行时间限制:5000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 请你实现一个数据结构,完成这样的功能: 给你一个 ...

  2. COJ 1008 WZJ的数据结构(八) 树上操作

    传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=986 WZJ的数据结构(八) 难度级别:E: 运行时间限制:3000ms: ...

  3. COJ 1007 WZJ的数据结构(七) 树上操作

    传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=983 WZJ的数据结构(七) 难度级别:C: 运行时间限制:1000ms: ...

  4. COJ 0970 WZJ的数据结构(负三十)树分治

    WZJ的数据结构(负三十) 难度级别:D: 运行时间限制:1000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 给你一棵N个点的无根树,点和边上均有权值.请你设计 ...

  5. COJ 1003 WZJ的数据结构(三)ST表

    WZJ的数据结构(三) 难度级别:B: 运行时间限制:3000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 请你设计一个数据结构,完成以下功能: 给定一个大小为N的 ...

  6. COJ 0981 WZJ的数据结构(负十九)树综合

    WZJ的数据结构(负十九) 难度级别:E: 运行时间限制:3500ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 WZJ的数据结构中有很多都是关于树的.这让很多练习 ...

  7. COJ 0967 WZJ的数据结构(负三十三)

    WZJ的数据结构(负三十三) 难度级别:E: 运行时间限制:7000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 请你设计一个数据结构,完成以下功能: 给定一个大 ...

  8. COJ 0990 WZJ的数据结构(负十)

    WZJ的数据结构(负十) 难度级别:D: 运行时间限制:5000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 给你一个N个节点的有根树,从1到N编号,根节点为1并给 ...

  9. COJ 0995 WZJ的数据结构(负五)区间操作

    WZJ的数据结构(负五) 难度级别:C: 运行时间限制:1000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 请你设计一个数据结构,完成以下功能: 给定一个大小为 ...

随机推荐

  1. [Angular 2] The form export from NgFormControl

    In last post, we need to create an instanse variable: sku: AbstructControl; We can get rid of this b ...

  2. Android三种实现自定义ProgressBar的方式介绍

    一.通过动画实现 定义res/anim/loading.xml如下: View Row Code<?xml version="1.0" encoding="UTF- ...

  3. buffer cache chain 图

    http://orabase.org/index.php/author/djeday84/page/7/

  4. oracle 自治事物 -- autonomous transaction

    一 使用规则 : 在begin 之前申明  : PRAGMA AUTONOMOUS_TRANSACTION; 二 使用理解:autonomous transaction 是一个独立的事务,这一点是理解 ...

  5. 【VB】StrConv函数 vbUnicode用法

    [VB]StrConv函数 StrConv(string, conversion, LCID) vbUnicode 64 根据系统的缺省码页将字符串转成Unicode. vbFromUnicode 1 ...

  6. B/S系统间跨域单点登录设计思路

    基于B/S系统间单点登录 此处说的单点登录的概念,即不同系统公用一个登录界面.一处系统通过登录验证,在接入的各系统均为登录状态.一般有两种情景: 1)  一级域名相同 例如:tieba.baidu.c ...

  7. (转)dedecms [field:array runphp='yes']标签使用技巧

    field支持用array获取任意字段的值:(支持标记:文章内容模板的 {dede:field name=’array’ /}.arclist.arclistsg.loop.sql 标签) 我们平时常 ...

  8. SVN设置钩子文件限制提交文件时必须填写更新日志

    进入相应SVN仓库hooks目录,编辑文件pre-commit #!/bin/sh # PRE-COMMIT HOOK## The pre-commit hook is invoked before ...

  9. Oracle处理特殊字符

    检查数据库级的参数设置 select * from nls_database_parameters;

  10. 使用下拉列表框<select>标签,节省空间

    下拉列表在网页中也常会用到,它可以有效的节省网页空间.既可以单选.又可以多选.如下代码: 讲解: 1.value: 2.selected="selected": 设置selecte ...