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 thei-th row and the j-th column. We say that the table is sorted in non-decreasing order in the columnj if ai, j ≤ ai + 1, j for alli from 1 to n - 1.
Teacher gave Alyona k tasks. For each of the tasks two integersl and r are given and Alyona has to answer the following question: if one keeps the rows froml 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 suchj that ai, j ≤ ai + 1, j for alli from l to r - 1 inclusive.
Alyona is too small to deal with this task and asks you to help!
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 integersli and ri (1 ≤ li ≤ ri ≤ n).
Print "Yes" to the i-th line of the output if the table consisting of rows fromli to ri inclusive is sorted in non-decreasing order in at least one column. Otherwise, print "No".
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
Yes No Yes Yes Yes No
题意:给你一个n*m的矩阵,k个询问,每个询问有l,r,让你求在l-r行里面有没有一列可以满足从上到下为非递减数列。
思路:二维数组存不了,用一维数组存储,怎么降低复杂度?由于k很大,必须做到O(k)复杂度,预处理就变得很重要,看代码;
//#include <bits/stdc++.h>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define maxn 100010
typedef long long LL;
using namespace std;
int a[maxn],b[maxn],c[maxn];
//a更新每行的信息,b更新每一列非递减的上限;
//c[i]更新i行为止最大能向上截止到j行有非递减数列(i>=j)
int main()
{
int n,m,x,k,l,r;
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(c[i]>b[j]) //有递减的存在,更新上限
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;
}