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. 【java】多线程同步生产者消费者问题

    package 多线程; class Producer implements Runnable{ private Data data; public Producer(Data data){ this ...

  2. HTML5 高级系列:web Storage

    前言 HTML5 的 web Storage 存储方式有两种:localStorage 和 sessionStorage. 这两种方式都是通过键值对保存数据,存取方便,不影响网站性能.他们的用法相同, ...

  3. iOS 进阶—— iOS 内存管理

    1 似乎每个人在学习 iOS 过程中都考虑过的问题 alloc retain release delloc 做了什么? autoreleasepool 是怎样实现的? __unsafe_unretai ...

  4. ArcGIS API for JavaScript 4.3学习笔记[新] AJS4.3和AJS3.20新特性

    今天"ArcGIS极客说"公众号推送了这两个大版本的更新,吓得我赶紧撸了一篇新博客. 这里就不写代码验证了,作为新特性小节简单介绍一下!~ AJS 4.3 1. 更强大的Featu ...

  5. 公牛与状压dp

    T1 疾病管理 裸得不能再裸的状压dp 不过数据范围骗人 考试时k==0的点没过 我也很无奈呀qwq #include<iostream> #include<cstdio> # ...

  6. bzoj 3932: [CQOI2015]任务查询系统

    Description 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的 任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si ...

  7. 关于html,css,js三者的加载顺序问题

    <head lang="en"> <meta charset="utf-8"> <title></title> ...

  8. linux下新建svn项目

    1.新建项目svnadmin create /mnt/fbdisk/svn/newproject 2.会在svn下面建立newproject目录total 24drwxr-xr-x 2 root ro ...

  9. Javascript流程控制

    Javascript流程控制 1.条件语句 (1)if(exp)执行一句代码 (2)if(exp){执行代码段;} (3)if(exp){exp为true执行代码段}else{exp为false执行的 ...

  10. HTTP结构

    HTTP结构 转载请注明出处:HTTP结构简介 HTTP通信过程包括从客户端发往服务器的请求和服务器返回客户端的响应,这篇文章就简单的了解一下HTTP请求和响应的结构与协议本身的状态管理. 用户HTT ...