Submission #551941

# Submission time Handle Problem Language Result Execution time Memory
551941 2022-04-21T23:12:40 Z Lobo Abduction 2 (JOI17_abduction2) C++17
100 / 100
1804 ms 301812 KB
#include<bits/stdc++.h>
using namespace std;

const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
// #define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()

const int maxn = 5e4+10;

int n, m, q, a[maxn], b[maxn];
map<int,long long> dp[maxn][5];
pair<int,int> mxalf[maxn][25], mxarg[maxn][25], mxblf[maxn][25], mxbrg[maxn][25];

//ver com 32 tambem
int mxblr(int l, int r) {
    if(l > r) return -1;
    int lg = 31-__builtin_clz(r-l+1);
    auto ans = max(mxbrg[l][lg],mxblf[r][lg]);
    return ans.fr;
}
int mxalr(int l, int r) {
    if(l > r) return -1;
    int lg = 31-__builtin_clz(r-l+1);
    auto ans = max(mxarg[l][lg],mxalf[r][lg]);
    return ans.fr;
}
    
//0 -> x aumenta, 1 -> x diminui e dobra para vertical
//2 -> y aumenta, 3 -> y diminui
//x -> M e y-> N
long long sol(int y, int x, int dir) {
    if(dp[y][dir].count(x)) return dp[y][dir][x];

    if(dir == 0) {
        //vou para um (x1,y) que b[x1] > a[y] e x1 > x
        if(mxblr(x+1,m) < a[y]) {
            return dp[y][dir][x] = m-x;
        }

        int l = x+1;
        int r = m;
        int ans = -1;

        while(l <= r) {
            int mid = (l+r)/2;

            if(mxblr(x+1,mid) >= a[y]) {
                ans = mid;
                r = mid-1;
            }
            else {
                l = mid+1;
            }
        }

        return dp[y][dir][x] = ans-x+max(sol(y,ans,2),sol(y,ans,3));
    }
    else if(dir == 1) {
        //vou para um (x1,y) que b[x1] > a[y] e x1 < x
        if(mxblr(1,x-1) < a[y]) {
            return dp[y][dir][x] = x-1;
        }

        int l = 1;
        int r = x-1;
        int ans = -1;

        while(l <= r) {
            int mid = (l+r)/2;

            if(mxblr(mid,x-1) >= a[y]) {
                ans = mid;
                l = mid+1;
            }
            else {
                r = mid-1;
            }
        }

        return dp[y][dir][x] = x-ans+max(sol(y,ans,2),sol(y,ans,3));
    }
    else if(dir == 2) {
        //vou para um (x,y1) que a[y1] > b[x] e y1 > y
        if(mxalr(y+1,n) < b[x]) {
            return dp[y][dir][x] = n-y;
        }

        int l = y+1;
        int r = n;
        int ans = -1;

        while(l <= r) {
            int mid = (l+r)/2;

            if(mxalr(y+1,mid) >= b[x]) {
                ans = mid;
                r = mid-1;
            }
            else {
                l = mid+1;
            }
        }

        return dp[y][dir][x] = ans-y+max(sol(ans,x,0),sol(ans,x,1));
    }
    else {
        //vou para um (x,y1) que a[y1] > b[x] e y1 < y
        if(mxalr(1,y-1) < b[x]) {
            return dp[y][dir][x] = y-1;
        }

        int l = 1;
        int r = y-1;
        int ans = -1;

        while(l <= r) {
            int mid = (l+r)/2;

            if(mxalr(mid,y-1) >= b[x]) {
                ans = mid;
                l = mid+1;
            }
            else {
                r = mid-1;
            }
        }

        return dp[y][dir][x] = y-ans+max(sol(ans,x,0),sol(ans,x,1));
    }
}



void solve() {
    cin >> n >> m >> q;

    for(int i = 1; i <= n; i++) {
        cin >> a[i];
        mxalf[i][0] = mxarg[i][0] = mp(a[i],i);
    }
    for(int i = 1; i <= m; i++) {
        cin >> b[i];
        mxblf[i][0] = mxbrg[i][0] = mp(b[i],i);
    }

    for(int pt = 1; pt <= 20; pt++) {
        for(int i = 1; i <= n; i++) {
            mxalf[i][pt] = max(mxalf[i][pt-1],mxalf[max(1,i-(1<<(pt-1)))][pt-1]);
        }
    }

    for(int pt = 1; pt <= 20; pt++) {
        for(int i = 1; i <= n; i++) {
            mxarg[i][pt] = max(mxarg[i][pt-1],mxarg[min(n,i+(1<<(pt-1)))][pt-1]);
        }
    }

    for(int pt = 1; pt <= 20; pt++) {
        for(int i = 1; i <= m; i++) {
            mxblf[i][pt] = max(mxblf[i][pt-1],mxblf[max(1,i-(1<<(pt-1)))][pt-1]);
        }
    }

    for(int pt = 1; pt <= 20; pt++) {
        for(int i = 1; i <= m; i++) {
            mxbrg[i][pt] = max(mxbrg[i][pt-1],mxbrg[min(m,i+(1<<(pt-1)))][pt-1]);
        }
    }

    while(q--) {
        int y,x;
        cin >> y >> x;
        cout << max({sol(y,x,0),sol(y,x,1),sol(y,x,2),sol(y,x,3)}) << endl;
    }

}

int32_t main() {
    ios::sync_with_stdio(false); cin.tie(0);

    // freopen("in.in", "r", stdin);
    // freopen("out.out", "w", stdout);
    
    int tt = 1;
    // cin >> tt;
    while(tt--) {
        solve();
    }

}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11988 KB Output is correct
2 Correct 8 ms 12044 KB Output is correct
3 Correct 6 ms 11988 KB Output is correct
4 Correct 7 ms 11988 KB Output is correct
5 Correct 6 ms 11988 KB Output is correct
6 Correct 6 ms 11988 KB Output is correct
7 Correct 9 ms 12116 KB Output is correct
8 Correct 6 ms 11988 KB Output is correct
9 Correct 6 ms 11988 KB Output is correct
10 Correct 6 ms 11988 KB Output is correct
11 Correct 6 ms 12072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11988 KB Output is correct
2 Correct 8 ms 12044 KB Output is correct
3 Correct 6 ms 11988 KB Output is correct
4 Correct 7 ms 11988 KB Output is correct
5 Correct 6 ms 11988 KB Output is correct
6 Correct 6 ms 11988 KB Output is correct
7 Correct 9 ms 12116 KB Output is correct
8 Correct 6 ms 11988 KB Output is correct
9 Correct 6 ms 11988 KB Output is correct
10 Correct 6 ms 11988 KB Output is correct
11 Correct 6 ms 12072 KB Output is correct
12 Correct 8 ms 13628 KB Output is correct
13 Correct 8 ms 13652 KB Output is correct
14 Correct 7 ms 13620 KB Output is correct
15 Correct 7 ms 13652 KB Output is correct
16 Correct 8 ms 13652 KB Output is correct
17 Correct 8 ms 13652 KB Output is correct
18 Correct 11 ms 13748 KB Output is correct
19 Correct 9 ms 14036 KB Output is correct
20 Correct 10 ms 14164 KB Output is correct
21 Correct 9 ms 14060 KB Output is correct
22 Correct 10 ms 14472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11988 KB Output is correct
2 Correct 8 ms 12044 KB Output is correct
3 Correct 6 ms 11988 KB Output is correct
4 Correct 7 ms 11988 KB Output is correct
5 Correct 6 ms 11988 KB Output is correct
6 Correct 6 ms 11988 KB Output is correct
7 Correct 9 ms 12116 KB Output is correct
8 Correct 6 ms 11988 KB Output is correct
9 Correct 6 ms 11988 KB Output is correct
10 Correct 6 ms 11988 KB Output is correct
11 Correct 6 ms 12072 KB Output is correct
12 Correct 8 ms 13628 KB Output is correct
13 Correct 8 ms 13652 KB Output is correct
14 Correct 7 ms 13620 KB Output is correct
15 Correct 7 ms 13652 KB Output is correct
16 Correct 8 ms 13652 KB Output is correct
17 Correct 8 ms 13652 KB Output is correct
18 Correct 11 ms 13748 KB Output is correct
19 Correct 9 ms 14036 KB Output is correct
20 Correct 10 ms 14164 KB Output is correct
21 Correct 9 ms 14060 KB Output is correct
22 Correct 10 ms 14472 KB Output is correct
23 Correct 50 ms 51580 KB Output is correct
24 Correct 50 ms 51540 KB Output is correct
25 Correct 56 ms 51600 KB Output is correct
26 Correct 74 ms 51588 KB Output is correct
27 Correct 52 ms 51580 KB Output is correct
28 Correct 66 ms 58688 KB Output is correct
29 Correct 51 ms 52624 KB Output is correct
30 Correct 134 ms 62668 KB Output is correct
31 Correct 116 ms 65632 KB Output is correct
32 Correct 51 ms 51656 KB Output is correct
33 Correct 64 ms 54920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 13956 KB Output is correct
2 Correct 11 ms 14036 KB Output is correct
3 Correct 9 ms 14024 KB Output is correct
4 Correct 9 ms 14036 KB Output is correct
5 Correct 10 ms 13976 KB Output is correct
6 Correct 9 ms 14100 KB Output is correct
7 Correct 11 ms 13960 KB Output is correct
8 Correct 14 ms 14548 KB Output is correct
9 Correct 14 ms 14540 KB Output is correct
10 Correct 11 ms 14420 KB Output is correct
11 Correct 11 ms 14676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11988 KB Output is correct
2 Correct 8 ms 12044 KB Output is correct
3 Correct 6 ms 11988 KB Output is correct
4 Correct 7 ms 11988 KB Output is correct
5 Correct 6 ms 11988 KB Output is correct
6 Correct 6 ms 11988 KB Output is correct
7 Correct 9 ms 12116 KB Output is correct
8 Correct 6 ms 11988 KB Output is correct
9 Correct 6 ms 11988 KB Output is correct
10 Correct 6 ms 11988 KB Output is correct
11 Correct 6 ms 12072 KB Output is correct
12 Correct 8 ms 13628 KB Output is correct
13 Correct 8 ms 13652 KB Output is correct
14 Correct 7 ms 13620 KB Output is correct
15 Correct 7 ms 13652 KB Output is correct
16 Correct 8 ms 13652 KB Output is correct
17 Correct 8 ms 13652 KB Output is correct
18 Correct 11 ms 13748 KB Output is correct
19 Correct 9 ms 14036 KB Output is correct
20 Correct 10 ms 14164 KB Output is correct
21 Correct 9 ms 14060 KB Output is correct
22 Correct 10 ms 14472 KB Output is correct
23 Correct 50 ms 51580 KB Output is correct
24 Correct 50 ms 51540 KB Output is correct
25 Correct 56 ms 51600 KB Output is correct
26 Correct 74 ms 51588 KB Output is correct
27 Correct 52 ms 51580 KB Output is correct
28 Correct 66 ms 58688 KB Output is correct
29 Correct 51 ms 52624 KB Output is correct
30 Correct 134 ms 62668 KB Output is correct
31 Correct 116 ms 65632 KB Output is correct
32 Correct 51 ms 51656 KB Output is correct
33 Correct 64 ms 54920 KB Output is correct
34 Correct 11 ms 13956 KB Output is correct
35 Correct 11 ms 14036 KB Output is correct
36 Correct 9 ms 14024 KB Output is correct
37 Correct 9 ms 14036 KB Output is correct
38 Correct 10 ms 13976 KB Output is correct
39 Correct 9 ms 14100 KB Output is correct
40 Correct 11 ms 13960 KB Output is correct
41 Correct 14 ms 14548 KB Output is correct
42 Correct 14 ms 14540 KB Output is correct
43 Correct 11 ms 14420 KB Output is correct
44 Correct 11 ms 14676 KB Output is correct
45 Correct 55 ms 52132 KB Output is correct
46 Correct 61 ms 52096 KB Output is correct
47 Correct 65 ms 52096 KB Output is correct
48 Correct 85 ms 52064 KB Output is correct
49 Correct 60 ms 52108 KB Output is correct
50 Correct 74 ms 58940 KB Output is correct
51 Correct 72 ms 59708 KB Output is correct
52 Correct 161 ms 71056 KB Output is correct
53 Correct 144 ms 69536 KB Output is correct
54 Correct 153 ms 68724 KB Output is correct
55 Correct 197 ms 78576 KB Output is correct
56 Correct 1804 ms 301812 KB Output is correct
57 Correct 490 ms 116540 KB Output is correct
58 Correct 384 ms 113460 KB Output is correct
59 Correct 487 ms 112936 KB Output is correct