【CF 1020A】New Building for SIS
A. New Building for SIS
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
Copy
3 6 2 3 3
1 2 1 3
1 4 3 4
1 2 2 3
output
Copy
1
4
2
题意:有n栋楼,每栋大楼有h层,每栋楼的a层b层相互连通,每次移动到楼的另一层耗时1分钟,给出两层楼的坐标,求出最短到达时间。
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<stdlib.h>
#include<cmath>
#include<set>
#include<map>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
#define closeio std::ios::sync_with_stdio(false)
int main()
{
int n,h,a,b,k,s1,s2,x1,x2,y1,y2,sum;
cin>>n>>h>>a>>b>>k;
while(k--)
{
cin>>x1>>y1>>x2>>y2;
if(x1==x2)
sum=abs(y1-y2);
else if(y1>a&&y1>b&&y2>a&&y2>b)
sum=abs(x1-x2)+abs(y1-y2)+min(y1-b,y2-b)*2;
else if(y1<a&&y1<b&&y2<a&&y2<b)
sum=abs(x1-x2)+abs(y1-y2)+(max(y1,y2)-a)*(-2);
else
sum=abs(x1-x2)+abs(y1-y2);
cout<<sum<<endl;
}
return 0;
}