Codeforces-777C. Alyona and Spreadsheet
n*m维矩阵,Q次询问,查询行数为[l,r]的矩阵内是否有某一列满足自上而下数据非减
其中1 ≤ n·m ≤ 100 000
对于某一行的任一列,记录其对应能满足数据非减的最小行(B[i][j]),进而得出每一行能满足条件的最小行(bst[i]),查询时判断bst[r]与l的关系即可
显然当aij>a(i-1)j,B[i][j]=B[i-1][j], 否则B[i][j]=i;
在每一行结束时记录下bst[i],就可以重复利用数组B了,只使用一维数组了
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 #define INF 0x3f3f3f3f 6 #define MOD 1000000007 7 using namespace std; 8 typedef long long LL; 9 10 int N, M, Q; 11 const int maxn = 1e5 + 10; 12 int A[3][maxn]; 13 int B[maxn]; 14 int bst[maxn]; 15 16 int main() { 17 for (int j = 1; j <= M; j++) { 18 B[j] = 1; 19 } 20 scanf("%d%d", &N, &M); 21 for (int i = 1; i <= N; i++) { 22 int t = i & 1; 23 for (int j = 1; j <= M; j++) { 24 scanf("%d", &A[t][j]); 25 } 26 for (int j = 1; j <= M; j++) { 27 B[j] = A[t][j] >= A[!t][j] ? B[j] : i; 28 } 29 bst[i] = N + 1; 30 for (int j = 1; j <= M; j++) { 31 bst[i] = min(bst[i], B[j]); 32 } 33 } 34 scanf("%d", &Q); 35 int dn, up; 36 while (Q--) { 37 scanf("%d%d", &dn, &up); 38 if (bst[up] <= dn) { 39 puts("Yes"); 40 } else { 41 puts("No"); 42 } 43 } 44 return 0; 45 }