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 ;
}

最新文章

  1. 数据结构算法C语言实现(八)--- 3.2栈的应用举例:迷宫求解与表达式求值
  2. js动画 无缝轮播 进度条 文字页面展示 div弹窗遮罩效果
  3. SpringMVC+spring-security+sitemesh+hibernate+freemarker整合-转
  4. [SQL]sql语句中charindex的用法
  5. HDU 2845 Beans (DP)
  6. PHP搭建OAuth2.0
  7. google 论文
  8. USB枚举详细过程剖析(转)
  9. sublime test3 使用技巧
  10. ES6是什么
  11. vue 初始化data中的数据
  12. Spark在Windows下的环境搭建(转)
  13. Atcoder Regular-074 Writeup
  14. Tesseract-OCR 3.05 多过语言文字识别(运行程序+中英日韩语言包)
  15. [HDU4123]Bob’s Race
  16. Qt 自学QGraphicsItem要点 积累
  17. Python 使用 Postfix 发送邮件
  18. [转]The NTLM Authentication Protocol and Security Support Provider
  19. ZT 设计模式六大原则(5):迪米特法则
  20. Oracle数据库的归档模式(archivelog mode)

热门文章

  1. iOS 中UIWebView的cookie
  2. python后端面试第一部分:python基础--长期维护
  3. 13.1 CentOS系统启动流程介绍
  4. linux centos的安装及一些相关知识的整理
  5. 分布式事物-2pc和3pc区别
  6. 关于JavaScript中的typeof与instanceof
  7. 吴裕雄--天生自然 R语言开发学习:基本图形(续三)
  8. Analysis of Hello2 source code
  9. spring-boot-学习笔记(三)-过滤器
  10. 使用 javascript 配置 nginx