Codeforces Round #401 (Div. 2)C. Alyona and Spreadsheet(暴力)

传送门

Description

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

Sample Input

5 41 2 3 53 1 3 24 5 2 35 5 3 24 4 3 461 12 54 53 51 31 5

Sample Output

YesNoYesYesYesNo

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.

思路

题意:给定一个nxm的矩阵,然后给出一些询问,在第x行到第y行之间是否存在至少一列的数呈非递减序列。

题解:暴力的求出每列数能往下到达的最大深度,然后记录下每一行可到达的最大深度的值。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 10005;

int main()
{
	int n,m,q,l,r;
	scanf("%d%d",&n,&m);
	int **a = (int **)malloc(n*sizeof(int *));
	int **b = (int **)malloc(n*sizeof(int *));
	int *len = (int *)malloc(n*sizeof(int));
	for (int i = 0;i < n;i++)	a[i] = (int *)malloc(m*sizeof(int)),b[i] = (int *)malloc(m*sizeof(int));

	for (int i = 0;i < n;i++)	for (int j = 0;j < m;j++)	scanf("%d",&a[i][j]);
	for (int i = 0;i < m;i++)
	{
		for (int j = 0;j < n;j++)
		{
			int pos = j;
			int cnt = 0;
			while (pos + 1< n &&a[pos][i] <= a[pos+1][i])	pos++,cnt++;
			for (int k = j;k <= pos;k++)	b[k][i] = pos;
			j += cnt;
		}
	}
	for (int i = 0;i < n;i++)
	{
		len[i] = b[i][0];
		for (int j = 1;j < m;j++)
		{
			len[i] = max(len[i],b[i][j]);
		}
	}
	scanf("%d",&q);
	while (q--)
	{
		scanf("%d%d",&l,&r);
		if (len[l-1] >= r - 1)	printf("Yes\n");
		else	printf("No\n");
	}
	return 0;
}

  

时间: 2024-08-07 04:48:22

Codeforces Round #401 (Div. 2)C. Alyona and Spreadsheet(暴力)的相关文章

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

题目链接:C. Alyona and Spreadsheet 题意: 给你一个n*m的矩阵,现在有k个询问,每次给你一个l,r,问你在[l,r]这行中能否有一列数十非递减的顺序 题解: 用vector来保存矩阵. 对于每一行n*m dp一下最远能达到的范围,然后询问的时候就判断l,r是否在这个范围内. 1 #include<bits/stdc++.h> 2 #define pb push_back 3 #define rs m+1,r,rt<<1|1 4 #define mst(a

Codeforces Round #401 (Div. 2) E 贪心,线段树

Codeforces Round #401 (Div. 2) A 循环节 B 暴力排一下 C 标记出来,但10^5,特耿直地码了个O(n^2)的上去,最气的是在最后3分钟的时候被叉== D 从后往前贪心暴糙一下就好.比赛时一眼瞄出来了不敢写,搞不懂这样竟然不会超时.. E. Hanoi Factory 题意:n个环体,内径a[i],外径b[i],高h[i].当 a[i+1]<b[i]<=b[i+1] 时,第 i 个环体可以堆在第 i+1个环体上.求可以堆出的最高高度. tags:佩服那些大佬,

Codeforces Round #401 (Div. 2) E. Hanoi Factory

题目链接:Codeforces Round #401 (Div. 2) E. Hanoi Factory 题意: 给你n个环,每个环有内径a,外径b,高度v,现在让你将这n个环重起来,问你能重的最大高度. 满足条件:bi>=bj,bj>ai.(i<j) 题解: 首先将所以数据离散化,然后我们先按b从大到小排序,以a为第二关键字. 这里有一个贪心的思想,就是b相同时,a越小的放上面,这样可以重更多. 然后用树状数组维护一下放当前环的最大值,更新一下答案就行. 这题也可以直接cdq分治,不过

Codeforces Round #401 (Div. 2)解题报告

A - Shell Game 1 #include <iostream> 2 #include<bits/stdc++.h> 3 #include <stack> 4 #include <queue> 5 #include <map> 6 #include <set> 7 #include <cstdio> 8 #include <cstring> 9 #include <algorithm> 10

【贪心】Codeforces Round #401 (Div. 2) D. Cloud of Hashtags

从后向前枚举字符串,然后从左向右枚举位. 如果该串的某位比之前的串的该位小,那么将之前的那串截断. 如果该串的某位比之前的串的该位大,那么之前那串可以直接保留全长度. 具体看代码. #include<cstdio> #include<iostream> #include<string> using namespace std; string a[500010]; int n,lens[500010],b[500010]; int main() { // freopen(

Codeforces Round #401 (div.2)

自己的号2000+了....临时找了一个1540的号,和一个大佬@ditoly一起打... 这是大佬↓ 迟到了一下,过了十分钟才报名..... A....... B.你有n个数,对面也有n个数,两两对决!!! 求最多大于对面的数多少场,大等于对面的数多少场. 直接贪心 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; i

Codeforces Round #401 (Div. 2) D. Cloud of Hashtags

题目链接:D. Cloud of Hashtags 题意: 给你n个字符串,让你删后缀,使得这些字符串按字典序排列,要求是删除的后缀最少 题解: 由于n比较大,我们可以将全部的字符串存在一个数组里面,然后记录一下每个字符串的开始位置和长度,然后从下面往上对比. 如果str[i][j]<str[i+1][j],直接退出,因为这里已经成为字典序. 如果str[i][j]>str[i+1][j],那么就把str[i][j]=0,表示从这里开始的后缀全部删掉. str[i][j]==str[i+1][

Codeforces Round #358 (Div. 2) D. Alyona and Strings(DP)

题目链接:点击打开链接 思路: 类似于LCS, 只需用d[i][j][k][p]表示当前到了s1[i]和s2[j], 形成了k个子序列, 当前是否和上一个字符和上一个字符相连形成一个序列的最长序列和. 细节参见代码: #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <string> #include <vector&

Codeforces Round #540 (Div. 3) C. Palindromic Matrix 【暴力】

任意门:http://codeforces.com/contest/1118/problem/C C. Palindromic Matrix time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Let's call some square matrix with integer values in its cells palind