题目链接:https://vjudge.net/problem/CodeForces-777C
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 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行至少有一列是非递减的,则输出Yes,否则输出No
刚开始做时,没想到用一维数组进行存储,导致存储问题。只是去思考每行所能达到的最大行,而不是每行所能达到的最
小行。
思路:进行预处理,对于任意一行j,求出符合条件的i(i<=j).
具体看代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
int n,m,q;
const int INF=1e5+5;
int a[INF],b[INF];
int main()
{
scanf("%d%d",&n,&m);
int i,j,k;
for(i=1;i<=n;i++){
b[i]=i;//初始化b数组
for(j=1;j<=m;j++)
{
scanf("%d",&a[(i-1)*m+j]);//进行一维存储
}
}
//预处理
for(i=1;i<=m;i++)//处理每一列
{
b[1]=1;k=1;
for(j=2;j<=n;j++)
{
if(a[(j-1)*m+i]>=a[(j-2)*m+i]){
b[j]=min(b[j],k);//取最小值
}
else {
k=j;//注意更新下限
}
}
}
scanf("%d",&q);
for(i=1;i<=q;i++)
{
scanf("%d%d",&j,&k);
if(b[k]<=j){
printf("Yes\n");
}
else printf("No\n");
}
return 0;
}