您的位置:首页 > 其它

codeforces 777C Alyona and Spreadsheet(DP+思维)

2017-08-02 20:07 375 查看
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 mcolumns. 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 rinclusive 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 r
4000
espectively. 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 lito 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.

 【题解】 这道题刚开始用二维数组写,但是题目给的范围比较大,数组炸了,过了77组数据就re了,后来再怎么改都不对了,最后才傻乎乎的注意到这是DP专题里的题,这才用动规思想,一列列推出来,修BUG修了好久,唉,总算是AC了, 不说了,,看代码吧。简单易懂。

  其实就是一个m*n的矩阵,先预处理一下,处理出一个数组tmp[i] = x,表示这一列中,从第x行到第i行是非递减存在的。而我们只需要在这个过程中维护一个pre[i] = x,表示从第x行到第i行是满足某一列是非递减的就好,最后查询的时候只要pre[b]<=a就是满足的,否则就是不满足的。

 【AC代码】

 

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
const int N=1e5+2;
vector<int>aa
;
int pre
;
int tmp
;
int m,n;

int main()
{
std::ios::sync_with_stdio(false);
int t,a,b;
cin>>m>>n;
for(int i=1;i<=m;++i){
aa[i].push_back(0);
for(int j=1;j<=n;++j)
{
int x;
cin>>x;
aa[i].push_back(x);
}
}
memset(pre,0x3f3f3f3f,sizeof(pre));
for(int i=1;i<=n;++i)
{
for(int j=1;j<=m;++j)
{
if(j==1) tmp[j]=1;
else
{
if(aa[j][i]>=aa[j-1][i]) tmp[j]=tmp[j-1];
else tmp[j]=j;
}
}
for(int j=1;j<=m;++j)
pre[j]=min(pre[j],tmp[j]);
}
cin>>t;
while(t--)
{
cin>>a>>b;
if(pre[b]<=a) cout<<"Yes\n";
else cout<<"No\n";
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: