Alyona and Spreadsheet ---CodeForces - 777C (思維題)

                                              Alyona and Spreadsheet

                                                       Time limit  1000 ms      Memory limit  262144 kB

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行至少有一列是單調不遞減的,就輸出Yes 否則輸出No

首先是存這個矩陣,題目給的大小是N*M<=1e5,所以簡單的二維數組是開不出來的,我們可以使用容器vector(類似於不定長數組)注意vector的性質,習慣從1進行計數的話,不要忘了做預處理

如果簡單的暴力查詢k個選項的話鐵定會超時,因此我們可以做一下預處理

我們使用一個數組 to[mm],其中to[i]表示第i行在滿足題目條件的情況下可以到達的最大行,當我們將to數組更新完成,直接比較to[l]和r的大小關係即可得出結果應該輸出什麼,代碼裏解釋的很詳細了

#include<cstdio>
#include<string>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int inf=0x3f3f3f3f;
const int mm=1e5+10; 

vector<int>v[mm];
int to[mm];//每一行所能到達的最大行  
int n,m;
int x;
int k;
int l,r; 

int main()
{
	cin>>n>>m;
	for(int i=0;i<=m;i++) 
		v[0].push_back(0);//全零行  
	for(int i=1;i<=n;i++){
		v[i].push_back(0);//空出來第一個  
		for(int j=1;j<=m;j++){
			scanf("%d",&x);
			v[i].push_back(x);
		}
	}
	for(int j=1;j<=m;j++){
		for(int i=1;i<=n;i++){//可以直接使用 while(i<=n) 
			int t;
			for(t=i+1;t<=n;t++)//循環到t結束,即最大可以到達t-1行  
				if(v[t][j]<v[t-1][j])
					break;	
			while(i<t){
				to[i]=max(to[i],t-1);//t行之前的全部更新  
				i++;
			}//退出時 i==t,不要忘了--  
			i--; 
		}	
	}
	cin>>k;
	while(k--){
		scanf("%d%d",&l,&r);
		if(to[l]>=r)
			printf("Yes\n");
		else 
			printf("No\n");
	}

	return 0;
}

 

發佈了84 篇原創文章 · 獲贊 15 · 訪問量 3632
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章