• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

codeforces777C

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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 mcolumns. 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 ifai, 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 rinclusive 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 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 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 lito 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.

解题思路:

这道题有很多种其中用二维数组做耗时比较少,只需注意一下,先输入m和n,在确定数组范围就行

由于被zz队友演了一波跟我说不能用二维数组,然后我就用一维数组直接递推了,差别不大

题意是 求每个询问代表的l行到r行之间是否有至少一列非递减序列

核心思想就是由上往下推,得出每一行所能到达的最上层,并用数组储存起来,最后只需比较询问的r行的最上层是否<=l行

代码中a[]储存当前一行的数据,b[]储存当前每一列能到达的最上层的值,pre[]储存这一行所能到达的最上层也就b[]中值最小的;

实现代码:

#include <iostream>
using namespace std;
int n,m,a[100005],b[100005],pre[100005],x,l,r,i,j,k;
int main(){
    cin>>n>>m;
    for(i=1;i<=n;i++){
        pre[i]=i;
        for(j=1;j<=m;j++){
            cin>>x;
            if(x<a[j])
            b[j]=i;
            a[j]=x;
            if(b[j]<pre[i])
            pre[i]=b[j];
        }
    }
    cin>>k;
    while(k--){
        cin>>l>>r;
        if(pre[r]<=l)
        cout<<"Yes"<<endl;
        else
        cout<<"No"<<endl;
    }
}

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Struct和Class的区别C#之结构struct(转)发布时间:2022-07-14
下一篇:
C++死循环在语言层面的检测发布时间:2022-07-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap