题干:

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

3 6 2 3 3
1 2 1 3
1 4 3 4
1 2 2 3

Output

1
4
2

题目大意:

第一行n <= 10^8 代表楼房个数
第二行h <= 10^8 代表楼房高度
然后 a,b 两个整数代表第a楼到第b楼有空中电梯
有电梯的楼层,相邻两栋楼移动一步可以到达,每个楼的相邻两个楼层移动一步可以到达
然后 一个整数k <= 10^4
然后k行每行有ta fa  tb fb
分别是 a b两个地方的楼号 楼层号
输出最少移动步数

解题报告:

直接模拟就行了,注意特殊样例,比如在同一个楼的不同层里,比如在不同楼的同一层里。

AC代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n,h,a,b,k;
int main()
{cin>>n>>h>>a>>b>>k;int t1,f1,t2,f2;int cur;ll ans = 0;while(k--) {ans = 0;scanf("%d%d%d%d",&t1,&f1,&t2,&f2);if(t1 == t2) {printf("%d\n",abs(f2-f1));continue;}if(t1 > t2) {swap(t1,t2);}if(f1>f2) swap(f1,f2);if(f1 <= a)  ans += (a-f1),cur = a;else if(f1 >= b) ans += (f1 - b), cur = b;else cur = f1;ans += (t2-t1);ans += abs(cur - f2);printf("%lld\n",ans);}return 0 ;
}

【CodeForces - 1020A】New Building for SIS(模拟)相关推荐

  1. Codeforces Round #249 (Div. 2) (模拟)

    Codeforces Round #249 (Div. 2) (模拟) C. Cardiogram time limit per test 1 second memory limit per test ...

  2. Codeforces Round #304 C(Div. 2)(模拟)

    题目链接: http://codeforces.com/problemset/problem/546/C 题意: 总共有n张牌,1手中有k1张分别为:x1, x2, x3, ..xk1,2手中有k2张 ...

  3. CodeForces - 1430E String Reversal(线段树+模拟)

    题目链接:点击查看 题目大意:给出一个字符串 sss ,令其反转的串为 ttt ,每次操作可以将 ttt 中的两个相邻位置的字符交换,问最少需要进行多少次操作才能使得 ttt 变成 sss 题目分析: ...

  4. CodeForces - 1362E Johnny and Grandmaster(贪心+模拟)

    题目链接:点击查看 题目大意:给出一个基数 p ,再给出 n 个指数 k ,换句话说,现在有一个长度为 n 的序列,每个元素都是 p^k[ i ] ,现在需要将这个序列分到两个集合中,使得两个集合元素 ...

  5. CodeForces - 1301D Time to Run(构造+模拟)

    题目链接:点击查看 题目大意:给出一个n*m的矩阵,现在每两个格子之间都有一条双向的通道,初始时有个人在左上角的格子中,现在要求这个人精确的走 k 条路,不过每条路只能走一次,但是每个格子可以走无限次 ...

  6. CodeForces - 1303D Fill The Bag(贪心+模拟)

    题目链接:点击查看 题目大意:给出一个背包,容量为 k ,再给出 n 个物品,每个物品的大小保证是 2 的幂次,现在可以进行操作,使得一个物品分为大小相等的,且大小等于原物品一半的两个物品,比如一个物 ...

  7. CF--思维练习--CodeForces - 216C - Hiring Staff (思维+模拟)

    ACM思维题训练集合 A new Berland businessman Vitaly is going to open a household appliances' store. All he's ...

  8. 【 CodeForces - 799A 】Carrot Cakes(模拟,细节,有坑)

    题干: In some game by Playrix it takes t minutes for an oven to bake k carrot cakes, all cakes are rea ...

  9. codeforces 解题报告 978A. Remove Duplicates 模拟

    http://codeforces.com/contest/978/problem/A 解题思路: 1.删掉重复的元素,只留下序列中不重复的最右边的元素 2.逆向搜一遍,把第一次遇到的元素入栈 3.依 ...

最新文章

  1. OpenKruise v0.8.0 版本发布:K8s 社区首个规模化镜像预热能力
  2. 深入浅出JVM-内存模型
  3. IPv6时代已来:双十一中的IPv6大规模应用实践
  4. 【吴恩达机器学习】学习笔记——1.5无监督学习
  5. k8s核心技术-Controller(DaemonSet)_部署守护进程---K8S_Google工作笔记0034
  6. SCPPO(二十六):测算过程中问题的解决总结
  7. Pair Programming (结对编程)
  8. window10运行不了1stopt_1stopt win10版下载
  9. Qt编写的SMTP客户端(库)
  10. SpringBoot启动成功后,访问接口报错404(error:“Not Found“)
  11. 鸿蒙1030鸿蒙,鸿蒙系统申请
  12. fatfs文件系统详解之f_mount函数分析
  13. 【流媒体服务器Mediasoup】多人音视频架构、流媒体的比较、mediasoup介绍 (一)
  14. 内存空间 逻辑地址空间 相对地址 绝对地址
  15. 35岁的程序员该何去何从?拒绝给自己设限!!
  16. VM虚拟机中的web服务内网穿透的设置,虚拟机连接主机的mysql(主机win10 虚拟机win10)
  17. 移动机器人差速轮运动学模型--(左右轮速度和线速度角速度的相互转换)
  18. Surface不占市场主导但仍关键
  19. flac是什么格式,flac转mp3怎么高效实现
  20. 项目管理 王如龙老师 经典语录

热门文章

  1. 【数据结构与算法】二分查找
  2. 学PHP的嫌弃什么歌,抖音再见了互相嫌弃的老同学是什么歌
  3. java 域的隐藏_Windows Server 2008R2\2012\2016使用域策略自定义隐藏指定驱动器
  4. 函数调用关系图如何画_程序是如何在 CPU 中运行的(二)
  5. matlab里数据类型转换,Matlab数据类型及转换(Matlab data type and conversion).doc
  6. Oracle杀事务数据库崩溃,关于pl/sql dev窗口崩溃导致锁表
  7. 零基础不建议学前端_web前端培训心得:零基础怎样学好web前端
  8. NSIS 查找文件是否存在,并设置安装路径
  9. visual studio 的各个版本下载地址
  10. Asterisk入门系列