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

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.


题解:

给出一个表格,给出l和r,问在l行和r行内(包括l行和r行),是否最少有一列,这列数随行数的递增,为非递减数列。

其中有k个l和r,k<=1000000, 情况这么大,每读入l r就对表格就行访问肯定会超时的,所以只能打表,读l r时直接访问。

其实怎么打表我是不会做的,所以还是看了通过的代码,发现他们都好聪明。

方法如下:用tt[j]现时记录在i行内,在aij以上(包括aij)有多少个非递减数列项,用maxn[i]记录在i行里tt[j]的最大值,即记录在i行内,哪一列的非递减数列项的个数最多。

于是在输入l r时,r-l+1即为需要判断的数列项个数, maxn[r]即为实际的数列项个数,如果maxn[r]>=r-l+1, 即表明l r行内存在非递减数列。

代码如下:

#include<stdio.h>
#include<cstring>
#define MAX(a,b) (a>b?a:b)
int main()
{
int n,m,k,l,r;
scanf("%d%d",&n,&m);
int a[n+5][m+5],maxn[n+5],tt[m+5];
memset(maxn,0,sizeof(maxn));
memset(tt,0,sizeof(tt));
for(int i = 0; i<n; i++)
for(int j = 0; j<m; j++)
{
scanf("%d",&a[i][j]);
if(i==0 || a[i][j]>=a[i-1][j]) tt[j]++;
else tt[j] = 1;
maxn[i] = MAX(maxn[i],tt[j]);
}
scanf("%d",&k);
for(int i = 0; i<k; i++)
{
scanf("%d%d",&l,&r);
if(r-l+1<=maxn[r-1]) puts("Yes");
else puts("No");
}
return 0;
}

Codeforces Round #401 (Div. 2) C Alyona and Spreadsheet —— 打表的更多相关文章

  1. 【离线】【递推】【multiset】 Codeforces Round #401 (Div. 2) C. Alyona and Spreadsheet

    对询问按右端点排序,对每一列递推出包含当前行的单调不下降串最多向前延伸多少. 用multiset维护,取个最小值,看是否小于等于该询问的左端点. #include<cstdio> #inc ...

  2. Codeforces Round #401 (Div. 2) 离翻身就差2分钟

    Codeforces Round #401 (Div. 2) 很happy,现场榜很happy,完全将昨晚的不悦忘了.终判我校一片惨白,小董同学怒怼D\E,离AK就差一个C了,于是我AC了C题还剩35 ...

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

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

  4. Codeforces Round #401 (Div. 2)

    和FallDream dalao一起从学长那借了个小号打Div2,他切ABE我做CD,我这里就写下CD题解,剩下的戳这里 AC:All Rank:33 小号Rating:1539+217->17 ...

  5. Codeforces Round #401 (Div. 2) A,B,C,D,E

    A. Shell Game time limit per test 0.5 seconds memory limit per test 256 megabytes input standard inp ...

  6. Codeforces Round #401 (Div. 2) A B C 水 贪心 dp

    A. Shell Game time limit per test 0.5 seconds memory limit per test 256 megabytes input standard inp ...

  7. Codeforces Round #381 (Div. 1) B. Alyona and a tree dfs序 二分 前缀和

    B. Alyona and a tree 题目连接: http://codeforces.com/contest/739/problem/B Description Alyona has a tree ...

  8. Codeforces Round #381 (Div. 1) A. Alyona and mex 构造

    A. Alyona and mex 题目连接: http://codeforces.com/contest/739/problem/A Description Alyona's mother want ...

  9. Codeforces Round #381 (Div. 2) D. Alyona and a tree 树上二分+前缀和思想

    题目链接: http://codeforces.com/contest/740/problem/D D. Alyona and a tree time limit per test2 secondsm ...

随机推荐

  1. Remove Nth Node From End of List(链表,带测试代码)

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  2. stun简介

    转载 http://blog.csdn.net/mazidao2008/article/details/4934257 STUN(Simple Traversal of UDP over NATs,N ...

  3. Codeforces Gym - 101147J Whistle's New Car

    Discription Statements Whistle has bought a new car, which has an infinite fuel tank capacity. He di ...

  4. CF997D

    分析: 假设在第一个树上我们有一个长度为x的环,在第二树上我们有一个长度为y的环,那么可以在叉积树上构造出$\binom{x+y}{x}$个长度为x+y的环 问题的关键就变成了如何统计出在一个树上的长 ...

  5. hdu6219(最大空凸包)

    题意: 给一些点,求出一个最大的空凸包,这个凸包里没有任何给定点且要求这个凸包面积最大 分析: 枚举凸包左下角的点,然后dp[i][j]表示凸包的最后两条边是j->i和i->O情况下凸包的 ...

  6. Windows下Python的虚拟环境

    前言 在开发python的应用程序的时候,有时候会遇到依赖包的版本问题,比如之前开发tensorflow应用的时候需要用到python3.5以下的版本(还是python3),但是日常做其它应用的时候用 ...

  7. Jena+fuseki

    1.下载apache-jena-3.1.0.tar.gz,这个可以将ttl三元组文件或者xml文件加载 进入bin目录,执行./tdbloader2 --loc /path/for/database ...

  8. Ubuntu切换至root错误:su:Authentication failure解决

    当前用户切换到root出现这个错误的原因是没有创建root用户,解决如下: 1.打开终端,输入命令sudo passwd root 会提示输入新的用户密码,输入后会再确认一次,成功后会显示:passw ...

  9. lua——基础语法

    -- test lua: for learning lua grammar -- line comment --[[ block comment ]]-- -- print hello world p ...

  10. G - Specialized Four-Digit Numbers(1.5.2)

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit cid=1006#sta ...