답안 #883880

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
883880 2023-12-06T10:48:44 Z DAleksa 유괴 2 (JOI17_abduction2) C++17
44 / 100
209 ms 23380 KB
#include <bits/stdc++.h>
 
using namespace std;

const int N = 5e4 + 10, LOG = 16;
int n, m, q;
int a[N], b[N];
int sta[N][LOG], stb[N][LOG];
int L[N], R[N], U[N], D[N];
map<pair<int, int>, int> dp;

int geta(int l, int r) {
    int len = r - l + 1;
    int bit = 31 - __builtin_clz(len);
    return max(sta[l][bit], sta[r - (1 << bit) + 1][bit]);
}

int getb(int l, int r) {
    int len = r - l + 1;
    int bit = 31 - __builtin_clz(len);
    return max(stb[l][bit], stb[r - (1 << bit) + 1][bit]);
}
    
int findL(int x, int y) {
    int l = 0, r = y - 1;
    int ans = l;
    while(l <= r) {
        int mid = (l + r) / 2;
        // if(x == 2 && y == 2) cout << "mid " << mid << " " << getb(mid, y - 1) << "\n";
        if(getb(mid, y - 1) > a[x]) {
            ans = mid;
            l = mid + 1;
        } else r = mid - 1;
    }
    return ans;
}

int findR(int x, int y) {
    int l = y + 1, r = m + 1;
    int ans = r;
    while(l <= r) {
        int mid = (l + r) / 2;
        if(getb(y + 1, mid) > a[x]) {
            ans = mid;
            r = mid - 1;
        } else l = mid + 1;
    }
    return ans;
}

int findU(int x, int y) {
    int l = 0, r = x - 1;
    int ans = l;
    while(l <= r) {
        int mid = (l + r) / 2;
        if(geta(mid, x - 1) > b[y]) {
            ans = mid;
            l = mid + 1;
        } else r = mid - 1;
    }
    return ans;
}

int findD(int x, int y) {
    int l = x + 1, r = n + 1;
    int ans = r;
    while(l <= r) {
        int mid = (l + r) / 2;
        if(geta(x + 1, mid) > b[y]) {
            ans = mid;
            r = mid - 1;
        } else l = mid + 1;
    }
    return ans;
}

int add(int x, int y) {
    if(x == 0 || x == n + 1 || y == 0 || y == m + 1) return 0;
    if(dp[{x, y}] != 0) return dp[{x, y}];
    // cout << "add " << x << " " << y << "\n";
    if(a[x] < b[y]) {
        int U = findU(x, y);
        int D = findD(x, y);
        dp[{x, y}] = max(dp[{x, y}], add(U, y) + x - U);
        dp[{x, y}] = max(dp[{x, y}], add(D, y) + D - x);
    } else {
        int L = findL(x, y);
        int R = findR(x, y);
        dp[{x, y}] = max(dp[{x, y}], add(x, L) + y - L);
        dp[{x, y}] = max(dp[{x, y}], add(x, R) + R - y);
    }
    if(dp[{x, y}] == 0) dp[{x, y}] = 1;
    // cout << "end " << x << " " << y << " " << dp[{x, y}] << "\n";
    return dp[{x, y}];
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m >> q;
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
        sta[i][0] = a[i];
    }
    for(int i = 1; i <= m; i++) {
        cin >> b[i];
        stb[i][0] = b[i];
    }
    for(int j = 1; j < LOG; j++) {
        for(int i = 0; i + (1 << (j - 1)) <= n; i++) {
            sta[i][j] = max(sta[i][j - 1], sta[i + (1 << (j - 1))][j - 1]);
        }
    }
    for(int j = 1; j < LOG; j++) {
        for(int i = 0; i + (1 << (j - 1)) <= m; i++) {
            stb[i][j] = max(stb[i][j - 1], stb[i + (1 << (j - 1))][j - 1]);
        }
    }
    while(q--) {
        int x, y;
        cin >> x >> y;
        int L = findL(x, y);
        int R = findR(x, y);
        int U = findU(x, y);
        int D = findD(x, y);
        // cout << L << " " << R << " " << U << " " << D << "\n";
        int ans = 1;
        if(L == 0) ans = max(ans, y);
        else {
            if(dp[{x, L}] == 0) dp[{x, L}] = add(x, L);
            ans = max(ans, dp[{x, L}] + y - L);
        }
        if(R == m + 1) ans = max(ans, m - y + 1);
        else {
            if(dp[{x, R}] == 0) dp[{x, R}] = add(x, R);
            ans = max(ans, dp[{x, R}] + R - y);
        }
        if(U == 0) ans = max(ans, x);
        else {
            if(dp[{U, y}] == 0) dp[{U, y}] = add(U, y);
            ans = max(ans, dp[{U, y}] + x - U);
        }
        if(L == 0) ans = max(ans, n - x + 1);
        else {
            if(dp[{D, y}] == 0) dp[{D, y}] = add(D, y);
            ans = max(ans, dp[{D, y}] + D - x);
        }
        // dp[{x, y}] = ans;
        cout << ans - 1 << "\n";
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4440 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4440 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4440 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4572 KB Output is correct
19 Correct 3 ms 4700 KB Output is correct
20 Correct 4 ms 4796 KB Output is correct
21 Correct 3 ms 4700 KB Output is correct
22 Correct 5 ms 4956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4440 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4440 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4572 KB Output is correct
19 Correct 3 ms 4700 KB Output is correct
20 Correct 4 ms 4796 KB Output is correct
21 Correct 3 ms 4700 KB Output is correct
22 Correct 5 ms 4956 KB Output is correct
23 Correct 11 ms 8540 KB Output is correct
24 Correct 12 ms 8540 KB Output is correct
25 Correct 12 ms 8540 KB Output is correct
26 Correct 12 ms 8540 KB Output is correct
27 Correct 12 ms 8540 KB Output is correct
28 Correct 37 ms 14172 KB Output is correct
29 Correct 14 ms 9308 KB Output is correct
30 Correct 92 ms 14940 KB Output is correct
31 Correct 111 ms 16840 KB Output is correct
32 Correct 15 ms 8792 KB Output is correct
33 Correct 30 ms 10596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4856 KB Output is correct
2 Correct 3 ms 4700 KB Output is correct
3 Correct 4 ms 4700 KB Output is correct
4 Correct 4 ms 4700 KB Output is correct
5 Correct 3 ms 4700 KB Output is correct
6 Correct 2 ms 4700 KB Output is correct
7 Correct 3 ms 4700 KB Output is correct
8 Correct 7 ms 4840 KB Output is correct
9 Correct 6 ms 5208 KB Output is correct
10 Correct 6 ms 4952 KB Output is correct
11 Correct 6 ms 5088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4440 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4440 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4572 KB Output is correct
19 Correct 3 ms 4700 KB Output is correct
20 Correct 4 ms 4796 KB Output is correct
21 Correct 3 ms 4700 KB Output is correct
22 Correct 5 ms 4956 KB Output is correct
23 Correct 11 ms 8540 KB Output is correct
24 Correct 12 ms 8540 KB Output is correct
25 Correct 12 ms 8540 KB Output is correct
26 Correct 12 ms 8540 KB Output is correct
27 Correct 12 ms 8540 KB Output is correct
28 Correct 37 ms 14172 KB Output is correct
29 Correct 14 ms 9308 KB Output is correct
30 Correct 92 ms 14940 KB Output is correct
31 Correct 111 ms 16840 KB Output is correct
32 Correct 15 ms 8792 KB Output is correct
33 Correct 30 ms 10596 KB Output is correct
34 Correct 4 ms 4856 KB Output is correct
35 Correct 3 ms 4700 KB Output is correct
36 Correct 4 ms 4700 KB Output is correct
37 Correct 4 ms 4700 KB Output is correct
38 Correct 3 ms 4700 KB Output is correct
39 Correct 2 ms 4700 KB Output is correct
40 Correct 3 ms 4700 KB Output is correct
41 Correct 7 ms 4840 KB Output is correct
42 Correct 6 ms 5208 KB Output is correct
43 Correct 6 ms 4952 KB Output is correct
44 Correct 6 ms 5088 KB Output is correct
45 Correct 15 ms 8796 KB Output is correct
46 Correct 15 ms 8808 KB Output is correct
47 Correct 15 ms 8792 KB Output is correct
48 Correct 15 ms 8796 KB Output is correct
49 Correct 16 ms 8788 KB Output is correct
50 Correct 45 ms 13516 KB Output is correct
51 Correct 43 ms 14160 KB Output is correct
52 Correct 144 ms 18908 KB Output is correct
53 Correct 165 ms 18516 KB Output is correct
54 Correct 130 ms 18260 KB Output is correct
55 Incorrect 209 ms 23380 KB Output isn't correct
56 Halted 0 ms 0 KB -