CodeForces - 780B The Meeting Place Cannot Be Changed(二分大法!! 二分答案)

The Meeting Place Cannot Be Changed

During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables.

Now she has a table filled with integers. The table consists of n rows and m columns. By ai, j we will denote the integer located at the i-th row and the j-th column. We say that the table is sorted in non-decreasing order in the column j if ai, j ≤ ai + 1, j for all i from 1 to n - 1.

Teacher gave Alyona k tasks. For each of the tasks two integers l and r are given and Alyona has to answer the following question: if one keeps the rows from l to r inclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column? Formally, does there exist such j that ai, j ≤ ai + 1, j for all i from lto r - 1 inclusive.

Alyona is too small to deal with this task and asks you to help!

Input

The first line of the input contains two positive integers n and m (1 ≤ n·m ≤ 100 000) — the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table.

Each of the following n lines contains m integers. The j-th integers in the i of these lines stands for ai, j (1 ≤ ai, j ≤ 109).

The next line of the input contains an integer k (1 ≤ k ≤ 100 000) — the number of task that teacher gave to Alyona.

The i-th of the next k lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n).

Output

Print "Yes" to the i-th line of the output if the table consisting of rows from li to ri inclusive is sorted in non-decreasing order in at least one column. Otherwise, print "No".

Example

Input

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

Output

Yes
No
Yes
Yes
Yes
No

Note

In the sample, the whole table is not sorted in any column. However, rows 1–3 are sorted in column 1, while rows 4–5 are sorted in column 3.

题意:给你 n 个 x 坐标,代表这个坐标上有一个人,然后每一个人有一个速度 v , 可以向两边跑(x 轴正方向, x 轴负方向), 这 n 个人要集中在一个点,问需要的最小时间。

思路:开始的时候想了想,这个题应该只能枚举,先想了一下看是否能够二分枚举终点,最后发现 二分终点没有单调性。。。  所以此题二分时间, 对于一个时间,判断是否能够成立,成立的话  就代表时间还可以在缩小。。。

而对于一个时间,假设所有人都向右跑,肯定是有一个人在最左端的,这个人也就无力了,只能让其他的人向这个点跑,所以再判断坐标位置大于这个点的人, 向这个点跑是否能够到达,能够达到就说明这个时间是  ok 的。

AC代码:

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

const int maxn = 6e4 + 10;
const double eps = 1e-6;
int x[maxn];
int v[maxn];
int n;

bool check(double t){
    double point = 1000000000.0;
    for(int i = 1;i <= n;i ++)
        point = min(point,x[i] * 1.0 + v[i] * t);
    for(int i = 1;i <= n;i ++){
        if(x[i] * 1.0 > point){
            if(x[i] * 1.0 - v[i] * t > point)
                return 0;
        }
    }
    return 1;
}

int main()
{
    while(~scanf("%d",&n)){
        for(int i = 1;i <= n;i ++)
            scanf("%d",&x[i]);
        for(int i = 1;i <= n;i ++)
            scanf("%d",&v[i]);
        double l = 0 ,r = 1000000000.0;
        while(fabs(r - l) > eps){
            double mid = (l + r) / 2;
            if(check(mid))
                r = mid;
            else l = mid;
        }
        printf("%.10lf\n",l);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/no_O_ac/article/details/82051956
今日推荐