Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3) C. Stairs and Elevators【二分查找】

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 (2≤n,m≤1082≤n,m≤108, 0≤cl,ce≤1050≤cl,ce≤105, 1≤cl+ce≤m−11≤cl+ce≤m−1, 1≤v≤n−11≤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 (1≤li≤m1≤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 (1≤q≤1051≤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 (1≤x1,x2≤n1≤x1,x2≤n, 1≤y1,y2≤m1≤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 32531 1 5 61 3 5 43 3 5 3

output

Copy

754

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个单元,有的单元房有电梯,有的单元房有楼梯,楼梯移动一层需要1个时间单位

电梯一个时间单位移动v层,同一层相邻单元之间移动消耗1个时间单位

现在有q次查询

每次查询给出起点和终点的层数和单元位置

问从起点到终点最少需要几个时间单位

做法:

二分查找起点和终点之间的和偏左的横坐标的左、偏右的横坐标的右侧的电梯和楼梯的位置,取最优答案即可

叉点:

特判同一层,不需要找楼梯和电梯了,直接过去就好

代码:

  1 #include<iostream>
  2 using namespace std;
  3 #include<cstdio>
  4 #include<cstdlib>
  5 #include<cstring>
  6 #include<algorithm>
  7 typedef long long ll;
  8 typedef long long LL;
  9 const ll maxn = 210000;
 10 ll dt[maxn],lt[maxn];
 11 ll n,m,cl,cd,v;
 12 int binsearch_big(LL a[],int n,int s){
 13     int mid;
 14     LL l = 1,r = n;
 15     while(l<=r){
 16         mid = (l+r)/2;
 17         if(a[mid]>=s)
 18             r = mid - 1;
 19         else
 20             l = mid + 1;
 21     }
 22     if(l>=1&&l<=n)
 23         return l;
 24     else
 25         return 0;
 26 }
 27 int binsearch_small(LL a[],int n,int d){
 28     int mid;
 29     LL l = 1,r = n;
 30     while(l<=r){
 31         mid = (l+r)/2;
 32         if(a[mid]<=d)
 33             l = mid + 1;
 34         else
 35             r = mid - 1;
 36     }
 37     if(r>=1&&r<=n)
 38         return r;
 39     else
 40         return 0;
 41 }
 42 int main(){
 43     scanf("%I64d%I64d%I64d%I64d%I64d",&n,&m,&cl,&cd,&v);
 44     for(int i=1;i<=cl;i++){
 45         scanf("%I64d",&lt[i]);
 46     }
 47     for(int i=1;i<=cd;i++){
 48         scanf("%I64d",&dt[i]);
 49     }
 50     sort(lt+1,lt+cl+1);
 51     sort(dt+1,dt+cd+1);
 52     int sq;
 53     scanf("%d",&sq);
 54     for(int i=0;i<sq;i++){
 55         LL x1,x2,y1,y2;
 56         scanf("%I64d%I64d%I64d%I64d",&y1,&x1,&y2,&x2);
 57         if(y1==y2){
 58             LL ans = abs(x1-x2);
 59             printf("%I64d\n",ans);
 60             continue;
 61         }
 62         LL ansl = 0,ansd = 0;
 63         ansl = abs(y2-y1);
 64         ansd = abs(y2-y1)/v+(abs(y2-y1)%v!=0);
 65         LL ll = x1,rr = x2;
 66         if(ll>rr)
 67             swap(ll,rr);
 68         LL l = binsearch_big(lt,cl,ll);
 69         LL r = binsearch_small(lt,cl,rr);
 70         if((l>0&&lt[l]>=ll&&lt[l]<=rr)||(r>0&&lt[r]>=ll&&lt[r]<=rr)){
 71             ansl+=abs(x2-x1);
 72         }
 73         else{
 74             LL wr = binsearch_big(lt,cl,rr);
 75             LL ans1 = 0x3f3f3f3f,ans2 = 0x3f3f3f3f;
 76             if(wr>0)
 77                 ans2 = abs(ll-lt[wr])+abs(rr-lt[wr]);
 78             LL wl = binsearch_small(lt,cl,ll);
 79             if(wl>0)
 80                 ans1 = abs(ll-lt[wl])+abs(rr-lt[wl]);
 81             ansl+=min(ans1,ans2);
 82         }
 83
 84
 85         l = binsearch_big(dt,cd,ll);
 86         r = binsearch_small(dt,cd,rr);
 87         if((l>0&&dt[l]<=rr&&dt[l]>=ll)||(r>0&&dt[r]>=ll&&dt[r]<=rr)){
 88             ansd+=abs(x2-x1);
 89         }
 90         else{
 91             LL wr = binsearch_big(dt,cd,rr);
 92             LL ans1 = 0x3f3f3f3f,ans2 = 0x3f3f3f3f;
 93             if(wr>0)
 94                 ans2 = abs(ll-dt[wr])+abs(rr-dt[wr]);
 95             LL wl = binsearch_small(dt,cd,ll);
 96             if(wl>0)
 97                 ans1 = abs(ll-dt[wl])+abs(rr-dt[wl]);
 98             ansd+=min(ans1,ans2);
 99         }
100         LL ans = min(ansd,ansl);
101         printf("%I64d\n",ans);
102     }
103     return 0;
104 }

原文地址:https://www.cnblogs.com/xfww/p/8973137.html

时间: 2024-10-10 05:43:18

Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3) C. Stairs and Elevators【二分查找】的相关文章

【枚举】【二分】【推导】Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3) D. Resource Distribution

题意:有两个服务要求被满足,服务S1要求x1数量的资源,S2要求x2数量的资源.有n个服务器来提供资源,第i台能提供a[i]的资源.当你选择一定数量的服务器来为某个服务提供资源后,资源需求会等量地分担给它们,要求每台服务器承担的资源需求不超过其所能提供的资源需求.给定一种合法的方案,每台服务器要么没有被分配给任何一个服务,或者被分配给其中一个服务. 对服务器按能提供的资源从小到大排序.枚举给S1分配的服务器数量i,然后在a数组中二分,就可以得到给S1提供的是哪i台服务器,它们占据了a数组中连续的

Codeforces Round #470 (rated, Div. 2, based on VK Cup 2018 Round 1)C. Producing Snow+差分标记

题目链接:C. Producing Snow 题意:给两个数组v[N],T[N],v[i]表示第i天造的雪,T[i],表示第i天的温度,一堆雪如果<=T[i],当天就会融完,否则融化T[i],要求输出每天的融雪总量. 题解:我对T数组求个前缀和,就可以二分找到每堆雪在那一天(pos)融化,余下的要加进答案中ans[i],然后用一个an数组在a[i]+1,a[pos]-1,最后求再求一次前缀和. ans[i]再加上an[i]*t[i].每次操作二分logn,N次操作.复杂度O(nlogn) #in

Codeforces Round #472 (rated, Div. 2, based on VK Cup 2018 Round 2)

A. Tritonic Iridescence 题解:分类讨论.注意题目要求,至少有两种方案. 1 #pragma warning(disable:4996) 2 #include<cstdio> 3 #include<string> 4 #include<cstring> 5 #include<iostream> 6 #include<algorithm> 7 using namespace std; 8 9 int n, m; 10 stri

【树形dp】Codeforces Round #405 (rated, Div. 1, based on VK Cup 2017 Round 1) B. Bear and Tree Jumps

我们要统计的答案是sigma([L/K]),L为路径的长度,中括号表示上取整. [L/K]化简一下就是(L+f(L,K))/K,f(L,K)表示长度为L的路径要想达到K的整数倍,还要加上多少. 于是,我们现在只需要统计sigma((L+f(L,K))),最后除以K即可. 统计sigma(L)时,我们考虑计算每条边出现在了几条路径中,设u为edgei的子节点,那么这条边对答案的贡献就是siz(u)*(n-siz(u)),siz(u)为u的子树大小. 统计sigma(f(L,K))时,我们需要dp出

Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) B

Description Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends.

Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) C

Description In the army, it isn't easy to form a group of soldiers that will be effective on the battlefield. The communication is crucial and thus no two soldiers should share a name (what would happen if they got an order that Bob is a scouter, if

Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) A

Description Bear Limak wants to become the largest of bears, or at least to become larger than his brother Bob. Right now, Limak and Bob weigh a and b respectively. It's guaranteed that Limak's weight is smaller than or equal to his brother's weight.

Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) B. T-Shirt Hunt

B. T-Shirt Hunt time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Not so long ago the Codecraft-17 contest was held on Codeforces. The top 25 participants, and additionally random 25 participant

Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) E. Prairie Partition 二分+贪心

E. Prairie Partition It can be shown that any positive integer x can be uniquely represented as x = 1 + 2 + 4 + ... + 2k - 1 + r, where k and r are integers, k ≥ 0, 0 < r ≤ 2k. Let's call that representation prairie partition of x. For example, the p