CodeForces - 777C Alyona and Spreadsheet (暴力 ,预处理)

 Alyona and Spreadsheet

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".

扫描二维码关注公众号,回复: 2944898 查看本文章

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.

题意:给你一个二维数组,然后有 q 个询问,分别为 l 到 r, 表示 如果从第 l 行到第 r 行,有任何一列是 非递减的序列,就输出 Yes, 反之就输出 No     .....

思路:首先得明白这一点,对于一个序列 如    4 5 6 7 8 9 1 2 3 ,  很显然从 第一个数开始能够连续非递减到达的最远的数为 9,那么 第二个数 也就是 5,能够连续非递减到达的最远的数也是 9 , 同理  6 7 8 9都是,  而对于 9 后面的数,就需要在重新计算一次。。。    所以这里有两个一维数组, 分别为 H 和 L ,   h 表示第 i 行能够到达的最大行数, L就表示目前当前这一列最大能够到达的行数、、、

AC代码:

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

const int maxn = 1e5 + 10;
int H[maxn],L[maxn];

int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m)){
        int a[n + 5][m + 5] = {0};
        memset(H,0,sizeof(H));
        memset(L,0,sizeof(L));
        for(int i = 1;i <= n;i ++)
            for(int j = 1;j <= m;j ++)
                scanf("%d",&a[i][j]);
        for(int i = 1;i <= n;i ++){
            int maxx = -1;
            for(int j = 1;j <= m;j ++){
                if(L[j] < i){  ///这里也就是前面提到的那一点,如果 j 这一列的值 小于 i 也就是说,从 i 这里出现了 非递减的断层,就需要重新计算一下
                    for(int k = i;k <= n;k ++){     /// 如果这个 if 不成立,也就是说从 i 这一行开始能够到达的最大的行 和i - 1 开始能够达到的最大的行 一样
                        if(a[k+1][j] < a[k][j]){
                            L[j] = k;
                            break;
                        }
                    }
                }
                maxx = max(maxx,L[j]);
            }
            H[i] = maxx;
        }
        int q; scanf("%d",&q);
        while(q --){
            int l,r; scanf("%d%d",&l,&r);
            if(H[l] >= r) puts("Yes");
            else puts("No");
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/no_O_ac/article/details/82055165