Submission #779678

# Submission time Handle Problem Language Result Execution time Memory
779678 2023-07-11T16:06:10 Z Ahmed57 Abduction 2 (JOI17_abduction2) C++17
100 / 100
1038 ms 193384 KB
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")

#include <bits/stdc++.h>
using namespace std;
//dir = 0 left
//dir = 1 right
//dir = 2 up
//dir = 3 down
unordered_map<int,long long>dp[50001][4];
int n,m,q;int logg[50001];
int arr1[50001],arr2[50001];
int spar1[50001][16];
int spar2[50001][16];
int query1(int l,int r){
    int sz = r-l+1;
    return max(spar1[l][logg[sz]],spar1[r-(1<<logg[sz])+1][logg[sz]]);
}
int query2(int l,int r){
    int sz = r-l+1;
    return max(spar2[l][logg[sz]],spar2[r-(1<<logg[sz])+1][logg[sz]]);
}
long long solve(int x,int y,int dir){
    if(x<0||x>=n||y<0||y>=m)return -1;
    //cout<<x<<" "<<y<<" "<<dir<<endl;
    if(dp[x][dir].count(y)!=0)return dp[x][dir][y];
    //cout<<x<<" "<<y<<" "<<dir<<endl;
    if(dir==0){
        int l = 0, r = y , ans = -1;
        while(l<=r){
            int mid = (l+r)/2;
            if(query2(mid,y)>arr1[x]){
                ans = mid;
                l = mid+1;
            }else r = mid-1;
        }
        //cout<<y<<" "<<ans<<" "<<dir<<endl;
        if(ans!=-1){
            long long c1 = 0;
            c1 = max(c1,solve(x-1,ans,2)+1);
            c1 = max(c1,solve(x+1,ans,3)+1);
            return dp[x][dir][y] = c1+(y-ans);
        }else{
            return dp[x][dir][y] = y;
        }
    }if(dir==1){
        int l = y, r = m-1 , ans = -1;
        while(l<=r){
            int mid = (l+r)/2;
            if(query2(y,mid)>arr1[x]){
                ans = mid;
                r = mid-1;
            }else l = mid+1;
        }
        //cout<<y<<" "<<ans<<" "<<dir<<endl;
        if(ans!=-1){
            long long c1 = 0;
            c1 = max(c1,solve(x-1,ans,2)+1);
            c1 = max(c1,solve(x+1,ans,3)+1);
            return dp[x][dir][y] = c1+(ans-y);
        }else{
            return dp[x][dir][y] = (m-1)-y;
        }
    }if(dir==2){
        int l = 0, r = x , ans = -1;
        while(l<=r){
            int mid = (l+r)/2;
            if(query1(mid,x)>arr2[y]){
                ans = mid;
                l = mid+1;
            }else r = mid-1;
        }
        //cout<<x<<" "<<ans<<" "<<dir<<endl;
        if(ans!=-1){
            long long c1 = 0;
            c1 = max(c1,solve(ans,y-1,0)+1);
            c1 = max(c1,solve(ans,y+1,1)+1);
            return dp[x][dir][y] = c1+(x-ans);
        }else{
            return dp[x][dir][y] = x;
        }
    }if(dir==3){
        int l = x, r = n-1 , ans = -1;
        while(l<=r){
            int mid = (l+r)/2;
            if(query1(x,mid)>arr2[y]){
                ans = mid;
                r = mid-1;
            }else l = mid+1;
        }
        //cout<<x<<" "<<ans<<" "<<dir<<endl;
        if(ans!=-1){
            long long c1 = 0;
            c1 = max(c1,solve(ans,y-1,0)+1);
            c1 = max(c1,solve(ans,y+1,1)+1);
            return dp[x][dir][y] = c1+(ans-x);
        }else{
            return dp[x][dir][y] = (n-1)-x;
        }
    }
}
int main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>n>>m>>q;
    for(int i = 0;i<n;i++)cin>>arr1[i];
    for(int i = 0;i<m;i++)cin>>arr2[i];
    for(int i = 0;i<n;i++)spar1[i][0] = arr1[i];
    for(int i = 0;i<m;i++)spar2[i][0] = arr2[i];
    for(int j = 1;j<16;j++){
        for(int i = 0;i<n;i++){
            if(i+(1<<j)>n)break;
            spar1[i][j] = max(spar1[i][j-1],spar1[i+(1<<(j-1))][j-1]);
        }
    }
    for(int j = 1;j<16;j++){
        for(int i = 0;i<m;i++){
            if(i+(1<<j)>m)break;
            spar2[i][j] = max(spar2[i][j-1],spar2[i+(1<<(j-1))][j-1]);
        }
    }
    logg[1] = 0;
    for(int i = 2;i<=50000;i++)logg[i] = logg[i/2]+1;
    while(q--){
        int a,b;cin>>a>>b;
        a--;b--;
        long long ans = 0;
        ans = max(ans,solve(a,b-1,0)+1);
        ans = max(ans,solve(a,b+1,1)+1);
        ans = max(ans,solve(a-1,b,2)+1);
        ans = max(ans,solve(a+1,b,3)+1);
        cout<<ans<<"\n";
    }
}

Compilation message

abduction2.cpp: In function 'long long int solve(int, int, int)':
abduction2.cpp:100:1: warning: control reaches end of non-void function [-Wreturn-type]
  100 | }
      | ^
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11476 KB Output is correct
2 Correct 5 ms 11476 KB Output is correct
3 Correct 6 ms 11476 KB Output is correct
4 Correct 6 ms 11500 KB Output is correct
5 Correct 6 ms 11476 KB Output is correct
6 Correct 5 ms 11476 KB Output is correct
7 Correct 5 ms 11476 KB Output is correct
8 Correct 6 ms 11424 KB Output is correct
9 Correct 6 ms 11476 KB Output is correct
10 Correct 6 ms 11476 KB Output is correct
11 Correct 6 ms 11476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11476 KB Output is correct
2 Correct 5 ms 11476 KB Output is correct
3 Correct 6 ms 11476 KB Output is correct
4 Correct 6 ms 11500 KB Output is correct
5 Correct 6 ms 11476 KB Output is correct
6 Correct 5 ms 11476 KB Output is correct
7 Correct 5 ms 11476 KB Output is correct
8 Correct 6 ms 11424 KB Output is correct
9 Correct 6 ms 11476 KB Output is correct
10 Correct 6 ms 11476 KB Output is correct
11 Correct 6 ms 11476 KB Output is correct
12 Correct 6 ms 11732 KB Output is correct
13 Correct 8 ms 11728 KB Output is correct
14 Correct 7 ms 11724 KB Output is correct
15 Correct 8 ms 11772 KB Output is correct
16 Correct 6 ms 11732 KB Output is correct
17 Correct 6 ms 11732 KB Output is correct
18 Correct 6 ms 11860 KB Output is correct
19 Correct 7 ms 12244 KB Output is correct
20 Correct 8 ms 12424 KB Output is correct
21 Correct 7 ms 12244 KB Output is correct
22 Correct 8 ms 12864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11476 KB Output is correct
2 Correct 5 ms 11476 KB Output is correct
3 Correct 6 ms 11476 KB Output is correct
4 Correct 6 ms 11500 KB Output is correct
5 Correct 6 ms 11476 KB Output is correct
6 Correct 5 ms 11476 KB Output is correct
7 Correct 5 ms 11476 KB Output is correct
8 Correct 6 ms 11424 KB Output is correct
9 Correct 6 ms 11476 KB Output is correct
10 Correct 6 ms 11476 KB Output is correct
11 Correct 6 ms 11476 KB Output is correct
12 Correct 6 ms 11732 KB Output is correct
13 Correct 8 ms 11728 KB Output is correct
14 Correct 7 ms 11724 KB Output is correct
15 Correct 8 ms 11772 KB Output is correct
16 Correct 6 ms 11732 KB Output is correct
17 Correct 6 ms 11732 KB Output is correct
18 Correct 6 ms 11860 KB Output is correct
19 Correct 7 ms 12244 KB Output is correct
20 Correct 8 ms 12424 KB Output is correct
21 Correct 7 ms 12244 KB Output is correct
22 Correct 8 ms 12864 KB Output is correct
23 Correct 17 ms 18096 KB Output is correct
24 Correct 17 ms 18056 KB Output is correct
25 Correct 17 ms 18100 KB Output is correct
26 Correct 17 ms 18052 KB Output is correct
27 Correct 18 ms 18132 KB Output is correct
28 Correct 41 ms 29800 KB Output is correct
29 Correct 20 ms 19768 KB Output is correct
30 Correct 55 ms 32428 KB Output is correct
31 Correct 65 ms 36568 KB Output is correct
32 Correct 19 ms 18864 KB Output is correct
33 Correct 26 ms 22456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12116 KB Output is correct
2 Correct 7 ms 12116 KB Output is correct
3 Correct 7 ms 12116 KB Output is correct
4 Correct 7 ms 12116 KB Output is correct
5 Correct 7 ms 12116 KB Output is correct
6 Correct 8 ms 12336 KB Output is correct
7 Correct 7 ms 12372 KB Output is correct
8 Correct 10 ms 12900 KB Output is correct
9 Correct 9 ms 12840 KB Output is correct
10 Correct 10 ms 12756 KB Output is correct
11 Correct 10 ms 13056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11476 KB Output is correct
2 Correct 5 ms 11476 KB Output is correct
3 Correct 6 ms 11476 KB Output is correct
4 Correct 6 ms 11500 KB Output is correct
5 Correct 6 ms 11476 KB Output is correct
6 Correct 5 ms 11476 KB Output is correct
7 Correct 5 ms 11476 KB Output is correct
8 Correct 6 ms 11424 KB Output is correct
9 Correct 6 ms 11476 KB Output is correct
10 Correct 6 ms 11476 KB Output is correct
11 Correct 6 ms 11476 KB Output is correct
12 Correct 6 ms 11732 KB Output is correct
13 Correct 8 ms 11728 KB Output is correct
14 Correct 7 ms 11724 KB Output is correct
15 Correct 8 ms 11772 KB Output is correct
16 Correct 6 ms 11732 KB Output is correct
17 Correct 6 ms 11732 KB Output is correct
18 Correct 6 ms 11860 KB Output is correct
19 Correct 7 ms 12244 KB Output is correct
20 Correct 8 ms 12424 KB Output is correct
21 Correct 7 ms 12244 KB Output is correct
22 Correct 8 ms 12864 KB Output is correct
23 Correct 17 ms 18096 KB Output is correct
24 Correct 17 ms 18056 KB Output is correct
25 Correct 17 ms 18100 KB Output is correct
26 Correct 17 ms 18052 KB Output is correct
27 Correct 18 ms 18132 KB Output is correct
28 Correct 41 ms 29800 KB Output is correct
29 Correct 20 ms 19768 KB Output is correct
30 Correct 55 ms 32428 KB Output is correct
31 Correct 65 ms 36568 KB Output is correct
32 Correct 19 ms 18864 KB Output is correct
33 Correct 26 ms 22456 KB Output is correct
34 Correct 8 ms 12116 KB Output is correct
35 Correct 7 ms 12116 KB Output is correct
36 Correct 7 ms 12116 KB Output is correct
37 Correct 7 ms 12116 KB Output is correct
38 Correct 7 ms 12116 KB Output is correct
39 Correct 8 ms 12336 KB Output is correct
40 Correct 7 ms 12372 KB Output is correct
41 Correct 10 ms 12900 KB Output is correct
42 Correct 9 ms 12840 KB Output is correct
43 Correct 10 ms 12756 KB Output is correct
44 Correct 10 ms 13056 KB Output is correct
45 Correct 21 ms 18772 KB Output is correct
46 Correct 25 ms 18772 KB Output is correct
47 Correct 20 ms 18752 KB Output is correct
48 Correct 20 ms 18772 KB Output is correct
49 Correct 20 ms 18752 KB Output is correct
50 Correct 38 ms 32140 KB Output is correct
51 Correct 39 ms 33268 KB Output is correct
52 Correct 90 ms 44108 KB Output is correct
53 Correct 88 ms 43080 KB Output is correct
54 Correct 86 ms 41628 KB Output is correct
55 Correct 126 ms 54032 KB Output is correct
56 Correct 1038 ms 193384 KB Output is correct
57 Correct 287 ms 67340 KB Output is correct
58 Correct 236 ms 66716 KB Output is correct
59 Correct 246 ms 67696 KB Output is correct