답안 #210143

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
210143 2020-03-16T16:03:05 Z edsa 유괴 2 (JOI17_abduction2) C++14
100 / 100
374 ms 9208 KB
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
using vii = vector<ii>;
const ll MOD = 998244353;
const int INF = 1e9+9;
const int MAXN = 50004;


enum direction{right = 0, up = 1, left = 2, down = 3};
int n, m, a[MAXN], b[MAXN];

int sparseA[MAXN][20], sparseB[MAXN][20];
void sparseTable() {
    for (int i = 0; i < n; i++) {
        sparseA[i][0] = i;
    }
    for (int j = 1; j < 20; j++) {
        for (int i = 0; i+(1<<j) <= n; i++) {
            int &l = sparseA[i][j-1], &r = sparseA[i+(1<<(j-1))][j-1];
            sparseA[i][j] = a[l] > a[r]? l : r;
        }
    }
    for (int i = 0; i < m; i++) {
        sparseB[i][0] = i;
    }
    for (int j = 1; j < 20; j++) {
        for (int i = 0; i+(1<<j) <= m; i++) {
            int &l = sparseB[i][j-1], &r = sparseB[i+(1<<(j-1))][j-1];
            sparseB[i][j] = b[l] > b[r]? l : r;
        }
    }
}

inline int maxa(int l, int r) {
    int logLen = 0;
    for (int i = r-l; i > 1; i>>=1) logLen++;
    int &L = sparseA[l][logLen], &R = sparseA[r-(1<<logLen)][logLen];
    return a[L] > a[R]? L : R;
}

inline int maxb(int l, int r) {
    int logLen = 0;
    for (int i = r-l; i > 1; i>>=1) logLen++;
    int &L = sparseB[l][logLen], &R = sparseB[r-(1<<logLen)][logLen];
    return b[L] > b[R]? L : R;
}

ll mid[4], midVal[4];
inline ll dist(direction dir, int id) {
    return midVal[dir] == -1? 0 : abs(2*id-mid[dir])/2+midVal[dir];
}

ll solve(int s, int t, direction dir) {
    if (s == -1 || s == n || t == -1 || t == m)
        return 0;
    
    using direction::right;
    using direction::left;
    int l = 0, r = m, d = 0, u = n;
    mid[0] = mid[1] = mid[2] = mid[3] = 0;
    midVal[0] = midVal[1] = midVal[2] = midVal[3] = -1;
    // cerr << "solve " << s << " " << t << endl;
    while (true) {
        // cerr << "it [" << d << ", " << u << "]x[" << l << ", " << r << "]" << endl;
        // for (int i = 0; i < 4; i++) {
        //    cerr << mid[i] << " " << midVal[i] << endl;
        // }
        int maxh = maxa(d, u);
        int maxv = maxb(l, r);

        if (b[maxv] > a[maxh]) {
            ll downVal = dist(down, maxv);
            ll upVal   = dist(up, maxv);
            // cerr << "downVal " << downVal << " upVal " << upVal << endl;
            if (maxv <= t) {
                l = maxv+1;
                mid[left] = d-downVal+u-1+upVal;
                midVal[left] = (-d+downVal+u+upVal+2)/2;
            } else {
                r = maxv;
                mid[right] = d-downVal+u-1+upVal;
                midVal[right] = (-d+downVal+u+upVal+2)/2;
            }

            if (maxv == t) {
                if (dir == down) {
                    return upVal+u-s;
                } else if (dir == up) {
                    return downVal+s-d+1;
                } else {
                    assert(max(upVal+u-s, downVal+s-d+1) == dist(left, s));
                    return dist(left, s);
                }
            }
        } else {
            ll leftVal = dist(left, maxh);
            ll rightVal = dist(right, maxh);
            // cerr << "leftVal " << leftVal << " rightVal " << rightVal << endl;
            if (maxh <= s) {
                d = maxh+1;
                mid[down] = l-leftVal+r-1+rightVal;
                midVal[down] = (-l+leftVal+r+rightVal+2)/2;
            } else {
                u = maxh;
                mid[up] = l-leftVal+r-1+rightVal;
                midVal[up] = (-l+leftVal+r+rightVal+2)/2;
            }
            if (maxh == s) {
                if (dir == left) {
                    return rightVal+r-t;
                } else if (dir == right) {
                    return leftVal+t-l+1;
                } else {
                    assert(max(rightVal+r-t, leftVal+t-l+1) == dist(down, t));
                    return dist(down, t);
                }
            }
        }
    }
}

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int q, s, t;
    ll ans;
    cin >> n >> m >> q;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (int i = 0; i < m; i++) {
        cin >> b[i];
    }
    sparseTable();

    while (q--) {
        cin >> s >> t;
        s--, t--;
        using direction::right;
        using direction::left;
        if (a[s] < b[t]) {
            ans = max(solve(s, t-1, right), solve(s, t+1, left));
            ans = max(solve(s, t, right)-1, ans);
        } else {
            ans = max(solve(s-1, t, up), solve(s+1, t, down));
            ans = max(solve(s, t, up)-1, ans);
        }
        cout << ans << '\n';
        // break;
    }
}

# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 504 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 504 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
12 Correct 6 ms 760 KB Output is correct
13 Correct 5 ms 760 KB Output is correct
14 Correct 5 ms 760 KB Output is correct
15 Correct 5 ms 760 KB Output is correct
16 Correct 6 ms 760 KB Output is correct
17 Correct 6 ms 760 KB Output is correct
18 Correct 6 ms 764 KB Output is correct
19 Correct 5 ms 760 KB Output is correct
20 Correct 6 ms 764 KB Output is correct
21 Correct 6 ms 760 KB Output is correct
22 Correct 5 ms 760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 504 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
12 Correct 6 ms 760 KB Output is correct
13 Correct 5 ms 760 KB Output is correct
14 Correct 5 ms 760 KB Output is correct
15 Correct 5 ms 760 KB Output is correct
16 Correct 6 ms 760 KB Output is correct
17 Correct 6 ms 760 KB Output is correct
18 Correct 6 ms 764 KB Output is correct
19 Correct 5 ms 760 KB Output is correct
20 Correct 6 ms 764 KB Output is correct
21 Correct 6 ms 760 KB Output is correct
22 Correct 5 ms 760 KB Output is correct
23 Correct 25 ms 9080 KB Output is correct
24 Correct 27 ms 9080 KB Output is correct
25 Correct 26 ms 9084 KB Output is correct
26 Correct 27 ms 9080 KB Output is correct
27 Correct 26 ms 9080 KB Output is correct
28 Correct 34 ms 9080 KB Output is correct
29 Correct 26 ms 9080 KB Output is correct
30 Correct 31 ms 9080 KB Output is correct
31 Correct 33 ms 9080 KB Output is correct
32 Correct 25 ms 8952 KB Output is correct
33 Correct 26 ms 9080 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 760 KB Output is correct
2 Correct 8 ms 760 KB Output is correct
3 Correct 6 ms 760 KB Output is correct
4 Correct 6 ms 760 KB Output is correct
5 Correct 6 ms 760 KB Output is correct
6 Correct 17 ms 760 KB Output is correct
7 Correct 16 ms 760 KB Output is correct
8 Correct 15 ms 760 KB Output is correct
9 Correct 17 ms 776 KB Output is correct
10 Correct 18 ms 760 KB Output is correct
11 Correct 15 ms 760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 504 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
12 Correct 6 ms 760 KB Output is correct
13 Correct 5 ms 760 KB Output is correct
14 Correct 5 ms 760 KB Output is correct
15 Correct 5 ms 760 KB Output is correct
16 Correct 6 ms 760 KB Output is correct
17 Correct 6 ms 760 KB Output is correct
18 Correct 6 ms 764 KB Output is correct
19 Correct 5 ms 760 KB Output is correct
20 Correct 6 ms 764 KB Output is correct
21 Correct 6 ms 760 KB Output is correct
22 Correct 5 ms 760 KB Output is correct
23 Correct 25 ms 9080 KB Output is correct
24 Correct 27 ms 9080 KB Output is correct
25 Correct 26 ms 9084 KB Output is correct
26 Correct 27 ms 9080 KB Output is correct
27 Correct 26 ms 9080 KB Output is correct
28 Correct 34 ms 9080 KB Output is correct
29 Correct 26 ms 9080 KB Output is correct
30 Correct 31 ms 9080 KB Output is correct
31 Correct 33 ms 9080 KB Output is correct
32 Correct 25 ms 8952 KB Output is correct
33 Correct 26 ms 9080 KB Output is correct
34 Correct 6 ms 760 KB Output is correct
35 Correct 8 ms 760 KB Output is correct
36 Correct 6 ms 760 KB Output is correct
37 Correct 6 ms 760 KB Output is correct
38 Correct 6 ms 760 KB Output is correct
39 Correct 17 ms 760 KB Output is correct
40 Correct 16 ms 760 KB Output is correct
41 Correct 15 ms 760 KB Output is correct
42 Correct 17 ms 776 KB Output is correct
43 Correct 18 ms 760 KB Output is correct
44 Correct 15 ms 760 KB Output is correct
45 Correct 27 ms 9080 KB Output is correct
46 Correct 26 ms 9080 KB Output is correct
47 Correct 26 ms 9080 KB Output is correct
48 Correct 28 ms 9080 KB Output is correct
49 Correct 27 ms 9080 KB Output is correct
50 Correct 362 ms 9080 KB Output is correct
51 Correct 315 ms 9080 KB Output is correct
52 Correct 374 ms 9080 KB Output is correct
53 Correct 374 ms 8952 KB Output is correct
54 Correct 350 ms 8952 KB Output is correct
55 Correct 289 ms 9080 KB Output is correct
56 Correct 81 ms 9208 KB Output is correct
57 Correct 45 ms 9208 KB Output is correct
58 Correct 43 ms 9208 KB Output is correct
59 Correct 44 ms 9208 KB Output is correct