H - Alyona and Spreadsheet

H - Alyona and Spreadsheet

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 ifai,?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 alli 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 lito ri inclusive is sorted in non-decreasing order in at least one column. Otherwise, print "No".

Example

Input

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

Output

YesNoYesYesYesNo题意:第一行输入两个整数n,m(1?≤?n·m?≤?100?000),第二行输入一个n×m的矩阵,然后再输入一个整数k,接下来k行,每行两个整数r,l,询问从r到l行,如果存在一列数是非递减的,就输出Yes,否则就输出No。题解:1.从上到下非递减,则上面的数比下面的数大就满足不了。   2.用二维数组用的不好就会超时,所以用一维数组a【】来存储每一行的数   3. 从后往前推,用一维数组b【】来存储每一列能到达的最上行,在用c【】来存储每一行能到达的最上行  4.最后只需要比较c【r】与l的大小即可。详情请看代码。代码:
#include<iostream>
using namespace std;
int a[100005],b[100005],c[100005];
int main()
{
    int n,m,x,i,j,r,l,k;
    cin>>n>>m;
    for(i=1;i<=m;i++)//没个数能到的最上一行是第一行。
        b[i]=1;
    for(i=1;i<=n;i++){
        c[i]=i;    //每个数都能到达所在的行。
        for(j=1;j<=m;j++){
            cin>>x;
            if(x<a[j])//如果x上面的数比x大,则不是非递增的,所以x不能到达桑拿一行。
                b[j]=i;
            a[j]=x;  //存储每一行的数。
            if(b[j]<c[i]) //找每一列能到达的最上行。
              c[i]=b[j];
        }
    }
    cin>>k;
    while(k--){
        cin>>r>>l;
        if(c[l]<=r)
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
    }
    return 0;
}
时间: 2024-10-07 22:39:30

H - 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 777C Alyona and Spreadsheet(思维)

题目链接 Alyona and Spreadsheet 记a[i][j]为读入的矩阵,c[i][j]为满足a[i][j],a[i - 1][j], a[i - 2][j],......,a[k][j]不上升的k的最小值. d[i]为max(c[i][j]) (1 <= j <= m) 那么对于每次询问l,r,若d[r] <= l,那么就符合,反之不符. 因为这里只说名了1 <= nm <= 100000,所以二维数组两个维的大小不确定. 数据可能有n=1, m=100000,

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 in

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

C++ GUI Qt4编程(10)-3.4spreadsheet

1. C++ GUI Qt4编程第三章,增加spreadsheet. 2. spreadsheet.h 1 /**/ 2 #ifndef SPREADSHEET_H 3 #define SPREADSHEET_H 4 5 #include <QTableWidget> 6 7 class Spreadsheet : public QTableWidget 8 { 9 Q_OBJECT 10 11 public: 12 Spreadsheet(QWidget *parent = 0); 13 v

【QT】C++ GUI Qt4 学习笔记3

菜单界面的实现. 看书上第三章,好长,好多代码.我敲了半天,想看看效果,结果却显示不出来.仔细一看,发现spreadsheet的实现在第四章.郁闷.... 又到官网上下代码,结果居然不能运行.难道是因为我的版本太高了? 只好自己改,把没实现的部分都先忽略掉,即忽略掉具体的功能,只是显示菜单.折腾了半天,搞定了. 总结一下: 创建菜单: 需要再主窗口类中声明 1.QMenu * 代表一个菜单 2.QAction * 代表菜单中的一个动作 一般一个菜单里会有很多歌动作 3.每个动作对应的槽函数 然后

CCPC-Wannafly Summer Camp 2019 全记录

// 7.19-7.29 东北大学秦皇岛校区十天训练营,题目都挂在了Vjudge上.训练期间比较忙,没空更博总结,回来继续补题消化. // https://vjudge.net/contest/312902 https://vjudge.net/contest/313217 https://vjudge.net/contest/313584 https://vjudge.net/contest/314412 https://vjudge.net/contest/314730 https://vj

POJ1420 Spreadsheet(拓扑排序)注意的是超内存

Spreadsheet Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 617   Accepted: 290 Description In 1979, Dan Bricklin and Bob Frankston wrote VisiCalc, the first spreadsheet application. It became a huge success and, at that time, was the ki

Codeforces 739B Alyona and a tree (树上路径倍增及差分)

题目链接 Alyona and a tree 弄了好几个小时终于Accepted了,之后发现这个题是Div1的. 比较考验我思维的一道好题. 首先,做一遍DFS预处理出t[i][j]和d[i][j].t[i][j]表示从第i个节点到离他第2^j近的祖先,d[i][j]表示从i开始到t[i][j]的路径上的路径权值总和. 在第一次DFS的同时,对节点x进行定位(结果为dist(x, y)<=a(y))的离x最远的x的某个祖先,然后进行O(1)的差分. 第一次DFS完成后,做第二次DFS统计答案(统