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

本文解析了一道来自Codeforces的编程题,题目要求计算在特定建筑物布局中两点间的最短移动时间。文章提供了详细的思路分析及C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接: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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谷丘CODER

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值