Submission #172327

#TimeUsernameProblemLanguageResultExecution timeMemory
172327maruiiAbduction 2 (JOI17_abduction2)C++14
13 / 100
5089 ms824 KiB
#include <bits/stdc++.h> using namespace std; int H, W, Q; int A[50005], B[50005]; int pfa[50005], sfa[50005], pfb[50005], sfb[50005]; int D[2002][2002][2]; const int SIZE = 1 << 16; struct ST { int A[2 * SIZE]; void update(int x, int v) { for (x += SIZE; x; x >>= 1) A[x] = max(A[x], v); } int query(int s, int e) { int ret = 0; for (s += SIZE, e += SIZE; s <= e; s >>= 1, e >>= 1) { if ( s & 1) ret = max(ret, A[s++]); if (~e & 1) ret = max(ret, A[e--]); } return ret; } } a, b; int f(int x, int y, int d) { if (!x || !y) return -2e9; int &ret = D[x][y][d]; if (ret) return ret; if (d == 0) { if (max(pfa[x - 1], sfa[x + 1]) < B[y]) return ret = max(x - 1, H - x); int p, q; { int l = x + 1, r = H; if (sfa[x + 1] < B[y]) p = 0; else { while (l < r) { int m = l + r >> 1; if (a.query(x + 1, m) > B[y]) r = m; else l = m + 1; } p = l; } } { int l = 1, r = x - 1; if (pfa[x - 1] < B[y]) q = 0; else { while (l < r) { int m = l + r + 1 >> 1; if (a.query(m, x - 1) > B[y]) l = m; else r = m - 1; } q = r; } } return max(abs(x - p) + f(p, y, 1), abs(x - q) + f(q, y, 1)); } else { if (max(pfb[y - 1], sfb[y + 1]) < A[x]) return ret = max(y - 1, W - y); int p, q; { int l = y + 1, r = W; if (sfb[y + 1] < A[x]) p = 0; else { while (l < r) { int m = l + r >> 1; if (b.query(y + 1, m) > A[x]) r = m; else l = m + 1; } p = l; } } { int l = 1, r = y - 1; if (pfb[y - 1] < A[x]) q = 0; else { while (l < r) { int m = l + r + 1 >> 1; if (b.query(m, y - 1) > A[x]) l = m; else r = m - 1; } q = r; } } return max(abs(y - p) + f(x, p, 0), abs(y - q) + f(x, q, 0)); } } int main() { ios_base::sync_with_stdio(0), cin.tie(0); cin >> H >> W >> Q; if (H > 2000 || W > 2000) assert(0); for (int i = 1; i <= H; ++i) cin >> A[i], a.update(i, A[i]); for (int i = 1; i <= W; ++i) cin >> B[i], b.update(i, B[i]); for (int i = 1; i <= H; ++i) pfa[i] = max(pfa[i - 1], A[i]); for (int i = 1; i <= W; ++i) pfb[i] = max(pfb[i - 1], B[i]); for (int i = H; i; --i) sfa[i] = max(sfa[i + 1], A[i]); for (int i = W; i; --i) sfb[i] = max(sfb[i + 1], B[i]); for (int i = 0; i < Q; ++i) { int x, y; cin >> x >> y; printf("%d\n", max(f(x, y, 0), f(x, y, 1))); } return 0; }

Compilation message (stderr)

abduction2.cpp: In function 'int f(int, int, int)':
abduction2.cpp:37:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
      int m = l + r >> 1;
              ~~^~~
abduction2.cpp:49:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
      int m = l + r + 1 >> 1;
              ~~~~~~^~~
abduction2.cpp:66:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
      int m = l + r >> 1;
              ~~^~~
abduction2.cpp:78:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
      int m = l + r + 1 >> 1;
              ~~~~~~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...