CodeForces - 1020A. New Building for SIS-模拟

题目链接:http://codeforces.com/problemset/problem/1020/A

input

Copy

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

给你n个相邻的建筑(从左到右编号1到n),每个建筑有h层,每个建筑的a层到b层中任意一层c层可以花一秒通过连廊直接到隔壁建筑的c层。上下楼1层需要1秒。求q次查询(建筑a,层a)到(建筑b,层b)的最短时间。

3个建筑,6层高,2到3有楼梯,3次查询

分三种情况,细分的话就是四种,一是在同一楼移动,只需要算楼层差就行,二是起点和终点都在天桥上,所以要先王下走到达天桥,然后往上走,三是起点终点都天桥下,需要先往上走,到达天桥,穿过天桥,再往下,第四种就是斜走法,左上到右下,或者右下到左上,或者水平通过天桥。直接算坐标差

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<set>
#include<vector>
#include<sstream>
#include<queue>
#define PI 3.1415926535897932384626
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=1005;
int a[4][4];
int main()
{
    int n,h,a,b,k;
    scanf("%d%d%d%d%d",&n,&h,&a,&b,&k);
    while(k--)
    {
        int ans;
        int ta,fa,tb,fb;
        scanf("%d%d%d%d",&ta,&fa,&tb,&fb);
        if(ta==tb)
            ans=abs(fb-fa);
        else if(fb>=b&&fa>=b)
            ans=abs(fb-b)+abs(fa-b)+abs(tb-ta);
        else if(fb<=a&&fa<=a)
            ans=abs(a-fa)+abs(a-fb)+abs(tb-ta);
        else
            ans=abs(tb-ta)+abs(fb-fa);
        cout<<ans<<endl;

    }
    return 0;
}

A. New Building for SIS

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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

猜你喜欢

转载自blog.csdn.net/qq_41568836/article/details/81706880
今日推荐