답안 #241189

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
241189 2020-06-23T08:53:53 Z osaaateiasavtnl 유괴 2 (JOI17_abduction2) C++17
100 / 100
1721 ms 273852 KB
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC

const int N = 1e5+7;

int a[N], b[N];

struct State {
    int x, y, t;
    int val() {
        if (t&1)
            return a[x];
        else
            return b[y];
    };
    bool operator <(State s) {
        return val() < s.val();
    }
};  

struct SegTree {

int mx[N << 2];
void build(int v, int tl, int tr, int a[N]) {
    if (tl == tr) {
        mx[v] = a[tl];
        return;
    }   
    int tm = (tl + tr) >> 1;
    build(v * 2 + 1, tl, tm, a);
    build(v * 2 + 2, tm + 1, tr, a);
    mx[v] = max(mx[v * 2 + 1], mx[v * 2 + 2]);
}   
int getl(int v, int tl, int tr, int l, int r, int x) {
    if (mx[v] <= x || tr < l || r < tl)
        return -1;
    if (tl == tr)
        return tl;
    int tm = (tl + tr) >> 1;
    int t = getl(v * 2 + 1, tl, tm, l, r, x);
    if (t != -1)
        return t;
    return getl(v * 2 + 2, tm + 1, tr, l, r, x);
}   
int getr(int v, int tl, int tr, int l, int r, int x) {
    if (mx[v] <= x || tr < l || r < tl)
        return -1;
    if (tl == tr)
        return tl;
    int tm = (tl + tr) >> 1;
    int t = getr(v * 2 + 2, tm + 1, tr, l, r, x);
    if (t != -1)    
        return t;
    return getr(v * 2 + 1, tl, tm, l, r, x);
}   

} dx, dy;  

int n, m, q;
map <int, int> dp[4][N];
int get(int x, int y, int t) {
    if (x < 0 || x >= n || y < 0 || y >= m) 
        return -1;

    if (dp[t][x].find(y) != dp[t][x].end())
        return dp[t][x][y];

    int &d = dp[t][x][y];
    d = 0;

    if (t&1) {
        if (t == 1) {
            int to = dy.getl(0, 0, m - 1, y, m - 1, a[x]);
            if (to == -1) {
                d = m - 1 - y;
            }
            else {
                d = max(d, get(x, to, 0));
                d = max(d, get(x, to, 2));
                d += to - y;
            }   
        }
        else {
            int to = dy.getr(0, 0, m - 1, 0, y, a[x]);
            if (to == -1) {
                d = y;
            }   
            else {
                d = max(d, get(x, to, 0));
                d = max(d, get(x, to, 2));
                d += y - to;
            }   
        }
    }   
    else {
        if (t == 2) {
            int to = dx.getl(0, 0, n - 1, x, n - 1, b[y]);
            if (to == -1) {
                d = n - 1 - x;
            }
            else {
                d = max(d, get(to, y, 1));
                d = max(d, get(to, y, 3));
                d += abs(x-to);
            }   
        }   
        else {
            int to = dx.getr(0, 0, n - 1, 0, x, b[y]);
            if (to == -1) {
                d = x;
            }   
            else {
                d = max(d, get(to, y, 1));
                d = max(d, get(to, y, 3));
                d += abs(x-to);
            }   
        }   
    }

    return d;
}   

signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif

    cin >> n >> m >> q;
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    for (int i = 0; i < m; ++i)
        cin >> b[i];
    
    dx.build(0, 0, n - 1, a);
    dy.build(0, 0, m - 1, b);

    while (q--) {
        int x, y;
        cin >> x >> y;
        --x; --y;
        int ans = 0;
        ans = max(ans, get(x-1,y,0)+1);
        ans = max(ans, get(x+1,y,2)+1);
        ans = max(ans, get(x,y-1,3)+1);
        ans = max(ans, get(x,y+1,1)+1);
        cout << ans << endl;
    }   
}
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 19072 KB Output is correct
2 Correct 16 ms 19328 KB Output is correct
3 Correct 15 ms 19072 KB Output is correct
4 Correct 19 ms 19200 KB Output is correct
5 Correct 15 ms 19328 KB Output is correct
6 Correct 18 ms 19072 KB Output is correct
7 Correct 16 ms 19200 KB Output is correct
8 Correct 15 ms 19072 KB Output is correct
9 Correct 15 ms 19200 KB Output is correct
10 Correct 18 ms 19200 KB Output is correct
11 Correct 18 ms 19072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 19072 KB Output is correct
2 Correct 16 ms 19328 KB Output is correct
3 Correct 15 ms 19072 KB Output is correct
4 Correct 19 ms 19200 KB Output is correct
5 Correct 15 ms 19328 KB Output is correct
6 Correct 18 ms 19072 KB Output is correct
7 Correct 16 ms 19200 KB Output is correct
8 Correct 15 ms 19072 KB Output is correct
9 Correct 15 ms 19200 KB Output is correct
10 Correct 18 ms 19200 KB Output is correct
11 Correct 18 ms 19072 KB Output is correct
12 Correct 18 ms 19200 KB Output is correct
13 Correct 17 ms 19200 KB Output is correct
14 Correct 18 ms 19200 KB Output is correct
15 Correct 17 ms 19328 KB Output is correct
16 Correct 16 ms 19200 KB Output is correct
17 Correct 17 ms 19200 KB Output is correct
18 Correct 16 ms 19328 KB Output is correct
19 Correct 17 ms 19712 KB Output is correct
20 Correct 18 ms 19968 KB Output is correct
21 Correct 18 ms 19712 KB Output is correct
22 Correct 21 ms 20352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 19072 KB Output is correct
2 Correct 16 ms 19328 KB Output is correct
3 Correct 15 ms 19072 KB Output is correct
4 Correct 19 ms 19200 KB Output is correct
5 Correct 15 ms 19328 KB Output is correct
6 Correct 18 ms 19072 KB Output is correct
7 Correct 16 ms 19200 KB Output is correct
8 Correct 15 ms 19072 KB Output is correct
9 Correct 15 ms 19200 KB Output is correct
10 Correct 18 ms 19200 KB Output is correct
11 Correct 18 ms 19072 KB Output is correct
12 Correct 18 ms 19200 KB Output is correct
13 Correct 17 ms 19200 KB Output is correct
14 Correct 18 ms 19200 KB Output is correct
15 Correct 17 ms 19328 KB Output is correct
16 Correct 16 ms 19200 KB Output is correct
17 Correct 17 ms 19200 KB Output is correct
18 Correct 16 ms 19328 KB Output is correct
19 Correct 17 ms 19712 KB Output is correct
20 Correct 18 ms 19968 KB Output is correct
21 Correct 18 ms 19712 KB Output is correct
22 Correct 21 ms 20352 KB Output is correct
23 Correct 31 ms 22008 KB Output is correct
24 Correct 33 ms 22008 KB Output is correct
25 Correct 34 ms 22008 KB Output is correct
26 Correct 30 ms 22016 KB Output is correct
27 Correct 31 ms 22016 KB Output is correct
28 Correct 54 ms 34296 KB Output is correct
29 Correct 33 ms 23808 KB Output is correct
30 Correct 81 ms 35576 KB Output is correct
31 Correct 98 ms 39472 KB Output is correct
32 Correct 33 ms 22784 KB Output is correct
33 Correct 48 ms 26232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 19584 KB Output is correct
2 Correct 18 ms 19584 KB Output is correct
3 Correct 21 ms 19584 KB Output is correct
4 Correct 19 ms 19584 KB Output is correct
5 Correct 19 ms 19712 KB Output is correct
6 Correct 19 ms 19840 KB Output is correct
7 Correct 18 ms 19712 KB Output is correct
8 Correct 20 ms 20224 KB Output is correct
9 Correct 20 ms 20224 KB Output is correct
10 Correct 20 ms 20096 KB Output is correct
11 Correct 21 ms 20352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 19072 KB Output is correct
2 Correct 16 ms 19328 KB Output is correct
3 Correct 15 ms 19072 KB Output is correct
4 Correct 19 ms 19200 KB Output is correct
5 Correct 15 ms 19328 KB Output is correct
6 Correct 18 ms 19072 KB Output is correct
7 Correct 16 ms 19200 KB Output is correct
8 Correct 15 ms 19072 KB Output is correct
9 Correct 15 ms 19200 KB Output is correct
10 Correct 18 ms 19200 KB Output is correct
11 Correct 18 ms 19072 KB Output is correct
12 Correct 18 ms 19200 KB Output is correct
13 Correct 17 ms 19200 KB Output is correct
14 Correct 18 ms 19200 KB Output is correct
15 Correct 17 ms 19328 KB Output is correct
16 Correct 16 ms 19200 KB Output is correct
17 Correct 17 ms 19200 KB Output is correct
18 Correct 16 ms 19328 KB Output is correct
19 Correct 17 ms 19712 KB Output is correct
20 Correct 18 ms 19968 KB Output is correct
21 Correct 18 ms 19712 KB Output is correct
22 Correct 21 ms 20352 KB Output is correct
23 Correct 31 ms 22008 KB Output is correct
24 Correct 33 ms 22008 KB Output is correct
25 Correct 34 ms 22008 KB Output is correct
26 Correct 30 ms 22016 KB Output is correct
27 Correct 31 ms 22016 KB Output is correct
28 Correct 54 ms 34296 KB Output is correct
29 Correct 33 ms 23808 KB Output is correct
30 Correct 81 ms 35576 KB Output is correct
31 Correct 98 ms 39472 KB Output is correct
32 Correct 33 ms 22784 KB Output is correct
33 Correct 48 ms 26232 KB Output is correct
34 Correct 20 ms 19584 KB Output is correct
35 Correct 18 ms 19584 KB Output is correct
36 Correct 21 ms 19584 KB Output is correct
37 Correct 19 ms 19584 KB Output is correct
38 Correct 19 ms 19712 KB Output is correct
39 Correct 19 ms 19840 KB Output is correct
40 Correct 18 ms 19712 KB Output is correct
41 Correct 20 ms 20224 KB Output is correct
42 Correct 20 ms 20224 KB Output is correct
43 Correct 20 ms 20096 KB Output is correct
44 Correct 21 ms 20352 KB Output is correct
45 Correct 33 ms 22520 KB Output is correct
46 Correct 33 ms 22528 KB Output is correct
47 Correct 37 ms 22528 KB Output is correct
48 Correct 40 ms 22528 KB Output is correct
49 Correct 33 ms 22524 KB Output is correct
50 Correct 57 ms 32376 KB Output is correct
51 Correct 53 ms 33784 KB Output is correct
52 Correct 132 ms 43256 KB Output is correct
53 Correct 132 ms 42488 KB Output is correct
54 Correct 121 ms 41724 KB Output is correct
55 Correct 161 ms 52472 KB Output is correct
56 Correct 1721 ms 273852 KB Output is correct
57 Correct 434 ms 88312 KB Output is correct
58 Correct 390 ms 84984 KB Output is correct
59 Correct 362 ms 84472 KB Output is correct