-
Notifications
You must be signed in to change notification settings - Fork 1
/
UVA_12192.cpp
71 lines (62 loc) · 1.54 KB
/
UVA_12192.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <bits/stdc++.h>
#define REP(i, a, b) for(int i = (a), _b = (b); i < _b; ++i)
#define FOR(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for(int i = (a), _b = (b); i >= _b; --i)
#define puts(_content_) cout << _content_ << endl;
#define endl "\n"
#define newline cout << "\n"
using namespace std;
int n, m;
int vmin, vmax;
vector<int> dia[1005];
int upper_bound (int val, vector<int> & vi) {
int left = 0, right = vi.size() - 1;
while(left < right) {
int mid = (left + right - 1)/2;
if(vi[mid] < val) left = mid + 1;
else right = mid;
}
return (vi[left] >= val) ? left : -1;
}
int lower_bound (int val, vector<int> & vi) {
int left = 0, right = vi.size() - 1;
while(left < right) {
int mid = (left + right + 1)/2;
if(vi[mid] > val) right = mid - 1;
else left = mid;
}
return (vi[left] <= val) ? left : -1;
}
int main() {
ios::sync_with_stdio(false);
#ifndef Home
freopen ("input.txt", "r", stdin);
#endif
while(cin >> n >> m) {
if(n + m == 0) return 0;
FOR(i, 500 - m, 500 + n) dia[i].clear();
int x;
REP(i, 0, n) REP(j, 0, m) {
cin >> x;
dia[500 + i - j].push_back(x);
}
int queries;
cin >> queries;
while(queries--) {
cin >> vmin >> vmax;
int answer = 0;
FOR(i, 500 - m, 500 + n) {
if((int)dia[i].size() > answer) {
int p1 = upper_bound(vmin, dia[i]);
if(p1 == -1) continue;
int p2 = lower_bound(vmax, dia[i]);
if(p2 == -1) continue;
answer = max(answer, (p2 - p1 + 1));
}
}
puts(answer);
}
puts("-");
}
return 0;
}