【lca+输入】Attack on Alpha-Zet
Attack on Alpha-Zet
题目描述
Space pirate Captain Krys has recently acquired a map of the artificial and highly secure planet Alpha-Zet which he has been planning to raid for ages. It turns out the whole planet is built on a 2D plane with modules that serve as one room each. There is exactly one module at every pair of integer coordinates and modules are exactly 1 × 1 units big. Every module is bidirectionally connected to at least one adjacent module. Also, for any two modules there exists exactly one path between them. All in all the modules create a rectangular maze without any loops.
Figure A.1: Illustration of Sample Input 2
On the map Captain Krys has marked several modules he wants to visit in exactly the marked order. What he intends to do there is none of your business, but he promises you a fortune if you determine the number of modules he has to walk through along
the route (since there are no loops he will always take the direct route from one marked module to the next). The first marked module indicates where he starts his journey, the last where he wants to finish.
输入
The input consists of:
•one line with two integers h and w (2 ≤ h, w ≤ 1 000) describing the height and the width of the maze.
•h + 1 lines follow, describing the maze in ASCII, each line containing 2 · w + 1 characters.
The description always follows these rules:
–In every row, columns with odd index (starting at index 1) contain either vertical walls or spaces and columns with even index contain either horizontal walls or spaces.
–The first row describes the northern wall of the maze (which always consists only of horizontal walls). Every subsequent row describes a row of modules.
–A module is located at every even column index. Its western and eastern walls are located at the directly neighboring odd column indices respectively, its northern wall is located at the same column index but one row above and its southern wall can be found at its own position. If a wall is missing, the corresponding position contains a space instead.
•After the description of the maze, an integer m (2 ≤ m ≤ 104) is given.
•Each of the following m lines describes a marked module with two integer coordinates x and y (1 ≤ x ≤ h; 1 ≤ y ≤ w). The first pair of coordinates is the start point of the journey, the last pair the end point. Modules may appear multiple times but never twice or more in a row. (1, 1) is the top left module and (h, w) is the bottom right module.
It is guaranteed that the maze itself is enclosed. Furthermore it is guaranteed that exactly one path exists between any two modules.
输出
Output one integer, the number of modules Captain Krys has to travel through if he follows the route in the exact order given in the input.
样例输入
2 6
_ _ _ _ _ _
| _ _ _ _ _|
|_ _ _ _ _ _|
5
1 5
1 1
1 6
1 1
1 5
样例输出
18
树上距离,lca裸题
有空格的输入
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1005;
const int maxm=2005;
char mp[maxn][maxm];
int n,m;
struct Edge
{
int v,next;
}e[maxn*maxm*2];
int head[maxn*maxn],tol;
int dep[maxn*maxn];
int f[maxn*maxn][22];
void add(int u,int v){tol++;e[tol].v=v;e[tol].next=head[u];head[u]=tol;}
void input()
{
getchar();
for(int i=0;i<=n;i++){
gets(mp[i]);
}
}
int num(int i , int j)
{
i = i-1;
j = (j+1)/2;
return i*m+j;
}
void build()
{
for(int i=1; i<=n; i++)
{
for(int j=1; j<=2*m; j+=2)
{
if(mp[i][j+1] != '|')
{
add(num(i,j) , num(i,j+2));
add(num(i,j+2) , num(i,j));
}
if(mp[i][j] != '_')
{
add(num(i,j) , num(i+1,j));
add(num(i+1,j) , num(i,j));
}
}
}
}
void dfs(ll u,ll fa){//dfs建树
dep[u]=dep[fa]+1;
f[u][0]=fa;//初始化每个点的父节点
for(int i=head[u];i;i=e[i].next){
int v=e[i].v;
if(v!=fa){
dfs(v,u);
}
}
}
void rmq_init(int k)
{
for(int j=1;j<=19;j++)
for(int i=1;i<=k;i++)
if(f[i][j-1]) f[i][j] = f[f[i][j-1]][j-1];
}
int lca(int u,int v)
{
if(dep[u]<dep[v]) swap(u,v);//深度深的先处理
for(int i=19;i>=0;i--){
if(dep[u]>=dep[v]+(1<<i)){
u = f[u][i];
}
}
if(u==v){//跳到同一深度判断是否完成
return u;
}
for(int i=19;i>=0;i--){//一起跳
if(f[u][i]!=f[v][i]){
u=f[u][i];
v=f[v][i];
}
}
return f[u][0];
}
void solve()
{
ll res=0;int l,r;
int now,last,tmp;
int q;scanf("%d",&q);
for(int i=1;i<=q;i++){
scanf("%d%d",&l,&r);
now=num(l,r*2-1);
if(i!=1){
tmp=lca(last,now);
res+=(dep[now]-dep[tmp]+dep[last]-dep[tmp]);
}
last=now;
}
printf("%lld",res);
}
int main()
{
scanf("%d%d",&n,&m);
input();
build();
dfs(1,0);
rmq_init(n*m);
solve();
return 0;
}
【lca+输入】Attack on Alpha-Zet的更多相关文章
- LCA入门题集小结
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题目: How far away ? Time Limit: 2000/1000 MS (Jav ...
- LaLeX数学公式
启用数学公式: 需要插入公式时,用 $ 将公式包围.若需要输入多行,则用一对 $$ 包围. 例如: $$ \rho = \sqrt{(\Delta x)^{2}+(\Delta y)^{2}} \\ ...
- Python+OpenCV图像处理(十二)—— 图像梯度
简介:图像梯度可以把图像看成二维离散函数,图像梯度其实就是这个二维离散函数的求导. Sobel算子是普通一阶差分,是基于寻找梯度强度.拉普拉斯算子(二阶差分)是基于过零点检测.通过计算梯度,设置阀值, ...
- UE4 材质Lerp节点解疑
转自:http://www.manew.com/thread-46268-1-1.html 1.A是一个灰色,B是一个红色,Alpha是一个颜色图 A到B是0到1,也就是黑到白,所以,alpha图,黑 ...
- RETE算法介绍
RETE算法介绍一. rete概述Rete算法是一种前向规则快速匹配算法,其匹配速度与规则数目无关.Rete是拉丁文,对应英文是net,也就是网络.Rete算法通过形成一个rete网络进行模式匹配,利 ...
- OpenCV——边缘检测(sobel算子、Laplacian算子、scharr滤波器)
#include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...
- Canvas 3D球形文字云动画特效
Canvas 3D球形文字云动画特效 效果图: 代码如下,复制即可使用: (适用浏览器:360.FireFox.Chrome.Opera.傲游.搜狗.世界之窗. 不支持Safari.IE8及以下浏览器 ...
- 【python-opencv】18-图像梯度+图像边界
效果图: *一阶导数与Soble算子 *二阶导数与拉普拉斯算子 定义:把图片想象成连续函数,因为边缘部分的像素值是与旁边像素明显有区别的,所以对图片局部求极值,就可以得到整幅图片的边缘信息了. 不过图 ...
- Android图形动画
一.动画基础 本质 每帧绘制不同的内容. 基本过程 开始动画后,调用View的invalidate触发重绘.重绘后检查动画是否停止,若未停止则继续调用invalidate触发下一帧(下一次重绘),直到 ...
随机推荐
- swift之水纹动画
import UIKit class CVLayerView: UIView { var pulseLayer : CAShapeLayer! //定义图层 override init(frame: ...
- 201909-1 小明种苹果 Java
思路: 保存掉落的苹果总数,和树是第几棵即可 import java.io.BufferedReader; import java.io.IOException; import java.io.Inp ...
- git修改已经push的commit message
git中修改上一次提交的commit的message git commit --amend -m "你的新的注释" git push -f 多个commit https://www ...
- Android进阶——多线程系列之Thread、Runnable、Callable、Future、FutureTask
多线程一直是初学者最抵触的东西,如果你想进阶的话,那必须闯过这道难关,特别是多线程中Thread.Runnable.Callable.Future.FutureTask这几个类往往是初学者容易搞混的. ...
- web系统能力培养计划
服务器知识掌握如下 01购买linux服务器 客户端工具:https://mobaxterm.mobatek.net/download.html 02linux常用命令 https://www.run ...
- PAT A1015-1016
A 1015 Reversible Primes 看清题意即可.给的数是十进制的,需要先判断是不是素数,然后按照给定进制转化成字符串后进行翻转,最后再转化为十进制并判断是否为素数. #include ...
- UVA - 1631 Locker(密码锁)(dp---记忆化搜索)
题意:有一个n(n<=1000)位密码锁,每位都是0~9,可以循环旋转.每次可以让1~3个相邻数字同时往上或者往下转一格.输入初始状态和终止状态(长度不超过1000),问最少要转几次. 分析: ...
- Mybatis框架的简单配置
Mybatis 的配置 1.创建项目(当然,这是废话) 2.导包 下载mybatis-3.2.0版:https://repo1.maven.org/maven2/org/mybatis/mybatis ...
- Scheduled定时任务器在Springboot中的使用
Scheduled定时任务器是Spring3.0以后自带的一个定时任务器. 使用方式: 1.添加依赖 <!-- 添加 Scheduled 坐标 --> <dependency> ...
- 在Azure Storage 托管HTTP静态网站
本文演示了在Azure Storage托管HTTP静态网站. 注意:HTTP已经不建议使用. 创建Azure StorageV2 存储账户 账户类型选择“StorageV2(通用版V2)”: 本例中, ...