竖向寻找矩阵最大递增元素长度,因为要求至少一列为递增数列,那么每行求一下最大值就可以作为len[i]:到i行截止的最长的递增数列长度。

C. Alyona and Spreadsheet
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std; int n, m;
const int Maxn = ;
vector<int>a[Maxn], dp[Maxn];
int len[Maxn]; int main()
{
cin>>n>>m;
for(int i = ; i <= n; i ++){
a[i].resize(m + );
dp[i].resize(m + );
for(int j = ; j <= m; j ++){
scanf("%d", &a[i][j]);
}
}
for(int j = ; j <= m; j ++) dp[][j] = ;
len[] = ;
for(int i = ; i <= n; i ++){
for(int j = ; j <= m; j ++){
if(a[i][j] >= a[i - ][j])
dp[i][j] = dp[i - ][j] + ;
else
dp[i][j] = ;
}
for(int j = ; j <= m; j ++)
len[i] = max(dp[i][j], len[i]);
}
int k, l, r;
cin>>k;
while(k --){
scanf("%d %d", &l, &r);
if(len[r] >= r - l + )
printf("Yes\n");
else
printf("No\n");
}
return ;
}

经典矩阵dp寻找递增最大长度的更多相关文章

  1. POJ2778&HDU2243&POJ1625(AC自动机+矩阵/DP)

    POJ2778 题意:只有四种字符的字符串(A, C, T and G),有M中字符串不能出现,为长度为n的字符串可以有多少种. 题解:在字符串上有L中状态,所以就有L*A(字符个数)中状态转移.这里 ...

  2. [经典算法题]寻找数组中第K大的数的方法总结

    [经典算法题]寻找数组中第K大的数的方法总结 责任编辑:admin 日期:2012-11-26   字体:[大 中 小] 打印复制链接我要评论   今天看算法分析是,看到一个这样的问题,就是在一堆数据 ...

  3. hdu 4975 最大流问题解决队伍和矩阵,利用矩阵dp优化

    //刚開始乱搞. //网络流求解,假设最大流=全部元素的和则有解:利用残留网络推断是否唯一, //方法有两种,第一种是深搜看看是否存在正边权的环.见上一篇4888 //至少四个点构成的环,另外一种是用 ...

  4. poj2114 寻找树上存在长度为k点对,树上的分治

    寻找树上存在长度为k点对,树上的分治  代码和  这个  差不多 ,改一下判断的就好 #include <iostream> #include <algorithm> #inc ...

  5. 矩阵dp

    矩阵dp 这里是矩阵dp,不是矩阵乘法优化dp. 矩阵上的dp好像也没什么特殊的?大概有一个套路就是从上向下,从左向右进行dp吧. 首先第一道题好像不是矩阵dp... 1005 矩阵取数游戏:http ...

  6. hdu4975 网络流解方程组(网络流+dfs判环或矩阵DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=4975 A simple Gaussian elimination problem. Time Limit: 20 ...

  7. Poj 2411 Mondriaan's Dream(压缩矩阵DP)

    一.Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, ...

  8. python得到最长递增子串长度及起始位置【转】

    def incSeq(seq): start = 0 for i in xrange(1, len(seq)): if seq[i] < seq[i-1]: yield start, i - s ...

  9. hdu 4975 最大流解决行列和求矩阵问题,用到矩阵dp优化

    //刚开始乱搞. //网络流求解,如果最大流=所有元素的和则有解:利用残留网络判断是否唯一, //方法有两种,第一种是深搜看看是否存在正边权的环,见上一篇4888 //至少四个点构成的环,第二种是用矩 ...

随机推荐

  1. 06-js的逻辑结构

    <html> <head> <title>js的逻辑结构和循环结构学习</title> <meta charset="UTF-8&quo ...

  2. Ubuntu 16.04安装UML工具StarUML 2

    StarUML 2是一个商业软件,但是没有时间限制,就像Sublime Text 3一样.而且具有跨平台,支持Mac.Windows. 这个软件曾经08年的时候在老D的博客上有推荐过,参考:http: ...

  3. spark开发环境配置

    以后spark,mapreduce,mpi可能三者集于同一平台,各自的侧重点有所不用,相当于云计算与高性能计算的集合,互补,把spark的基础看了看,现在把开发环境看看,主要是看源码,最近Apache ...

  4. mysql: reinit the password

    You can reinit the password : 1.stop mysql /etc/init.d/mysql stop 2.start mysql safe : mysqld_safe - ...

  5. MapReduce WordCount Combiner程序

    MapReduce WordCount Combiner程序 注意使用Combiner之后的累加情况是不同的: pom.xml <project xmlns="http://maven ...

  6. CentOS firewall添加开放端口

    添加 firewall-cmd --zone=public --add-port=80/tcp --permanent (–permanent永久生效,没有此参数重启后失效) 重新载入 firewal ...

  7. jqury-validate表单验证

    首先需要引入插件:jquery.validate.js这个插件. 然后对需要验证的表单实现js: $("#add-firewalls-form").validate({ submi ...

  8. redis 事务 及发布于订阅功能

    事务: Redis事务可以一次执行多个命令,事务具有以下特征: 1.隔离操作:事务中的所有命令都会序列化.按顺序地执行,不会被其他命令打扰. 2.原子操作:事务中的命令要么全部被执行,要么全部都不执行 ...

  9. 11种常见sqlmap使用方法

    sqlmap是渗透中常用的一个注入工具,其实在注入工具方面,一个sqlmap就足够用了,只要你用的熟,秒杀各种工具,只是一个便捷性问题. 一.SQLMAP用于Access数据库注入 (1) 猜解是否能 ...

  10. C/C++一些库函数的实现

    1. 写出String的具体实现 类的定义: #include <iostream> #include <cstring> using namespace std; class ...