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 \(a_{i, 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 \(a_{i, j} ≤ a_{i + 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 \(a_{i, j} ≤ a_{i + 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 \(a_{i, j} (1 ≤ a_{i, j} ≤ 10^9)\).

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 \(l_i\) and \(r_i\) \((1 ≤ l_i ≤ r_i ≤ n)\).

Output

Print "Yes" to the \(i\)-th line of the output if the table consisting of rows from \(l_i\) to \(r_i\) 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\times m\)的矩阵,判断第\(l\)行~第\(r\)行中是否有一列是非递减的

思路

如果这题用暴力来写的话,时间复杂度为:\(O(n\times \sum^{k}_{i=1}(r_i-l_i))\),有题目可知,这个时间是肯定过不去的

所以我们可以预处理:预处理从每一行往上最高到哪一行,可以保持有至少一个非递增的序列

先处理每一列的每一个位置向上的非递增序列可以延伸到哪个位置,然后每一列的对应位置去一个最大值,即可得到该行可以向上延伸的最大位置。

每次输入\(l,r\),只需判断\(r\)行向上的位置是否小于等于\(l\)即可

代码

注意$ (1 ≤ n·m ≤ 100 000)$,可以直接用vector进行存,也可以用一维数组

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=1e6+10;
const int mod=1e9+7;
const int maxm=1e3+10;
using namespace std;
vector<int>ve[maxn];
// 当前行能往上延伸的最高位置
int can[maxn];
// 当前列能往上的最高位置
int line[maxn];
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
srand((unsigned int)time(NULL));
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int n,m;
cin>>n>>m;
int x;
for(int i=0;i<m;i++)
ve[0].push_back(0);
for(int i=1;i<=n;i++)
for(int j=0;j<m;j++)
cin>>x,ve[i].push_back(x);
for(int i=1;i<=n;i++)
{
can[i]=i;
for(int j=0;j<m;j++)
{
int now_num=ve[i][j];
int up_num=ve[i-1][j];
if(now_num<up_num)
line[j]=i;
can[i]=min(can[i],line[j]);
}
}
int t;
cin>>t;
while(t--)
{
int l,r;
cin>>l>>r;
if(can[r]>l)
cout<<"No\n";
else
cout<<"Yes\n";
}
#ifndef ONLINE_JUDGE
cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl;
#endif
return 0;
}

Codeforces 777C:Alyona and Spreadsheet(预处理)的更多相关文章

  1. Codeforces 777C Alyona and Spreadsheet

    C. Alyona and Spreadsheet time limit per test:1 second memory limit per test:256 megabytes input:sta ...

  2. Codeforces 777C Alyona and Spreadsheet(思维)

    题目链接 Alyona and Spreadsheet 记a[i][j]为读入的矩阵,c[i][j]为满足a[i][j],a[i - 1][j], a[i - 2][j],......,a[k][j] ...

  3. Codeforces 777C - Alyona and Spreadsheet - [DP]

    题目链接:http://codeforces.com/problemset/problem/777/C 题意: 给定 $n \times m$ 的一个数字表格,给定 $k$ 次查询,要你回答是否存在某 ...

  4. codeforces 777C.Alyona and Spreadsheet 解题报告

    题目链接:http://codeforces.com/problemset/problem/777/C 题目意思:给出一个 n * m 的矩阵,然后问 [l, r] 行之间是否存在至少一列是非递减序列 ...

  5. Codeforces Round #401 (Div. 2) C Alyona and Spreadsheet —— 打表

    题目链接:http://codeforces.com/contest/777/problem/C C. Alyona and Spreadsheet time limit per test 1 sec ...

  6. C Alyona and Spreadsheet Codeforces Round #401(Div. 2)(思维)

    Alyona and Spreadsheet 这就是一道思维的题,谈不上算法什么的,但我当时就是不会,直到别人告诉了我,我才懂了的.唉 为什么总是这么弱呢? [题目链接]Alyona and Spre ...

  7. codeforces 777C

    C.Alyona and Spreadsheet During the lesson small girl Alyona works with one famous spreadsheet compu ...

  8. Codeforces777C Alyona and Spreadsheet 2017-05-04 17:46 103人阅读 评论(0) 收藏

    C. Alyona and Spreadsheet time limit per test 1 second memory limit per test 256 megabytes input sta ...

  9. Codeforces E. Alyona and a tree(二分树上差分)

    题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

随机推荐

  1. 33、搜索旋转排序数组 | 算法(leetode,附思维导图 + 全部解法)300题

    零 标题:算法(leetode,附思维导图 + 全部解法)300题之(33)搜索旋转排序数组 一 题目描述! 题目描述 二 解法总览(思维导图) 三 全部解法 1 方案1 1)代码: // 方案1 & ...

  2. IPFS是什么?IPFS原理、IPFS存储

    以下内容调研截止到2021/11/5日 IPFS简介 IPFS是一种内容可寻址.点对点.分布式文件系统.IPFS采用内容-地址寻址技术,即通过文件内容进行检索而不是通过文件的网络地址.简单来说,就是对 ...

  3. Shell $()、${}、$[]、$(())

    目录 Shell中的 $().${}.$[].$(()) $().${} 替换 ${} 变量内容的替换.删除.取代 数组 $[].$(()) 运算符 Shell中的 $().${}.$[].$(()) ...

  4. 100个Shell脚本——【脚本8】每日生成一个文件

    [脚本8]每日生成一个文件 要求:请按照这样的日期格式(xxxx-xx-xx)每日生成一个文件,例如今天生成的文件为)2017-07-05.log, 并且把磁盘的使用情况写到到这个文件中,(不用考虑c ...

  5. 【Java 8】 集合间转换工具——Stream.collect

    集合运算 交集 (list1 + list2) List<T> intersect = list1.stream() .filter(list2::contains) .collect(C ...

  6. liunux 6.5设置网卡默认开启

    编辑如下文件; vi /etc/sysconfig/network-scripts/ifcfg-eth0 把 ONBOOT=no 改为 ONBOOT=yes 好了网卡会在启动机器的时候一起启动了.

  7. Tomcat(1):安装Tomcat

    一,安装Tomcat服务器 1,下载tomcat网址: http://tomcat.apache.org/ 2,找到Download 3,下载 4:下载完成后,解压到任意目录 5:解压完成后得到目录 ...

  8. 重量级&轻量级

    重量级 就是说包的大小,还有就是与个人项目的耦合程度,重量级的框架与项目耦合程度大些 代表EJB容器的服务往往是"买一送三",不要都不行 轻量级 就是相对较小的包,当然与项目的耦合 ...

  9. 【Linux】【Shell】【Basic】数组

    1. 数组:         变量:存储单个元素的内存空间:         数组:存储多个元素的连续的内存空间:             数组名:整个数组只有一个名字:             数组 ...

  10. Java中的循环结构进阶

    循环结构进阶 学习本章用到的单词 triangle:三角形 circle:圆形 diamond:钻石 password:密码 row:行.排列 二重循环结构 简单的说:二重循环就是一个循环体内又包含另 ...