正在加载...
题干:
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 ofnrows andmcolumns. Byai, jwe will denote the integer located at thei-th row and thej-th column. We say that the table is sorted in non-decreasing order in the columnjifai, j ≤ ai + 1, jfor allifrom1ton - 1.
Teacher gave Alyonaktasks. For each of the tasks two integerslandrare given and Alyona has to answer the following question: if one keeps the rows fromltorinclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column? Formally, does there exist suchjthatai, j ≤ ai + 1, jfor allifromltor - 1inclusive.
Alyona is too small to deal with this task and asks you to help!
Input
The first line of the input contains two positive integersnandm(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 followingnlines containsmintegers. Thej-th integers in theiof these lines stands forai, j(1 ≤ ai, j ≤ 109).
The next line of the input contains an integerk(1 ≤ k ≤ 100 000)— the number of task that teacher gave to Alyona.
Thei-th of the nextklines contains two integersliandri(1 ≤ li ≤ ri ≤ n).
Output
Print "Yes" to thei-th line of the output if the table consisting of rows fromlitoriinclusive 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 column1, while rows 4–5 are sorted in column3.
题目大意:
给出n*m的矩阵,有k次查询,每次查询给出l和r,如果第l行到r行至少有一列是非递减的,则输出Yes,否则输出No
解题报告:
预处理一个数组sum,按列更新,考虑sum[i][j],如果这个数比他上面那个数大的话,就sum[i-1][j]+1,否则为1.
然后看到给出的询问都是对于行的,所以我们先把对于每一行而言,每一列的信息都压缩给这一行,维护一个c数组,对于每一行,二分查找他能递增到的最大的行,对于check函数,只要判断区间和是否等于区间长度即可,注意取前缀和的时候不能sum[r]-sum[l-1]了,因为这个前缀信息是断裂的,所以需要sum[r]-sum[l]+1这样。每次查询的时候O1输出即可。总复杂度O(n*m*log(n))
AC代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
int n,m,c[MAX];
vector<vector<int>> vv,sum;
bool ok(int up,int down) {
for(int j = 1; j<=m; j++) {
if(sum[down][j] - sum[up][j] +1 == down-up+1) return 1;
}return 0;
}
int main()
{
cin>>n>>m;
vv.resize(n+1);sum.resize(n+1);
for(int i = 0; i<=n; i++) {
vv[i].resize(m+1);sum[i].resize(m+1);
}
for(int x,i = 1; i<=n; i++) {
for(int j = 1; j<=m; j++) scanf("%d",&x),vv[i][j]=x;
}
for(int j = 1; j<=m; j++) {
for(int i = 1; i<=n; i++) {
if(vv[i][j] >= vv[i-1][j]) sum[i][j] = sum[i-1][j]+1;
else sum[i][j] = 1;
}
}
for(int i = 1; i<=n; i++) {
int l=i,r=n,mid,ans=0;
while(l<=r) {
mid=(l+r)>>1;
if(ok(i,mid)) l = mid+1,ans = mid;
else r = mid-1;
}c[i] = ans;
}
int q,l,r;
cin>>q;
while(q--) {
scanf("%d%d",&l,&r);
if(c[l] >= r) puts("Yes");
else puts("No");
}
return 0 ;
}