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 ta, fa, tb, fb (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
3 6 2 3 3
1 2 1 3
1 4 3 4
1 2 2 3
Output
1
4
2
题意:一个人在楼之间穿梭,不同楼层之间通过的楼梯给出范围,穿梭楼层以及不同楼之间的时间为1min,求从目标地到最后的地方需要最少的时间
题解:该题有个大坑-----此人可能在一栋楼上,那么不需要考虑楼层之间的问题;(万幸吃个零食想了出来orz)
那么情况分为两种:
一种是一栋楼,,直接做差即可;
第二种是不同楼,画图可知----走弯路情况只有两种(一个是目的地和出发的都在规定楼层上面,一个是在规定楼层下面),其余情况做差就可
注意:做差时加绝对值
ac代码
#include<iostream>
#include<cmath>
#include<cstdlib>
using namespace std;
int n,h,low,hig,k;
int main()
{
    long long ans;
    int ta,fa,tb,fb;
    cin>>n>>h>>low>>hig>>k;
    for(int i=1; i<=k; i++)
    {
        cin>>ta>>fa>>tb>>fb;
        if(ta==tb)
            ans=abs(fa-fb);
        else
        {
            ans=abs(tb-ta);
            if(fa<low&&fb<low)
                ans+=(low-fa+low-fb);
            else if(fa>hig&&fb>hig)
                ans+=(fa-hig+fb-hig);
            else
                ans+=abs(fa-fb);
        }
        cout<<ans<<endl;
    }
    return 0;
}

最新文章

  1. MongoDB 搭建副本集
  2. 【JVM】JVM系列之执行引擎(五)
  3. windows 10
  4. CSS规则的执行顺序(转)
  5. HDOJ2024C语言合法标识符
  6. DataGridView几个基本属性
  7. Visual Studio快速封装字段方法
  8. IE 下使用firebug
  9. haproxy 中的http请求和https请求
  10. 使用JS意识到自己主动提交表单
  11. ipython的用法详解
  12. java 线程池 ---- newCachedThreadPool()
  13. 末学者笔记--apache编译安装及LAMP架构上线
  14. warning: ISO C++ forbids converting a string constant to &#39;char*&#39; [-Wwrite-strings]
  15. Spark基本架构
  16. Git多账号配置,同一电脑多个ssh-key的管理
  17. dialog 关闭 清除
  18. ;(function(){ //代码})(); 自执行函数开头为什么要加;或者!
  19. JVM系列3:类加载机制
  20. lombok 工具类的介绍

热门文章

  1. ES6 promise用法总结
  2. linux环境搭建单机kafka
  3. SpringBoot2.x入门:快速创建一个SpringBoot应用
  4. 异常重试框架Spring Retry实践
  5. 真的可以,用C语言实现面向对象编程OOP
  6. (私人收藏)2019科协WER解决方案
  7. [开源]eCharts配置简化包OptionCreator[typescript版]
  8. 使用.Net Core实现的一个图形验证码
  9. 常用API - 包装类、System类
  10. C++栈(stack)、队列(queue)、链表(list)的常用函数