Submission #925837

#TimeUsernameProblemLanguageResultExecution timeMemory
925837mickey080929Abduction 2 (JOI17_abduction2)C++17
44 / 100
110 ms27488 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; int H, W, Q; map<ll,int> memo; int mxl[50010][16], mxr[50010][16], mxu[50010][16], mxd[50010][16]; int A[50010], B[50010]; int dfs(int x, int y, int dir) { if (memo.find((ll)x*(ll)W*2LL + (ll)y*2LL + (ll)dir) != memo.end()) return memo[(ll)x*(ll)W*2LL + (ll)y*2LL + (ll)dir]; int cur = 0; if (dir == 0) { int u = x - 1, d = x + 1; for (int lv=15; lv>=0; lv--) { if (u - (1 << lv) >= -1 && mxu[u][lv] < B[y]) u -= (1 << lv); if (d + (1 << lv) <= H && mxd[d][lv] < B[y]) d += (1 << lv); } if (u != -1) cur = max(cur, dfs(u, y, 1) + x - u); else cur = max(cur, x); if (d != H) cur = max(cur, dfs(d, y, 1) + d - x); else cur = max(cur, H - x - 1); } else { int l = y - 1, r = y + 1; for (int lv=15; lv>=0; lv--) { if (l - (1 << lv) >= -1 && mxl[l][lv] < A[x]) l -= (1 << lv); if (r + (1 << lv) <= W && mxr[r][lv] < A[x]) r += (1 << lv); } if (l != -1) cur = max(cur, dfs(x, l, 0) + y - l); else cur = max(cur, y); if (r != W) cur = max(cur, dfs(x, r, 0) + r - y); else cur = max(cur, W - y - 1); } memo[(ll)x*(ll)W*2LL + (ll)y*2LL + (ll)dir] = cur; return cur; } int main() { ios_base :: sync_with_stdio(false); cin.tie(NULL); cin >> H >> W >> Q; for (int i=0; i<H; i++) { cin >> A[i]; mxu[i][0] = mxd[i][0] = A[i]; } for (int i=0; i<W; i++) { cin >> B[i]; mxl[i][0] = mxr[i][0] = B[i]; } for (int lv=1; lv<=15; lv++) { for (int i=0; i<H; i++) { if (i - (1 << lv) >= -1) mxu[i][lv] = max(mxu[i][lv-1], mxu[i-(1<<(lv-1))][lv-1]); if (i + (1 << lv) <= H) mxd[i][lv] = max(mxd[i][lv-1], mxd[i+(1<<(lv-1))][lv-1]); } } for (int lv=1; lv<=15; lv++) { for (int i=0; i<W; i++) { if (i - (1 << lv) >= -1) mxl[i][lv] = max(mxl[i][lv-1], mxl[i-(1<<(lv-1))][lv-1]); if (i + (1 << lv) <= W) mxr[i][lv] = max(mxr[i][lv-1], mxr[i+(1<<(lv-1))][lv-1]); } } while (Q --) { int x, y; cin >> x >> y; x --; y --; cout << max(dfs(x, y, 0), dfs(x, y, 1)) << '\n'; } }
#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...