You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.

The building consists of n towers, h floors each, where the towers are labeled from 1 to n, the floors are labeled from 1 to h. There is a passage between any two adjacent towers (two towers i and i + 1 for all i: 1 ≤ i ≤ n - 1) on every floor x, where a ≤ x ≤ b. It takes exactly one minute to walk between any two adjacent floors of a tower, as well as between any two adjacent towers, provided that there is a passage on that floor. It is not permitted to leave the building.

The picture illustrates the first example.

You have given k pairs of locations (ta, fa), (tb, fb): floor fa of tower ta and floor fb of tower tb. For each pair you need to determine the minimum walking time between these locations.

Input

The first line of the input contains following integers:

  • n: the number of towers in the building (1 ≤ n ≤ 108),
  • h: the number of floors in each tower (1 ≤ h ≤ 108),
  • a and b: the lowest and highest floor where it's possible to move between adjacent towers (1 ≤ a ≤ b ≤ h),
  • k: total number of queries (1 ≤ k ≤ 104).

Next k lines contain description of the queries. Each description consists of four integers tafatbfb (1 ≤ ta, tb ≤ n, 1 ≤ fa, fb ≤ h). This corresponds to a query to find the minimum travel time between fa-th floor of the ta-th tower and fb-th floor of the tb-th tower.

Output

For each query print a single integer: the minimum walking time between the locations in minutes.

Example
input

Copy
3 6 2 3 3
1 2 1 3
1 4 3 4
1 2 2 3
output

Copy
1
4
2
一道特别简单的题
题意:从一个塔的某一层到另一个塔的某一层,需要的最短时间。条件:每上下一层都要一秒,每从一个塔去临近的塔需要一秒,每个塔去临近的塔只有a-b层有通道去。 
思路:分类讨论一下。 
1、当出发地和目的地是同一个塔:abs(fa-fb) 
2.1、当出发地和目的地不同塔:但是有一个在有通道范围内,或者两者都不在范围内但一个<=a一个>=b:abs(ta-tb)+abs(fa-fb) 
2.2、当出发和目的地楼层都<=a:abs(ta-tb)+abs(fa-a)+abs(fb-a) 
2.3、当出发地和目的地楼层都>=b:abs(ta-tb)+abs(fa-b)+abs(fb-b)
此人的思维:https://blog.csdn.net/miranda_ymz/article/details/81603271
但是我不喜欢此人的代码。
if(ta==tb)
cout<<abs(fa-fb)<<endl;

这是第一部分“当出发地和目的地是同一个塔”

else
{
if(fa>=b&&fb>=b)
cout<<abs(ta-tb)+(fb-b)+(fa-b)<<endl;
else if(fa<=a&&fb<=a)
cout<<abs(ta-tb)+(a-fa)+(a-fb)<<endl;
else
cout<<abs(ta-tb)+abs(fa-fb)<<endl;
}

这是第二部分,你只需要控制先判断同在b之上和同在a之下,剩余的就是一个大于b或者小于a,另一个在a和b之间。(不用那么多的判断)

上代码

 #include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n,h,a,b,k;
cin>>n>>h>>a>>b>>k;
while(k--)
{
int ta,tb,fa,fb;
cin>>ta>>fa>>tb>>fb;
if(ta==tb)
cout<<abs(fa-fb)<<endl;
else
{
if(fa>=b&&fb>=b)
cout<<abs(ta-tb)+(fb-b)+(fa-b)<<endl;
else if(fa<=a&&fb<=a)
cout<<abs(ta-tb)+(a-fa)+(a-fb)<<endl;
else
cout<<abs(ta-tb)+abs(fa-fb)<<endl;
}
}
return ;
}

A. New Building for SIS Codeforce的更多相关文章

  1. A - New Building for SIS

    You are looking at the floor plan of the Summer Informatics School's new building. You were tasked w ...

  2. 【CF1020A】New Building for SIS(签到)

    题意: 有n栋楼,从一栋楼某个地方,到大另一栋楼的某个地方,每栋楼给了连接楼的天桥,每走一层或者穿个一栋楼花费一分钟,求出起点到大目的点最少花费的时间 n,h<=1e8,q<=1e4 思路 ...

  3. Codeforces Round #503 (by SIS, Div. 2) Solution

    从这里开始 题目列表 瞎扯 Problem A New Building for SIS Problem B Badge Problem C Elections Problem D The hat P ...

  4. CF-503div2-A/B/C

    A. New Building for SIS time limit per test 1 second memory limit per test 256 megabytes input stand ...

  5. Codeforces Round #503 Div. 2

    时间相对来说还是比较合适的,正好放假就可以打一打啦. A. New Building for SIS:http://codeforces.com/contest/1020/problem/A 题意概述 ...

  6. Codeforce Round #574(Div.2)

                                                                                                        ...

  7. Building the Testing Pipeline

    This essay is a part of my knowledge sharing session slides which are shared for development and qua ...

  8. BZOJ 4742: [Usaco2016 Dec]Team Building

    4742: [Usaco2016 Dec]Team Building Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 21  Solved: 16[Su ...

  9. Building OpenCASCADE on Debian

    Building OpenCASCADE on Debian eryar@163.com Abstract. When you are familiar with OpenCASCADE on Win ...

随机推荐

  1. 关联规则之FpGrowth算法

    Aprori算法利用频繁集的两个特性,过滤了很多无关的集合,效率提高不少,但是我们发现Apriori算法是一个候选消除算法,每一次消除都需要扫描一次所有数据记录,造成整个算法在面临大数据集时显得无能为 ...

  2. 依赖注入&控制反转

    IoC——Inversion of Control  控制反转DI——Dependency Injection   依赖注入 要想理解上面两个概念,就必须搞清楚如下的问题: 参与者都有谁? 依赖:谁依 ...

  3. git的命令操作指南

    Git图形化界面我用的还可以,但是命令就不太会了,索性和大家一起学习下Git命令的用法...一般来说,日常使用只要记住下图6个命令,就可以了.但是熟练使用,恐怕要记住60-100个命令. fetch ...

  4. Exchange Onine功能介绍

    Exchange Online是Office 365中提供的一个邮箱服务.Microsoft Exchange Online是将Microsoft Exchange Server功能作为基于云的服务提 ...

  5. mysql 多字段更新

    更新一个字段当好写 ; Query OK, rows affected (17.36 sec) Rows matched: Changed: Warnings: 问题是想更新多个字段sql serve ...

  6. Robustness|Variability|Diversification|Complexity|自组装|

    生命组学 进化方向有以下四个特性:Robustness:变稳定,比如杀虫剂最大浓度也有杀不死的虫子.Variability易变性与Diversification多样性,容易变多和变多.Complexi ...

  7. flink分层 api

    最底层的processFunction 功能强大,使用复杂 中间层的DataSet api map reduce ...一些基本运算api 中上层的tableAPI 最上层 SQL 两个相似,只是写法 ...

  8. java 面向对象 购物车

    一个商城包括多个商品.多个用户.拥有销售商品.展示商品和查找商品功能.2)    一个用户拥有一个购物车,购物车具有结算功能.3)    商城具有名称,静态字符串类型4)    用户类是抽象类,两个子 ...

  9. python3之urllib代理池

    1.常见状态吗 301:重定向到新的URL,永久性302:重定向到临时URL,非永久性304:请求的资源未更新400:非法请求401:请求未经授权403:禁止访问404:没找到对应页面500:服务器内 ...

  10. 吴裕雄--天生自然python学习笔记:Python3 模块

    Python3 模块 在前面的几个章节中我们脚本上是用 python 解释器来编程,如果你从 Python 解释器退出再进入,那么你定义的所有的方法和变量就都消失了. 为此 Python 提供了一个办 ...