VK Cup 2018 - Round 3 A.Stairs and Elevators

A. Stairs and Elevators
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

In the year of 30XX30XX participants of some world programming championship live in a single large hotel. The hotel has nn floors. Each floor has mm sections with a single corridor connecting all of them. The sections are enumerated from 11 to mm along the corridor, and all sections with equal numbers on different floors are located exactly one above the other. Thus, the hotel can be represented as a rectangle of height nn and width mm. We can denote sections with pairs of integers (i,j)(i,j), where ii is the floor, and jj is the section number on the floor.

The guests can walk along the corridor on each floor, use stairs and elevators. Each stairs or elevator occupies all sections (1,x)(1,x)(2,x)(2,x)(n,x)(n,x) for some xx between 11 and mm. All sections not occupied with stairs or elevators contain guest rooms. It takes one time unit to move between neighboring sections on the same floor or to move one floor up or down using stairs. It takes one time unit to move up to vvfloors in any direction using an elevator. You can assume you don't have to wait for an elevator, and the time needed to enter or exit an elevator is negligible.

You are to process qq queries. Each query is a question "what is the minimum time needed to go from a room in section (x1,y1)(x1,y1) to a room in section (x2,y2)(x2,y2)?"

Input

The first line contains five integers n,m,cl,ce,vn,m,cl,ce,v (2n,m1082≤n,m≤1080cl,ce1050≤cl,ce≤1051cl+cem11≤cl+ce≤m−11vn11≤v≤n−1) — the number of floors and section on each floor, the number of stairs, the number of elevators and the maximum speed of an elevator, respectively.

The second line contains clcl integers l1,,lcll1,…,lcl in increasing order (1lim1≤li≤m), denoting the positions of the stairs. If cl=0cl=0, the second line is empty.

The third line contains cece integers e1,,ecee1,…,ece in increasing order, denoting the elevators positions in the same format. It is guaranteed that all integers lili and eiei are distinct.

The fourth line contains a single integer qq (1q1051≤q≤105) — the number of queries.

The next qq lines describe queries. Each of these lines contains four integers x1,y1,x2,y2x1,y1,x2,y2 (1x1,x2n1≤x1,x2≤n1y1,y2m1≤y1,y2≤m) — the coordinates of starting and finishing sections for the query. It is guaranteed that the starting and finishing sections are distinct. It is also guaranteed that these sections contain guest rooms, i. e. y1y1 and y2y2 are not among lili and eiei.

Output

Print qq integers, one per line — the answers for the queries.

Example
input
Copy
5 6 1 1 3
2
5
3
1 1 5 6
1 3 5 4
3 3 5 3
output
Copy
7
5
4
Note

In the first query the optimal way is to go to the elevator in the 5-th section in four time units, use it to go to the fifth floor in two time units and go to the destination in one more time unit.

In the second query it is still optimal to use the elevator, but in the third query it is better to use the stairs in the section 2.


题意:给你一个二维数组,代表一栋楼,每个楼中有房间、电梯和楼梯。电梯和楼梯都是按列分布(只要有就占一列),在同一层的每个房间相互走动的话走一个单位要花一个单位的时间,如果想要跨楼层走动的话(即上楼和下楼)必须要走电梯或者楼梯(两者的数量都有可能是0,但总和一定大于0),楼梯每走一个单位要花一个单位时间,电梯每走V个单位花一个时间,给你q个问题,每个问题给定起点和终点,问从起点到终点最少要花多少时间。


思路:数据范围都很大,所以不可能用数组保存图,能够保存的只有楼梯和电梯的位置。思路很简单,就是比较走起点左边的第一个电梯、右边的第一个电梯、左边的第一个楼梯、右边第一个楼梯,哪个花费的时间最少。q最大为1e5,所以要找到这四个数据的话只能二分。


总结:这个题思路比较简单,但是写起来有点乱,又因为是在脑子很乱的时候写的,所以出现了很多弱智的小错误,也因此wa了无数发,还是太马虎。唯一的收货就是对二分又加深了些理解,刚开始的时候二分缩区间的时候用的是l=mid+1和r=mid-1,可能是会丢掉某个数据(具体怎么丢的还是不太清楚 qaq),于是后来改成l=mid和r=mid就过了,然后看了大白本,发现大白本上的二分都是这样写的。。。。所以以后还是这种写法优先吧。。。。

AC代码:

#define _CRT_SBCURE_MO_DEPRECATE
#define fi first
#define se second
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using std::pair;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<PII,int> PPI;
typedef pair<ll,int> PLI;
const double PI=acos(-1);
const int N = 100000 + 10;
const int M = 100 + 10;
const int INF = 0x3f3f3f3f;
using namespace std;
int n,m,cs,ce,v;
int a[N],b[N];
int sx,sy,ex,ey;
int fun(int n)
{
    if(n<0)
        return -n;
    else
        return n;
}
int fun2(int a,int b)
{
    if(a%b==0)
    {
        return a/b;

    }
    else
    {
        return a/b+1;
    }
}
int besearch(int *a,int sum)
{
    if(a[0]>sy)
        return -1e8;       //返回的时候取绝对值就会无限大,因为要求最小值,所以不会对结果产生影响
    if(a[sum-1]<=sy)
        return a[sum-1];
    int l=0,r=sum-1;
    while(l<r)
    {
        int mid=(l+r)/2;
        if(a[mid]<sy&&a[mid+1]>sy)
            return a[mid];
        else if(a[mid]>sy)
            r=mid;
        else if(a[mid]<sy)
            l=mid; 
    }
}
int upsearch(int *a,int sum)
{
    if(a[0]>sy)
        return a[0];
    if(a[sum-1]<=sy)
        return INF;
    int l=0,r=sum-1;
    while(l<r)
    {
        int mid=(l+r)/2;
        if(a[mid]<sy&&a[mid+1]>sy)
            return a[mid+1];
        else if(a[mid]>sy)
            r=mid;
        else if(a[mid]<sy)
            l=mid;
    }
}
int main()
{
    scanf("%d%d%d%d%d",&n,&m,&cs,&ce,&v);
    int t;
    int tem;
        for(int i=0;i<cs;i++)
            scanf("%d",&a[i]);
    for(int i=0;i<ce;i++)
        scanf("%d",&b[i]);
    int q;
    scanf("%d",&q);
    while(q--)
    {
        ll mi=INF;
        scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
        if(sx==ex)
        {
            printf("%d\n",fun(sy-ey));  //特判如果在同一行的情况
            continue;
        }
        if(ce==0)
        {

            int bs=besearch(a,cs);  //左楼
            int us=upsearch(a,cs);  //右楼
            mi=min(fun(bs-ey)+fun(bs-sy)+fun(sx-ex),fun(us-ey)+fun(us-sy)+fun(sx-ex));
        }
        else if(cs==0)
        {
            int be=besearch(b,ce);  //左电
            int ue=upsearch(b,ce);   //右电
            mi=min(fun(be-ey)+fun(be-sy)+fun2(fun(sx-ex),v),fun(ue-ey)+fun(ue-sy)+fun2(fun(sx-ex),v));
        }
        else
        {
            int bs=besearch(a,cs);
            int us=upsearch(a,cs);
            int be=besearch(b,ce);
            int ue=upsearch(b,ce);
             mi=min(min(1ll*fun(bs-ey)+fun(bs-sy)+fun(sx-ex),1ll*fun(us-ey)+fun(us-sy)+fun(sx-ex)),min(1ll*fun(be-ey)+fun(be-sy)+fun2(fun(sx-ex),v),1ll*fun(ue-ey)+fun(ue-sy)+fun2(fun(sx-ex),v)));

        }
        printf("%d\n",mi);
    }
}

你可能感兴趣的:(VK Cup 2018 - Round 3 A.Stairs and Elevators)