您的位置:首页 > 其它

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

2017-02-25 21:08 579 查看
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 thatai, 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.

题意:n行m列的格子,每个格子里都有一个数,如果有一列的元素都非递减,那么就称这列为非递减列。现在给你l和r求l行到r行之间的这m列是否存在非递减列。

思路:orz强势暴力TLE了...哈哈...

这道题其实也很水了....当时愣是没想到....菜...还是某巨给了我思路

没啥好说的了,开一个数组ans[i]记录下以当前行为右边界递增的话它的左边界最小是第几行,然后输入一对l,r ans[r]<=l 就满足。

至于怎么求这个ans,就是一个简单的dp 如果当前值比上一行打就dp[i][j]=dp[i-1][j] 否则dp[i][j]=i;

注意要对每列取一个最值

具体见代码

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
int a[2][maxn];
vector<int>dp[maxn];
int ans[maxn];
int main()
{
int n,m,k,l,r;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
ans[i]=i;//初始为自己所在的行
for(int j=0;j<m;j++)
{
if(i==1)
{
scanf("%d",&a[1][j]);
dp[i].push_back(i);
}
else
{
scanf("%d",&a[1][j]);
if(a[1][j]>=a[0][j])
{
dp[i].push_back(dp[i-1][j]);
}
else
{
dp[i].push_back(i);
}
}
ans[i]=min(ans[i],dp[i][j]);
}
for(int j=0;j<m;j++)
a[0][j]=a[1][j];
}
scanf("%d",&k);
for(int i=0;i<k;i++)
{
scanf("%d%d",&l,&r);
if(ans[r]<=l)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: