C. Stairs and Elevators 二分查找

C. 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 vv floors 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.

题意:n*m的矩形  共n层每层 m个房间    有的房间有电梯 有的有楼梯  走楼梯一层1个单位时间 坐电梯 一个单位时间可走V层(可以半路停下来,按单位时间算,1.1单位时间算2)     q次查询  每次给出一组 坐标   求这两组坐标所需的最短时间        给出楼梯 和电梯

每个楼梯和电梯都站一个 竖直方向 如  电梯为2  则每层的2都有电梯

只要读懂题意就好写了     移动时间的大小差异只和上下楼有关   所以要么走 电梯 要么走楼梯 (或者都行)时间最小  不可能混合走 因为 电梯速度为1/v 楼梯为1 两者必定有明确大小关系

二分找离随便一个点最近的电梯Lower_bound 返回第一个大于等于的编号  所以还要算下 走 编号减一的楼梯或者电梯 情况会不会更快   

其中   

(abs(x1-x2)+v-1)/v   

向上取整 例如   v=3 x1-x2=1  则需要向上取  消耗一个单位时间


#include<bits/stdc++.h>
using namespace std;

int n,m,v,c1,c2,q,a[120000],b[120000];

int main(){

	while(cin>>n>>m>>c1>>c2>>v){
            for(int i=0;i<c1;i++){
                cin>>a[i];
            }
            for(int i=0;i<c2;i++){
                cin>>b[i];
            }
            int    q;
            cin>>q;
            while(q--){
                int x1,y1,x2,y2;
                cin>>x1>>y1>>x2>>y2;
                if(x1==x2){
                    printf("%d\n",abs(y2-y1));
                    continue;
                }
                int temp=lower_bound(a,a+c1,y1)-a;
                int ans=INT_MAX;
                if(temp<c1){
                    ans=min(ans,abs(a[temp]-y1)+abs(a[temp]-y2)+abs(x1-x2));
                }
                if(temp>0){
                    ans=min(ans,abs(a[temp-1]-y1)+abs(a[temp-1]-y2)+abs(x1-x2));
                }
                temp=lower_bound(b,b+c2,y1)-b;
                if(temp<c2){
                    ans=min(ans,abs(b[temp]-y1)+abs(b[temp]-y2)+(abs(x1-x2)+v-1)/v);
                }
                if(temp>0){
                    ans=min(ans,abs(b[temp-1]-y1)+abs(b[temp-1]-y2)+(abs(x1-x2)+v-1)/v);
                }
                printf("%d\n",ans);

            }

	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/bug___maker/article/details/80151119

相关文章