제출 #698976

#제출 시각아이디문제언어결과실행 시간메모리
698976tengiz05유괴 2 (JOI17_abduction2)C++17
13 / 100
5090 ms113092 KiB
#include <bits/stdc++.h>

using i64 = long long;

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int n, m, q;
    std::cin >> n >> m >> q;
    
    std::vector<int> a(n), b(m);
    for (int i = 0; i < n; i++) {
        std::cin >> a[i];
    }
    for (int i = 0; i < m; i++) {
        std::cin >> b[i];
    }
    
    while (q--) {
        int s, t;
        std::cin >> s >> t;
        s--;
        t--;
        int ans = 0;
        std::queue<std::array<int, 4>> q;
        q.push({0, s, t, 0});
        q.push({0, s, t, 1});
        while (!q.empty()) {
            auto [dis, x, y, type] = q.front();
            q.pop();
            ans = std::max(ans, dis);
            //~ std::cout << dis << ": " << x << " " << y << "\n";
            if (type == 0) {
                int i = x + 1;
                while (i + 1 < n && a[i] <= b[y]) i++;
                if (i < n && a[i] > b[y]) q.push({dis + i - x, i, y, 1});
                else if (i == n - 1) ans = std::max(ans, dis + i - x);
                i = x - 1;
                while (i > 0 && a[i] <= b[y]) i--;
                if (i >= 0 && a[i] > b[y]) q.push({dis + x - i, i, y, 1});
                else if (i == 0) ans = std::max(ans, dis + x);
            } else {
                int j = y + 1;
                while (j + 1 < m && b[j] <= a[x]) j++;
                if (j < m && b[j] > a[x]) q.push({dis + j - y, x, j, 0});
                else if (j == m - 1) ans = std::max(ans, dis + j - y);
                j = y - 1;
                while (j > 0 && b[j] <= a[x]) j--;
                if (j >= 0 && b[j] > a[x]) q.push({dis + y - j, x, j, 0});
                else if (j == 0) ans = std::max(ans, dis + y);
            }
        }
        std::cout << ans << "\n";
    }
    
    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...