codeforce 777 C. Alyona and Spreadsheet (打表预处理)

来源:互联网 发布:java管理系统有哪些 编辑:程序博客网 时间:2024/06/07 22:32

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 l to 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*m的矩阵,有q次查询,每次查询给出 ,v;
对于每次查询,询问在矩阵的第u行到第v行中,是否存在一个非递减的列。


思路:
打表预处理,有效缩减时间复杂度。
对于每一行,用b[i]存放矩阵中第i行维持非递减规律最多到第几行。


#include<cstdio>#include<cstring>#include<algorithm>#include<vector> using namespace std;const int MAX = 1e5+10;int b[MAX];vector<int >a[MAX]; //这里需要用到vector ,否则会因为数组大小过大或者过小而CE或者REint main(){    int n,m;    while(scanf("%d%d",&n,&m)!=EOF)    {        int temp;        for(int i=1;i<=n;i++)        {             a[i].push_back(0);            for(int j=1;j<=m;j++)            {                scanf("%d",&temp);                a[i].push_back(temp);            }        }//将矩阵存入数组        for(int j=1;j<=m;j++)        {            for(int i=1;i<n;)            {                if(a[i+1][j]>=a[i][j])//需要对最后一行特判                 {                    int flag=i;                    while(flag<n&&a[flag+1][j]>=a[flag][j])                        flag++;                    for(int k=i;k<flag;k++)                        a[k][j]=flag;                    i=flag;                }                else                {                    a[i][j]=i;                    i++;                }            }        }        b[n]=n;//对最后一行的特判         for(int i=1; i<n; i++)        {            int Max=a[i][1];            for(int j=2; j<=m; j++)                Max=max(Max,a[i][j]);            b[i]=Max;        }        int q;        scanf("%d",&q);        while(q--)        {            int u,v;            scanf("%d%d",&u,&v);            if(b[u]>=v)                printf("Yes\n");            else printf("No\n");        }    }    return 0;}
原创粉丝点击