#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
using vii = vector<ii>;
const ll MOD = 998244353;
const int INF = 1e9+9;
const int MAXN = 50004;
enum direction{right = 0, up = 1, left = 2, down = 3};
int n, m, a[MAXN], b[MAXN];
int sparseA[MAXN][20], sparseB[MAXN][20];
void sparseTable() {
for (int i = 0; i < n; i++) {
sparseA[i][0] = i;
}
for (int j = 1; j < 20; j++) {
for (int i = 0; i+(1<<j) <= n; i++) {
int &l = sparseA[i][j-1], &r = sparseA[i+(1<<(j-1))][j-1];
sparseA[i][j] = a[l] > a[r]? l : r;
}
}
for (int i = 0; i < m; i++) {
sparseB[i][0] = i;
}
for (int j = 1; j < 20; j++) {
for (int i = 0; i+(1<<j) <= m; i++) {
int &l = sparseB[i][j-1], &r = sparseB[i+(1<<(j-1))][j-1];
sparseB[i][j] = b[l] > b[r]? l : r;
}
}
}
inline int maxa(int l, int r) {
int logLen = 0;
for (int i = r-l; i > 1; i>>=1) logLen++;
int &L = sparseA[l][logLen], &R = sparseA[r-(1<<logLen)][logLen];
return a[L] > a[R]? L : R;
}
inline int maxb(int l, int r) {
int logLen = 0;
for (int i = r-l; i > 1; i>>=1) logLen++;
int &L = sparseB[l][logLen], &R = sparseB[r-(1<<logLen)][logLen];
return b[L] > b[R]? L : R;
}
int mid[4], midVal[4];
inline int dist(direction dir, int id) {
return midVal[dir] == -1? 0 : abs(2*id-mid[dir])/2+midVal[dir];
}
int solve(int s, int t, direction dir) {
if (s == -1 || s == n || t == -1 || t == m)
return 0;
using direction::right;
using direction::left;
int l = 0, r = m, d = 0, u = n;
mid[0] = mid[1] = mid[2] = mid[3] = 0;
midVal[0] = midVal[1] = midVal[2] = midVal[3] = -1;
// cerr << "solve " << s << " " << t << endl;
while (true) {
// cerr << "it [" << d << ", " << u << "]x[" << l << ", " << r << "]" << endl;
// for (int i = 0; i < 4; i++) {
// cerr << mid[i] << " " << midVal[i] << endl;
// }
int maxh = maxa(d, u);
int maxv = maxb(l, r);
if (b[maxv] > a[maxh]) {
int downVal = dist(down, maxv);
int upVal = dist(up, maxv);
// cerr << "downVal " << downVal << " upVal " << upVal << endl;
if (maxv <= t) {
l = maxv+1;
mid[left] = d-downVal+u-1+upVal;
midVal[left] = (-d+downVal+u+upVal+2)/2;
} else {
r = maxv;
mid[right] = d-downVal+u-1+upVal;
midVal[right] = (-d+downVal+u+upVal+2)/2;
}
if (maxv == t) {
if (dir == down) {
return upVal+u-s;
} else if (dir == up) {
return downVal+s-d+1;
} else {
return dist(left, s);
}
}
} else {
int leftVal = dist(left, maxh);
int rightVal = dist(right, maxh);
// cerr << "leftVal " << leftVal << " rightVal " << rightVal << endl;
if (maxh <= s) {
d = maxh+1;
mid[down] = l-leftVal+r-1+rightVal;
midVal[down] = (-l+leftVal+r+rightVal+2)/2;
} else {
u = maxh;
mid[up] = l-leftVal+r-1+rightVal;
midVal[up] = (-l+leftVal+r+rightVal+2)/2;
}
if (maxh == s) {
if (dir == left) {
return rightVal+r-t;
} else if (dir == right) {
return leftVal+t-l+1;
} else {
return dist(down, t);
}
}
}
}
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int q, s, t, ans;
cin >> n >> m >> q;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < m; i++) {
cin >> b[i];
}
sparseTable();
while (q--) {
// cin >> s >> t;
cin >> s >> t;
s--, t--;
using direction::right;
using direction::left;
if (a[s] < b[t]) {
ans = 1+max(solve(s, t-1, right), solve(s, t+1, left));
ans = max(solve(s, t, right), ans);
} else {
ans = 1+max(solve(s-1, t, up), solve(s+1, t, down));
ans = max(solve(s, t, up), ans);
}
cout << ans-1 << '\n';
// break;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
380 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
380 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
380 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
380 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
760 KB |
Output is correct |
13 |
Correct |
6 ms |
760 KB |
Output is correct |
14 |
Correct |
5 ms |
760 KB |
Output is correct |
15 |
Correct |
6 ms |
760 KB |
Output is correct |
16 |
Correct |
6 ms |
760 KB |
Output is correct |
17 |
Correct |
6 ms |
760 KB |
Output is correct |
18 |
Correct |
6 ms |
760 KB |
Output is correct |
19 |
Correct |
5 ms |
760 KB |
Output is correct |
20 |
Correct |
6 ms |
760 KB |
Output is correct |
21 |
Correct |
6 ms |
760 KB |
Output is correct |
22 |
Correct |
6 ms |
760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
380 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
380 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
760 KB |
Output is correct |
13 |
Correct |
6 ms |
760 KB |
Output is correct |
14 |
Correct |
5 ms |
760 KB |
Output is correct |
15 |
Correct |
6 ms |
760 KB |
Output is correct |
16 |
Correct |
6 ms |
760 KB |
Output is correct |
17 |
Correct |
6 ms |
760 KB |
Output is correct |
18 |
Correct |
6 ms |
760 KB |
Output is correct |
19 |
Correct |
5 ms |
760 KB |
Output is correct |
20 |
Correct |
6 ms |
760 KB |
Output is correct |
21 |
Correct |
6 ms |
760 KB |
Output is correct |
22 |
Correct |
6 ms |
760 KB |
Output is correct |
23 |
Correct |
25 ms |
9464 KB |
Output is correct |
24 |
Correct |
25 ms |
9464 KB |
Output is correct |
25 |
Correct |
28 ms |
9724 KB |
Output is correct |
26 |
Correct |
25 ms |
9464 KB |
Output is correct |
27 |
Correct |
26 ms |
9464 KB |
Output is correct |
28 |
Correct |
32 ms |
9592 KB |
Output is correct |
29 |
Correct |
28 ms |
9464 KB |
Output is correct |
30 |
Correct |
29 ms |
9464 KB |
Output is correct |
31 |
Correct |
31 ms |
9464 KB |
Output is correct |
32 |
Correct |
25 ms |
9464 KB |
Output is correct |
33 |
Correct |
26 ms |
9464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
764 KB |
Output is correct |
2 |
Correct |
7 ms |
760 KB |
Output is correct |
3 |
Correct |
6 ms |
760 KB |
Output is correct |
4 |
Correct |
6 ms |
760 KB |
Output is correct |
5 |
Correct |
6 ms |
760 KB |
Output is correct |
6 |
Correct |
16 ms |
760 KB |
Output is correct |
7 |
Correct |
16 ms |
700 KB |
Output is correct |
8 |
Correct |
14 ms |
760 KB |
Output is correct |
9 |
Correct |
15 ms |
760 KB |
Output is correct |
10 |
Correct |
17 ms |
760 KB |
Output is correct |
11 |
Correct |
13 ms |
760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
380 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
380 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
760 KB |
Output is correct |
13 |
Correct |
6 ms |
760 KB |
Output is correct |
14 |
Correct |
5 ms |
760 KB |
Output is correct |
15 |
Correct |
6 ms |
760 KB |
Output is correct |
16 |
Correct |
6 ms |
760 KB |
Output is correct |
17 |
Correct |
6 ms |
760 KB |
Output is correct |
18 |
Correct |
6 ms |
760 KB |
Output is correct |
19 |
Correct |
5 ms |
760 KB |
Output is correct |
20 |
Correct |
6 ms |
760 KB |
Output is correct |
21 |
Correct |
6 ms |
760 KB |
Output is correct |
22 |
Correct |
6 ms |
760 KB |
Output is correct |
23 |
Correct |
25 ms |
9464 KB |
Output is correct |
24 |
Correct |
25 ms |
9464 KB |
Output is correct |
25 |
Correct |
28 ms |
9724 KB |
Output is correct |
26 |
Correct |
25 ms |
9464 KB |
Output is correct |
27 |
Correct |
26 ms |
9464 KB |
Output is correct |
28 |
Correct |
32 ms |
9592 KB |
Output is correct |
29 |
Correct |
28 ms |
9464 KB |
Output is correct |
30 |
Correct |
29 ms |
9464 KB |
Output is correct |
31 |
Correct |
31 ms |
9464 KB |
Output is correct |
32 |
Correct |
25 ms |
9464 KB |
Output is correct |
33 |
Correct |
26 ms |
9464 KB |
Output is correct |
34 |
Correct |
6 ms |
764 KB |
Output is correct |
35 |
Correct |
7 ms |
760 KB |
Output is correct |
36 |
Correct |
6 ms |
760 KB |
Output is correct |
37 |
Correct |
6 ms |
760 KB |
Output is correct |
38 |
Correct |
6 ms |
760 KB |
Output is correct |
39 |
Correct |
16 ms |
760 KB |
Output is correct |
40 |
Correct |
16 ms |
700 KB |
Output is correct |
41 |
Correct |
14 ms |
760 KB |
Output is correct |
42 |
Correct |
15 ms |
760 KB |
Output is correct |
43 |
Correct |
17 ms |
760 KB |
Output is correct |
44 |
Correct |
13 ms |
760 KB |
Output is correct |
45 |
Correct |
28 ms |
9532 KB |
Output is correct |
46 |
Correct |
28 ms |
9548 KB |
Output is correct |
47 |
Correct |
37 ms |
9532 KB |
Output is correct |
48 |
Correct |
26 ms |
9568 KB |
Output is correct |
49 |
Correct |
27 ms |
9592 KB |
Output is correct |
50 |
Correct |
362 ms |
9592 KB |
Output is correct |
51 |
Correct |
295 ms |
9468 KB |
Output is correct |
52 |
Correct |
314 ms |
9464 KB |
Output is correct |
53 |
Correct |
309 ms |
9336 KB |
Output is correct |
54 |
Correct |
289 ms |
9468 KB |
Output is correct |
55 |
Incorrect |
235 ms |
9592 KB |
Output isn't correct |
56 |
Halted |
0 ms |
0 KB |
- |