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. Python的range(n)的用法

    Python的range(n) 方法就是: API定义: If you do need to iterate(迭代) over a sequence(一系列) of numbers, the buil ...

  2. python数据类型:字符串

    字符串是python中最常见的数据类型,使用单引号或双引号创建字符串 python不支持单字符类型,单字符在python中也是字符串 文档原文:http://www.runoob.com/python ...

  3. 12款优秀的 JavaScript 日历和时间选择控件

    这些插件能够帮助  Web 开发人员更快速的实现各种精美的日历和时间选择效果. 1. The Coolest Calendar 界面非常漂亮的一款日期选择插件,有详细的使用文档,最新版本 1.5. 点 ...

  4. HTML的img标签:alt属性和title属性

    当浏览器卖主扭曲了标准并且自顾自的不按规则去做一些事,他们可能会造成一些问题,或者至少产生了混淆.例子之一就是一些浏览器处理alt属性(一般会被错误的称作alt标签)的方式,比如拥有大量用户的Wind ...

  5. 【Linux_Shell 脚本编程学习笔记三、分支与循环结构】

    if 语句是实际生产工作中最重要且最常用的语句,所以,必须掌握牢固 if 条件语法 1. 单分支机构 if  [ 条件 ] then 指令 fi 或 if  [  条件 ]; then 指令 fi   ...

  6. C# 内置的类型转换方法

    C# 提供了下列内置的类型转换方法: 序号 方法 & 描述 1 ToBoolean把类型转换为布尔型. 2 ToByte把类型转换为字节类型. 3 ToChar如果可能的话,把类型转换为单个 ...

  7. 接口测试-chap4-关联接口测试

    关联接口测试指:请求第2个接口之前必须先请求第一个接口 1.请求第2个接口时需要带着第一个请求返回的cookie 2.如果不使用cookie,如何进行关联接口测试:使用session保持连接.可以代替 ...

  8. iPhoneSE2要在印度独家生产真得没戏?

    现在,关于iPhone SE2的消息层出不穷,总的来说,它是一款真实存在的手机,整体性能和iPhone5X/SE相似,大概可能差不多会加上一些"无线充电"之类的无聊功能.普通消费者 ...

  9. cas单点登录打包时下载慢!

    环境:win10 百度网盘下载 版本:  cas-overlay-template-5.3  提取码    d1b6 添加阿里的到pom.xml    注意添加到第一列 <!--阿里云仓库--& ...

  10. 洛谷P4180【Beijing2010组队】次小生成树Tree

    题目描述: 小C最近学了很多最小生成树的算法,Prim算法.Kurskal算法.消圈算法等等.正当小C洋洋得意之时,小P又来泼小C冷水了.小P说,让小C求出一个无向图的次小生成树,而且这个次小生成树还 ...