Codeforces 777C: Alyona and Spreadsheet (pre-treatment)

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 \(a_{i, 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 \(a_{i, j} ≤ a_{i + 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 \(a_{i, j} ≤ a_{i + 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 \(a_{i, j} (1 ≤ a_{i, j} ≤ 10^9)\).

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 \(l_i\) and \(r_i\) \((1 ≤ l_i ≤ r_i ≤ n)\).

Output

Print "Yes" to the \(i\)-th line of the output if the table consisting of rows from \(l_i\) to \(r_i\) 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\).

The meaning of problems

Gives a \ (n \ times m \) matrix, determines whether the first \ (L \) th to \ (R & lt \) line if there is a non-decreasing of

Thinking

If this problem with violence to write, time complexity is: \ (O (n-\ Times \ SUM ^ {K} _ {I =. 1} (r_i-L_i)) \) , has a problem seen, this time is in the affirmative make life difficult

So we can Pretreatment: The pretreatment each line up from the highest to which his party, can maintain at least a non-increasing sequence

To process each column of each of the non-increasing sequence may extend upwardly to a position which position corresponds to the position of each column and then to a maximum value, to obtain the maximum position of the line may extend upwardly.

Each entry \ (L, R & lt \) , is determined simply \ (R & lt \) line up position is less than equal to \ (L \) to

Code

Note $ (1 ≤ n · m ≤ 100 000) $, can directly vectorbe stored, one-dimensional arrays may also be used

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=1e6+10;
const int mod=1e9+7;
const int maxm=1e3+10;
using namespace std;
vector<int>ve[maxn];
// 当前行能往上延伸的最高位置
int can[maxn];
// 当前列能往上的最高位置
int line[maxn];
int main(int argc, char const *argv[])
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
        srand((unsigned int)time(NULL));
    #endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,m;
    cin>>n>>m;
    int x;
    for(int i=0;i<m;i++)
        ve[0].push_back(0);
    for(int i=1;i<=n;i++)
        for(int j=0;j<m;j++)
            cin>>x,ve[i].push_back(x);
    for(int i=1;i<=n;i++)
    {
        can[i]=i;
        for(int j=0;j<m;j++)
        {
            int now_num=ve[i][j];
            int up_num=ve[i-1][j];
            if(now_num<up_num)
                line[j]=i;
            can[i]=min(can[i],line[j]);
        }
    }
    int t;
    cin>>t;
    while(t--)
    {
        int l,r;
        cin>>l>>r;
        if(can[r]>l)
            cout<<"No\n";
        else
            cout<<"Yes\n";
    }
    #ifndef ONLINE_JUDGE
        cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl;
    #endif
    return 0;
}

Guess you like

Origin www.cnblogs.com/Friends-A/p/11569247.html