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.

题目链接:http://codeforces.com/problemset/problem/777/C

分析:

题意:给你一个n*m的矩阵,k个询问,每个询问有l,r,让你求在l-r行里面有没有一列可以满足从上到下为非递减数列。

解题思路:刚开始想到直接从下到上更新就好了,但是发现一个问题,n*m<=100000,那么n,m都有可能为100000,所以不能开二维数组更新,所以想到从上到下更新,然后用一个一维数组更新每行的数,用另一个一维数组更新这一行的每一列能到达的最上面的非递增数列,再用最后一个数组更新这一行能到达的最上面的非递增数列即可,然后在询问的时候就能用O(k)的时间算出答案了。

 #include <bits/stdc++.h>
using namespace std;
#define maxn 100005
int main()
{
int a[maxn],b[maxn],c[maxn];
int n,m,x;
int k,l,r;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
c[i]=i;
for(int j=;j<=m;j++)
{
scanf("%d",&x);
if(x<a[j]) b[j]=i;
a[j]=x;
if(b[j]<c[i])
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 ;
}

Codeforces 777C Alyona and Spreadsheet的更多相关文章

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

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

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

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

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

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

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

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

  6. codeforces 777C

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

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

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

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

  9. Codeforces 777C:Alyona and Spreadsheet(预处理)

    During the lesson small girl Alyona works with one famous spreadsheet computer program and learns ho ...

随机推荐

  1. sourceTree每次拉取代码和提交代码都需要输入密码

    今天新安装的sourceTree导入项目,拉取代码的时候一直提示让我输入git密码,每次拉取和提交的时候都需要重新输入密码,甚是麻烦,在网上,搜索,解决办法五花八门,这里提供一种简单有效的方法供大家参 ...

  2. mysql 先分组在排序

    mysql语句的语法模板: select distinct <select_list> from <left_table><join_type> join < ...

  3. 三菱Q系列PLC的智能功能模块程序

    一.模拟量输入模块Q64AD 1.模块开关或者参数设置 1.1I/O分配 1.2开关设置使用通道1,0-5v, 1.3使用GX configurator设置自动刷新PLC设置智能功能模块参数,即将模拟 ...

  4. ArcGIS API for JavaScript 4.2学习笔记[22] 使用【QueryTask类】进行空间查询 / 弹窗样式

    上一篇写道,使用Query类进行查询featureLayer图层的要素,也简单介绍了QueryTask类的使用. 这一篇博文继续推进,使用Query类和QueryTask类进行空间查询,查询USA的著 ...

  5. Ubuntu配置Django+ Apache2+ mysql

    # 我的Ubuntu上自带的python3.5,所以安装一下 python3.6sudo add-apt-repository ppa:jonathonf/python-3.6sudo apt-get ...

  6. Oracle 启动参数

    查看数据库的SID DB_NAME SERVICE_NAME

  7. Django2文档-入门概览

    Django 概览 Django 是设计是为了使通用的Web开发任务变得快速而又简单, 一下是如何使用Django编写数据库驱动的Web应用程序的非正式概述. 这个文档的目标是给你足够的技术细节来理解 ...

  8. Java容器---List

          List 承诺可以将元素维护在特定的序列中.List 接口在Collection的基础上添加了大量的方法,使得可以在List的中间插入和移除元素. 有两种类型的List: -----基本的 ...

  9. echart异步刷新图表,详细配置注释

    echarts刷新技巧: echartData.chear(); //当异步改变数据时,配合echartData .setOption(option)才会有动画效果 echartData.resize ...

  10. jQuery 文档操作方法

    jQuery 文档操作方法 这些方法对于 XML 文档和 HTML 文档均是适用的,除了:html(). 方法 描述 addClass() 向匹配的元素添加指定的类名. after() 在匹配的元素之 ...