Submission #958384

#TimeUsernameProblemLanguageResultExecution timeMemory
958384Soumya1Abduction 2 (JOI17_abduction2)C++17
44 / 100
87 ms18516 KiB
#include <bits/stdc++.h> using namespace std; const int mxN = 5e4 + 5; const int lg = 16; int a[mxN][lg], b[mxN][lg]; map<int, int> dp; int n, m, q; int solve(int x, int y, int dir) { if (x <= 0 || y <= 0 || x > n || y > m) return -1; int node = (x * (m + 1) + y) * 2 + dir; if (dp.find(node) != dp.end()) return dp[node]; int ans = 0; if (dir == 0) { int ry = y + 1, ly = y - 1; for (int i = lg - 1; i >= 0; i--) { if (ry + (1 << i) - 1 <= m && b[ry][i] <= a[x][0]) ry += (1 << i); if (ly - (1 << i) + 1 >= 1 && b[ly - (1 << i) + 1][i] <= a[x][0]) ly -= (1 << i); } ans = max(ans, solve(x, ry, 1) + ry - y); ans = max(ans, solve(x, ly, 1) + y - ly); } else { int rx = x + 1, lx = x - 1; for (int i = lg - 1; i >= 0; i--) { if (rx + (1 << i) - 1 <= n && a[rx][i] <= b[y][0]) rx += (1 << i); if (lx - (1 << i) + 1 >= 1 && a[lx - (1 << i) + 1][i] <= b[y][0]) lx -= (1 << i); } ans = max(ans, solve(rx, y, 0) + rx - x); ans = max(ans, solve(lx, y, 0) + x - lx); } return dp[node] = ans; } void testCase() { cin >> n >> m >> q; for (int i = 1; i <= n; i++) cin >> a[i][0]; for (int i = 1; i <= m; i++) cin >> b[i][0]; for (int j = 1; j < lg; j++) { for (int i = 1; i + (1 << j) - 1 <= n; i++) { a[i][j] = max(a[i][j - 1], a[i + (1 << (j - 1))][j - 1]); } } for (int j = 1; j < lg; j++) { for (int i = 1; i + (1 << j) - 1 <= m; i++) { b[i][j] = max(b[i][j - 1], b[i + (1 << (j - 1))][j - 1]); } } while (q--) { int x, y; cin >> x >> y; cout << max(solve(x, y, 0), solve(x, y, 1)) << "\n"; } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); testCase(); return 0; }
#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...