Codeforces777C 技巧

C. 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 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!

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 integersli and ri (1 ≤ li ≤ ri ≤ n).

Output

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".

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

题意:给你一个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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值