답안 #308412

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
308412 2020-10-01T05:50:59 Z thecodingwizard 유괴 2 (JOI17_abduction2) C++11
100 / 100
3837 ms 257628 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define ii pair<int, int>
#define f first
#define s second
#define mp make_pair

int h, w, q; 
int A[50000], B[50000];
int stH[20][50000];
int stW[20][50000];

int rmq(const int st[20][50000], int L, int R) {
    int maxLen = 31 - __builtin_clz(R-L+1);
    return max(st[maxLen][L], st[maxLen][R - (1 << maxLen)+1]);
}

map<pair<int, ii>, ll> memo;
int dx[4] = {-1,0,1,0};
int dy[4] = {0,1,0,-1};
ll dp(int i, int j, int d) {
    auto memoIdx = mp(i, mp(j, d));
    if (memo.count(memoIdx)) {
        return memo[memoIdx];
    }

    int maxTravel;
    if (d == 0) maxTravel = i;
    else if (d == 1) maxTravel = w-j-1;
    else if (d == 2) maxTravel = h-i-1;
    else if (d == 3) maxTravel = j;

    ll ans = 0;
    int lo = 1, hi = maxTravel+1, mid, k = 0;
    while (lo < hi) {
        mid = (lo + hi)/2;
        int a = i + dx[d]*mid, b = j + dy[d]*mid;
        if (a < 0 || a >= h || b < 0 || b >= w) {
            hi = mid;
            continue;
        }
        if (d == 0 || d == 2) {
            if (rmq(stH, min(i+dx[d], a), max(i+dx[d], a)) > B[j]) {
                k = mid;
                hi = mid;
            } else {
                lo = mid + 1;
            }
        }
        if (d == 1 || d == 3) {
            if (rmq(stW, min(j+dy[d], b), max(j+dy[d], b)) > A[i]) {
                k = mid;
                hi = mid;
            } else {
                lo = mid + 1;
            }
        }
    }

    if (k > 0) {
        ans += k;
        int a = i + dx[d]*k, b = j + dy[d]*k;
        if (d == 0 || d == 2) {
            ans += max(dp(a, b, 1), dp(a, b, 3));
        }
        if (d == 1 || d == 3) {
            ans += max(dp(a, b, 0), dp(a, b, 2));
        }
    } else {
        ans += maxTravel;
    }

    return memo[memoIdx] = ans;
}

int main() {
    cin.tie(0); ios_base::sync_with_stdio(false);
    cin >> h >> w >> q;
    for (int i = 0; i < h; i++) cin >> A[i];
    for (int i = 0; i < w; i++) cin >> B[i];

    for (int i = 0; i < h; i++) {
        stH[0][i] = A[i];
    }
    for (int len = 1; len < 20; len++) {
        for (int i = 0; i <= h - (1 << len); i++) {
            stH[len][i] = max(stH[len-1][i], stH[len-1][i + (1<<(len-1))]);
        }
    }
    for (int i = 0; i < w; i++) {
        stW[0][i] = B[i];
    }
    for (int len = 1; len < 20; len++) {
        for (int i = 0; i <= w - (1 << len); i++) {
            stW[len][i] = max(stW[len-1][i], stW[len-1][i + (1<<(len-1))]);
        }
    }

    for (int i = 0; i < q; i++) {
        int s, t; cin >> s >> t; --s; --t;
        ll best = 0;
        for (int j = 0; j < 4; j++) {
            best = max(best, dp(s, t, j));
        }
        cout << best << "\n";
    }

    return 0;
}

Compilation message

abduction2.cpp: In function 'll dp(int, int, int)':
abduction2.cpp:30:9: warning: 'maxTravel' may be used uninitialized in this function [-Wmaybe-uninitialized]
   30 |     int maxTravel;
      |         ^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 416 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 416 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 2 ms 640 KB Output is correct
13 Correct 1 ms 640 KB Output is correct
14 Correct 1 ms 640 KB Output is correct
15 Correct 1 ms 640 KB Output is correct
16 Correct 1 ms 640 KB Output is correct
17 Correct 1 ms 640 KB Output is correct
18 Correct 2 ms 640 KB Output is correct
19 Correct 4 ms 1024 KB Output is correct
20 Correct 6 ms 1152 KB Output is correct
21 Correct 5 ms 1024 KB Output is correct
22 Correct 9 ms 1536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 416 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 2 ms 640 KB Output is correct
13 Correct 1 ms 640 KB Output is correct
14 Correct 1 ms 640 KB Output is correct
15 Correct 1 ms 640 KB Output is correct
16 Correct 1 ms 640 KB Output is correct
17 Correct 1 ms 640 KB Output is correct
18 Correct 2 ms 640 KB Output is correct
19 Correct 4 ms 1024 KB Output is correct
20 Correct 6 ms 1152 KB Output is correct
21 Correct 5 ms 1024 KB Output is correct
22 Correct 9 ms 1536 KB Output is correct
23 Correct 16 ms 6528 KB Output is correct
24 Correct 16 ms 6528 KB Output is correct
25 Correct 17 ms 6528 KB Output is correct
26 Correct 16 ms 6528 KB Output is correct
27 Correct 16 ms 6528 KB Output is correct
28 Correct 50 ms 14204 KB Output is correct
29 Correct 20 ms 7676 KB Output is correct
30 Correct 133 ms 17784 KB Output is correct
31 Correct 168 ms 21112 KB Output is correct
32 Correct 24 ms 7168 KB Output is correct
33 Correct 61 ms 10104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 1024 KB Output is correct
2 Correct 5 ms 1024 KB Output is correct
3 Correct 5 ms 1024 KB Output is correct
4 Correct 5 ms 1024 KB Output is correct
5 Correct 6 ms 896 KB Output is correct
6 Correct 3 ms 1024 KB Output is correct
7 Correct 4 ms 1024 KB Output is correct
8 Correct 9 ms 1536 KB Output is correct
9 Correct 10 ms 1536 KB Output is correct
10 Correct 9 ms 1408 KB Output is correct
11 Correct 12 ms 1664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 416 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 2 ms 640 KB Output is correct
13 Correct 1 ms 640 KB Output is correct
14 Correct 1 ms 640 KB Output is correct
15 Correct 1 ms 640 KB Output is correct
16 Correct 1 ms 640 KB Output is correct
17 Correct 1 ms 640 KB Output is correct
18 Correct 2 ms 640 KB Output is correct
19 Correct 4 ms 1024 KB Output is correct
20 Correct 6 ms 1152 KB Output is correct
21 Correct 5 ms 1024 KB Output is correct
22 Correct 9 ms 1536 KB Output is correct
23 Correct 16 ms 6528 KB Output is correct
24 Correct 16 ms 6528 KB Output is correct
25 Correct 17 ms 6528 KB Output is correct
26 Correct 16 ms 6528 KB Output is correct
27 Correct 16 ms 6528 KB Output is correct
28 Correct 50 ms 14204 KB Output is correct
29 Correct 20 ms 7676 KB Output is correct
30 Correct 133 ms 17784 KB Output is correct
31 Correct 168 ms 21112 KB Output is correct
32 Correct 24 ms 7168 KB Output is correct
33 Correct 61 ms 10104 KB Output is correct
34 Correct 5 ms 1024 KB Output is correct
35 Correct 5 ms 1024 KB Output is correct
36 Correct 5 ms 1024 KB Output is correct
37 Correct 5 ms 1024 KB Output is correct
38 Correct 6 ms 896 KB Output is correct
39 Correct 3 ms 1024 KB Output is correct
40 Correct 4 ms 1024 KB Output is correct
41 Correct 9 ms 1536 KB Output is correct
42 Correct 10 ms 1536 KB Output is correct
43 Correct 9 ms 1408 KB Output is correct
44 Correct 12 ms 1664 KB Output is correct
45 Correct 23 ms 7040 KB Output is correct
46 Correct 23 ms 7168 KB Output is correct
47 Correct 23 ms 7168 KB Output is correct
48 Correct 23 ms 7032 KB Output is correct
49 Correct 23 ms 7048 KB Output is correct
50 Correct 58 ms 14200 KB Output is correct
51 Correct 63 ms 15096 KB Output is correct
52 Correct 242 ms 26232 KB Output is correct
53 Correct 231 ms 25592 KB Output is correct
54 Correct 218 ms 24440 KB Output is correct
55 Correct 418 ms 34040 KB Output is correct
56 Correct 3837 ms 257628 KB Output is correct
57 Correct 911 ms 72100 KB Output is correct
58 Correct 871 ms 68920 KB Output is correct
59 Correct 891 ms 68560 KB Output is correct