Gate

倍增模板,在一个严格小根堆中,给定$x,y$,求$x$的祖先中$≥y$的最高点。

注意清零

#include<cstdio>
#include<iostream>
#include<cstring>
#define MogeKo qwq
using namespace std;
const int maxn = 1e5+;
int t,n,q,val[maxn],cnt;
int dpth[maxn],p[maxn][];
int to[maxn],head[maxn],nxt[maxn]; void add(int x,int y) {
to[++cnt] = y;
nxt[cnt] = head[x];
head[x] = cnt;
} void dfs(int x,int fa) {
dpth[x] = dpth[fa]+;
p[x][] = fa;
for(int i = ; (<<i)<=dpth[x]; i++)
p[x][i] = p[p[x][i-]][i-];
for(int i = head[x]; i; i = nxt[i]) {
if(to[i] == fa)continue;
dfs(to[i],x);
}
} int solve(int x,int y) {
for(int i = ; i >= ; i--) {
if(val[p[x][i]] >= y) x = p[x][i];
if(x== || val[x]==y)break;
}
return x;
} int main() {
scanf("%d",&t);
for(int j = ;j <= t;j++) {
printf("Case %d:\n",j);
memset(to,,sizeof(to));
memset(head,,sizeof(head));
memset(nxt,,sizeof(nxt));
memset(p,,sizeof(p));
cnt = ;
val[] = ;
dpth[] = ;
scanf("%d%d",&n,&q);
int x,y;
for(int i = ; i <= n-; i++) {
scanf("%d%d",&x,&y);
add(x,i);
val[i] = y;
}
dfs(,);
for(int i = ; i <= q; i++) {
scanf("%d%d",&x,&y);
printf("%d\n",solve(x,y));
}
}
return ;
}

Lightoj 1128 - Greatest Parent的更多相关文章

  1. 1128 - Greatest Parent---LightOj(LCA+离线算法)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1128 给你一颗树,树的每个节点都有一个权值,树根是节点0,权值为1,树中每个节点的权值 ...

  2. PHP中this,self,parent三个关键字

    this,self,parent三个关键字从字面上比较好理解,分别是指这.自己.父亲. this是指向当前对象的指针(姑且用C里面的指针来看吧)self是指向当前类的指针parent是指向父类的指针( ...

  3. Android之RecyclerView的原生Bug-Inconsistency detected. Invalid view holder adapter positionViewHolder{a1bbfa3 position=2 id=-1, oldPos=-1, pLpos:-1 no parent}

    今天在运行自己编写的App时,突然发现App在运行时闪退,然后就查看了Android Studio的Log,发现了这个错误,上网查了一下,才知道是RecyclerView的原生Bug,在数据更新时会出 ...

  4. git: fatal: Not a git repository (or any of the parent directories): .git

    在看书 FlaskWeb开发:基于Python的Web应用开发实战 时,下载完源码后 git clone https://github.com/miguelgrinberg/flasky.git 试着 ...

  5. 关于js中window.location.href,location.href,parent.location.href,top.location.href的用法

    "window.location.href"."location.href"是本页面跳转 "parent.location.href"是上一 ...

  6. 关于一个parent(),siblings()的小问题

    今天发现一个小问题,现在也不知道到底是哪个梗在作祟,但是感觉是parent()和siblings()其中的一个. 我是想这样的根据输入的条件删选内容: demo: <!DOCTYPE html& ...

  7. 区间DP LightOJ 1422 Halloween Costumes

    http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...

  8. jQuery通过parent()和parents()方法访问父级元素

    <div class="inputGroup"> <p>2.您的最高学历是?</p> <label><input type=& ...

  9. MySQL主从复制中断,报“Error on master: message (format)='Cannot delete or update a parent row: a foreign key constraint fails' error code=1217” 错误

    前几天,发现从库挂了,具体报错信息如下: 分析思路 1. 因为我采用的是选择性复制,只针对以下几个库进行复制: card,upay,deal,monitor,collect.所以,不太可能出现对于sa ...

随机推荐

  1. springboot情操陶冶-web配置(八)

    本文关注应用的安全方面,涉及校验以及授权方面,以springboot自带的security板块作为讲解的内容 实例 建议用户可直接路由至博主的先前博客spring security整合cas方案.本文 ...

  2. Perl多线程(2):数据共享和线程安全

    线程数据共享 在介绍Perl解释器线程的时候一直强调,Perl解释器线程在被创建出来的时候,将从父线程中拷贝数据到子线程中,使得数据是线程私有的,并且数据是线程隔离的.如果真的想要在线程间共享数据,需 ...

  3. MyBatis动态代理执行原理

    前言 大家使用MyBatis都知道,不管是单独使用还是和Spring集成,我们都是使用接口定义的方式声明数据库的增删改查方法.那么我们只声明一个接口,MyBatis是如何帮我们来实现SQL呢,对吗,我 ...

  4. Linux常用命令详解(week1_day1_2)--技术流ken

    本节内容 aliasunaliasunamesuhostnamehistorywhichwcwwhowhoamipingkillseqdudffreedate 命令详解 1. alias 设置.’查看 ...

  5. [Go] golang的error接口

    error接口1.error就是一个接口interface2.属于errors包,该包有一个导出方法New,返回了errorString类型3.errorString类型实现了error接口4.之所以 ...

  6. Java开发笔记(三十)大小数BigDecimal

    前面介绍的BigInteger只能表达任意整数,但不能表达小数,要想表达任意小数,还需专门的大小数类型BigDecimal.如果说设计BigInteger的目的是替代int和long类型,那么设计Bi ...

  7. Java开发笔记(三十四)字符串的赋值及类型转换

    不管是基本的char字符型,还是包装字符类型Character,它们的每个变量只能存放一个字符,无法满足对一串字符的加工.为了能够直接操作一连串的字符,Java设计了专门的字符串类型String,该类 ...

  8. Java几种常见的设计模式

    --------------------- 本文来自 旭日Follow_24 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/xuri24/article/detail ...

  9. Netty学习笔记(二) 实现服务端和客户端

    在Netty学习笔记(一) 实现DISCARD服务中,我们使用Netty和Python实现了简单的丢弃DISCARD服务,这篇,我们使用Netty实现服务端和客户端交互的需求. 前置工作 开发环境 J ...

  10. 扩展1000!(n!)的尾数零的个数

    #include <stdio.h> #include <malloc.h> //计算1000!尾数零的个数 //扩展n!的尾数零的个数 //2^a * 5^b //obvio ...