codeforces 777C 想法题

来源:互联网 发布:淘宝店铺导航条全屏 编辑:程序博客网 时间:2024/05/11 01:48

Alyona and Spreadsheet

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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的矩阵,k个询问,每个询问有l,r,让你求在l-r行里面有没有一列可以满足从上到下为非递减数列。

解题思路:刚开始想到直接从下到上更新就好了,但是发现一个问题,n*m<=100000,那么n,m都有可能为100000,所以不能开二维数组更新,所以想到从上到下更新,然后用一个一维数组更新每行的数,用另一个一维数组更新这一行的每一列能到达的最上面的非递增数列,再用最后一个数组更新这一行能到达的最上面的非递增数列即可,然后在询问的时候就能用O(k)的时间算出答案了。

#include<cstdio>#define maxn 100005int a[maxn],b[maxn],c[maxn];int x,n,m,k,l,r;int main(){    scanf("%d%d",&n,&m);    for(int i=1;i<=n;i++){        c[i]=i;        for(int j=1;j<=m;j++){            scanf("%d",&x);            if(x<a[j])  b[j]=i;            a[j]=x;            if(b[j]<c[i])   c[i]=b[j];        }    }    scanf("%d",&k);    while(k--){        scanf("%d%d",&l,&r);        if(c[r]<=l) printf("Yes\n");        else printf("No\n");    }    return 0;}
0 0
原创粉丝点击