思维:预处理,时间复杂度O(k)
一开始我用预处理+暴力,结果TLE100...
后面搜索到题解,才知道可以算出以r为边界行上能到达的最上端的行+预处理即可。。。思维还是太僵硬了。。。。
错误代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 100000 + 20;
int n,m,k;
vector<int>v[N];
vector<int>down[N];
int main()
{
ios_base::sync_with_stdio(false);
scanf("%d%d",&n,&m);
for(int i = 0;i < n;++ i){
for(int j = 0;j < m;++ j){
int x;
scanf("%d",&x);
v[i].push_back(x);
}
}
for(int i = 0;i < m;++ i){
int cnt = 1;
down[i].push_back(1);
for(int j = 1;j < n;++ j){
if(v[j-1][i] > v[j][i])
++ cnt;
down[i].push_back(cnt);
}
}
scanf("%d",&k);
for(int i = 0;i < k;++ i){
int l,r;
scanf("%d%d",&l,&r);
-- l, -- r;
bool flag = 0;
for(int j = 0;j < m;++ j){
if(down[j][l] == down[j][r]){
flag = 1;
break;
}
}
if(flag)
printf("Yes\n");
else
printf("No\n");
}
}
AC代码;
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
const int N = 100000 + 20;
int a[N],c[N],b[N],n,m,k;
int main()
{
ios_base::sync_with_stdio(false);
cin >> n >> m;
for(int i = 1;i <= m;++ i)
b[i] = 1;
for(int i = 1;i <= n;++ i){
int q = INF;
for(int j = 1;j <= m;++ j){
int x;
cin >> x;
if(a[j] > x)///a[i]是上一行的值
b[j] = i;
q = min(q,b[j]);
a[j] = x;
}
c[i] = q;
}
/*for(int i = 1;i <= n;++ i)
cout << c[i] << endl;*/
cin >> k;
for(int i = 0;i < k;++ i){
int l,r;
cin >> l >> r;
if(l >= c[r])
cout << "Yes\n";
else
cout << "No\n";
}
}