题目:

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 thatai, 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.

思路参考了:ACM进阶之路

思路:

我们考虑从上向下更新,引入两个数组a{MAXN]:记录上一行各列的数。b[MAXN]:记录当前这一行的每一列能达到的最上面的符合条件(非严格单调递增)的下标。那么我们在讨论第3行时,就不需要考虑第一列的数是多少了,我们的两个数组已经把信息记录下来了。我们在引入c[MAXN],记录每一行能达到的最上面的符合条件(非严格单调递增)的下标。

code:

#include<cstdio>
const int MAXN=1e5+5;
int a[MAXN],b[MAXN],c[MAXN];
int main(){
    int n,m;scanf("%d%d",&n,&m);
    for(int i=1;i<=n;++i){
        c[i]=i;//初始化假设第i行恰好是分界点。
        for(int j=1;j<=m;++j){
            int x;scanf("%d",&x);
            if(x<a[j])b[j]=i;//如果这一行的某一个数x小于该行上一个数a[j],那么这一行的这一列的数最大能达到的下标是i。换句话说b[j]记录断点。
            a[j]=x;//更新,作为下一行的上一行。
            if(b[j]<c[i])c[i]=b[j];//c[i]为该行能达到的最小的下标。
        }
    }
    int k;scanf("%d",&k);
    while(k--){
        int l,r;scanf("%d%d",&l,&r);
        if(c[r]<=l)printf("Yes\n");
        else printf("No\n");
    }
}

codeforce #401 div2 Alyona and Spreadsheet 思维题相关推荐

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

  2. ACM思维题训练 Section A

    题目地址: 选题为入门的Codeforce div2/div1的C题和D题. 题解: A:CF思维联系–CodeForces -214C (拓扑排序+思维+贪心) B:CF–思维练习-- CodeFo ...

  3. little w and Soda(思维题)

    链接:https://ac.nowcoder.com/acm/contest/297/A 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言5242 ...

  4. ZOJ 3829 贪心 思维题

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...

  5. yoyo思维题(困难) 组合数学

    问题 B: yoyo思维题(困难) 时间限制: 1 Sec  内存限制: 256 MB 提交: 11  解决: 3 [提交][状态][讨论版][命题人:qianyouyou][Edit] [TestD ...

  6. 1884: 三个家庭(思维题)

    1884: 三个家庭 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 244 Solved: 81 [Submit][Status][Web Board] ...

  7. 思维题 UVA 10881 Piotr's Ants

    题目传送门 1 /* 2 题意:在坐标轴上一群蚂蚁向左或向右爬,问经过ts后,蚂蚁的位置和状态 3 思维题:本题的关键1:蚂蚁相撞看作是对穿过去,那么只要判断谁是谁就可以了 4 关键2:蚂蚁的相对位置 ...

  8. CF--思维练习-- CodeForces - 215C - Crosses(思维题)

    ACM思维题训练集合 There is a board with a grid consisting of n rows and m columns, the rows are numbered fr ...

  9. CodeForces - 1102A(思维题)

    https://vjudge.net/problem/2135388/origin Describe You are given an integer sequence 1,2,-,n. You ha ...

  10. ☆【CodeForces - 764C】Timofey and a tree (思维题,树的性质)

    题干: Each New Year Timofey and his friends cut down a tree of n vertices and bring it home. After tha ...

最新文章

  1. python 删除重复字符_Google面试问题指南:使用Python删除重复出现的字符
  2. 递归——阶乘加斐波那契数列(简单掌握递归思想的敲门砖)
  3. mysql基础之存储引擎
  4. centos7 减少/home分区空间,扩大/ 的空间
  5. 富国基金:基金公司是如何进行数据架构规划与实践的
  6. c语言显示cpuid_ccpuid:CPUID信息模块。范例:显示所有的CPUID信息
  7. FreeBSD使用网易镜像来安装软件
  8. 服装CAD计算机按钮在哪里,富仪服装CAD快捷键大全
  9. PPT嵌入视频,添加控件按钮控制视频播放
  10. b站上的计算机课程有哪些,B站课程排行榜,这届大学生最爱学什么?
  11. 计算机原理(3)主板上的CPU,存储器,南桥北桥等的总结
  12. hiberfil.sys文件过大
  13. edk2中的fdf文件简介
  14. 智商高的人都不爱说话,程序员也是如此吗?
  15. Python实现主播人气排行榜,带你发现人气王
  16. JavaFx笔记二:布局
  17. Oracle存储空间管理
  18. Mac电脑技巧:如何将Sublime Text设置成中文版
  19. 技术牛人在阿里内网的公开信:“王坚,你为什么要放弃”
  20. bugreport_xf.exe 系统错误 丢失LIBEAY32.dll

热门文章

  1. 跨专业考c语言程序设计,多位跨考大神,教你如何跨专业上岸
  2. 中创向心力:如何把思想政治教育贯穿职业教育全过程?
  3. 编辑ListView(Edit,Update,Inse
  4. 电脑无缘无故黑屏,无法调节亮度。
  5. 电脑远程vps时无法复制粘贴怎么办
  6. npm init 自动创建 package.json 文件
  7. 【前端就业课 第一阶段】HTML5 零基础到实战(二)超链接
  8. 或许再过两年,ASML将可以自由给中国供应EUV光刻机
  9. python mysql library,python调用mysql报错解决方案
  10. 完全背包问题 旅游付费