【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触发下一帧(下一次重绘),直到 ...
随机推荐
- DataRow转实体
调用 DataRow row = new DataRow(); ConvertToEntity<实体类>(row) private T Conv ...
- 转: 十大Intellij IDEA快捷键
Intellij IDEA中有很多快捷键让人爱不释手,stackoverflow上也有一些有趣的讨论.每个人都有自己的最爱,想排出个理想的榜单还真是困难.以前也整理过Intellij的快捷键,这次就按 ...
- 组件state
一.设计合适的state 1.1 定义: state代表一个组件UI呈现的完整状态 stae代表一个组件UI呈现的最小状态集[所有状态都用于组件UI的变化,没有任何多余的状态] 1.2 state和p ...
- Q3:Longest Substring Without Repeating Characters
3. Longest Substring Without Repeating Characters 官方的链接:3. Longest Substring Without Repeating Chara ...
- 【JavaScript】回流(reflow)与重绘(repaint)
重绘与回流 首先要了解页面是如何呈现的: HTML文档加载后生成DOM树(包括display:none;元素): 在DOM树的基础上配合css样式结构体生成render树(不包含display:non ...
- arp攻击 (可查看同一局域网他人手机照片)
国家法律一定要遵守,知识要用在对的地方. 本贴只为了和大家交流学习,请勿用在其他地方,损害任何人的利益. 今天我,来说一下arp攻击的原理和教程 原理什么的还是自行百度好,因为专业的说明是严谨而又经得 ...
- 小程序使用wxs 解决wxml保留2位小数问题
1.出现溢出表现 从图中可以看到数字超出了很长长度.代码里面是如下这样的.为什么在0.35出现?或者一些相成的计算出现? 而 0.34却不会. 0.41 也会出现,好像是二进制运算出现结果. data ...
- 微信小程序自定义分享封面
onShareAppMessage:function(options){ let thas = this; if (options.from === 'button') { // 来自页面内转发按钮 ...
- 【每日Scrum】第四天冲刺
一.计划会议内容 连接数据库报错,解决问题中. 二.任务看板 三.scrum讨论照片 四.产品的状态 无 五.任务燃尽图
- pycharm2018专业版
https://blog.csdn.net/moshanghuali/article/details/94396935 1 下载Pycharm专业版2018.3.1许多人直接在官网直接下载,默认的都是 ...