Submission #779670

# Submission time Handle Problem Language Result Execution time Memory
779670 2023-07-11T15:58:20 Z Ahmed57 Abduction 2 (JOI17_abduction2) C++17
44 / 100
99 ms 38220 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
map<int,int>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]]);
}
int 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){
            int 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){
            int 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){
            int 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){
            int 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--;
        int 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 '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 5 ms 9812 KB Output is correct
2 Correct 4 ms 9812 KB Output is correct
3 Correct 4 ms 9812 KB Output is correct
4 Correct 4 ms 9844 KB Output is correct
5 Correct 4 ms 9852 KB Output is correct
6 Correct 5 ms 9848 KB Output is correct
7 Correct 4 ms 9812 KB Output is correct
8 Correct 5 ms 9812 KB Output is correct
9 Correct 5 ms 9812 KB Output is correct
10 Correct 7 ms 9960 KB Output is correct
11 Correct 5 ms 9848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9812 KB Output is correct
2 Correct 4 ms 9812 KB Output is correct
3 Correct 4 ms 9812 KB Output is correct
4 Correct 4 ms 9844 KB Output is correct
5 Correct 4 ms 9852 KB Output is correct
6 Correct 5 ms 9848 KB Output is correct
7 Correct 4 ms 9812 KB Output is correct
8 Correct 5 ms 9812 KB Output is correct
9 Correct 5 ms 9812 KB Output is correct
10 Correct 7 ms 9960 KB Output is correct
11 Correct 5 ms 9848 KB Output is correct
12 Correct 6 ms 10244 KB Output is correct
13 Correct 6 ms 10156 KB Output is correct
14 Correct 6 ms 10248 KB Output is correct
15 Correct 6 ms 10244 KB Output is correct
16 Correct 6 ms 10240 KB Output is correct
17 Correct 5 ms 10196 KB Output is correct
18 Correct 6 ms 10196 KB Output is correct
19 Correct 7 ms 10488 KB Output is correct
20 Correct 8 ms 10660 KB Output is correct
21 Correct 6 ms 10480 KB Output is correct
22 Correct 6 ms 10784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9812 KB Output is correct
2 Correct 4 ms 9812 KB Output is correct
3 Correct 4 ms 9812 KB Output is correct
4 Correct 4 ms 9844 KB Output is correct
5 Correct 4 ms 9852 KB Output is correct
6 Correct 5 ms 9848 KB Output is correct
7 Correct 4 ms 9812 KB Output is correct
8 Correct 5 ms 9812 KB Output is correct
9 Correct 5 ms 9812 KB Output is correct
10 Correct 7 ms 9960 KB Output is correct
11 Correct 5 ms 9848 KB Output is correct
12 Correct 6 ms 10244 KB Output is correct
13 Correct 6 ms 10156 KB Output is correct
14 Correct 6 ms 10248 KB Output is correct
15 Correct 6 ms 10244 KB Output is correct
16 Correct 6 ms 10240 KB Output is correct
17 Correct 5 ms 10196 KB Output is correct
18 Correct 6 ms 10196 KB Output is correct
19 Correct 7 ms 10488 KB Output is correct
20 Correct 8 ms 10660 KB Output is correct
21 Correct 6 ms 10480 KB Output is correct
22 Correct 6 ms 10784 KB Output is correct
23 Correct 17 ms 17456 KB Output is correct
24 Correct 17 ms 17504 KB Output is correct
25 Correct 16 ms 17456 KB Output is correct
26 Correct 17 ms 17456 KB Output is correct
27 Correct 18 ms 17492 KB Output is correct
28 Correct 27 ms 23572 KB Output is correct
29 Correct 18 ms 18304 KB Output is correct
30 Correct 44 ms 26164 KB Output is correct
31 Correct 55 ms 28636 KB Output is correct
32 Correct 21 ms 17900 KB Output is correct
33 Correct 24 ms 20144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 10516 KB Output is correct
2 Correct 7 ms 10428 KB Output is correct
3 Correct 6 ms 10452 KB Output is correct
4 Correct 7 ms 10504 KB Output is correct
5 Correct 8 ms 10452 KB Output is correct
6 Correct 6 ms 10532 KB Output is correct
7 Correct 6 ms 10452 KB Output is correct
8 Correct 7 ms 10836 KB Output is correct
9 Correct 7 ms 10860 KB Output is correct
10 Correct 7 ms 10836 KB Output is correct
11 Correct 7 ms 10964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9812 KB Output is correct
2 Correct 4 ms 9812 KB Output is correct
3 Correct 4 ms 9812 KB Output is correct
4 Correct 4 ms 9844 KB Output is correct
5 Correct 4 ms 9852 KB Output is correct
6 Correct 5 ms 9848 KB Output is correct
7 Correct 4 ms 9812 KB Output is correct
8 Correct 5 ms 9812 KB Output is correct
9 Correct 5 ms 9812 KB Output is correct
10 Correct 7 ms 9960 KB Output is correct
11 Correct 5 ms 9848 KB Output is correct
12 Correct 6 ms 10244 KB Output is correct
13 Correct 6 ms 10156 KB Output is correct
14 Correct 6 ms 10248 KB Output is correct
15 Correct 6 ms 10244 KB Output is correct
16 Correct 6 ms 10240 KB Output is correct
17 Correct 5 ms 10196 KB Output is correct
18 Correct 6 ms 10196 KB Output is correct
19 Correct 7 ms 10488 KB Output is correct
20 Correct 8 ms 10660 KB Output is correct
21 Correct 6 ms 10480 KB Output is correct
22 Correct 6 ms 10784 KB Output is correct
23 Correct 17 ms 17456 KB Output is correct
24 Correct 17 ms 17504 KB Output is correct
25 Correct 16 ms 17456 KB Output is correct
26 Correct 17 ms 17456 KB Output is correct
27 Correct 18 ms 17492 KB Output is correct
28 Correct 27 ms 23572 KB Output is correct
29 Correct 18 ms 18304 KB Output is correct
30 Correct 44 ms 26164 KB Output is correct
31 Correct 55 ms 28636 KB Output is correct
32 Correct 21 ms 17900 KB Output is correct
33 Correct 24 ms 20144 KB Output is correct
34 Correct 8 ms 10516 KB Output is correct
35 Correct 7 ms 10428 KB Output is correct
36 Correct 6 ms 10452 KB Output is correct
37 Correct 7 ms 10504 KB Output is correct
38 Correct 8 ms 10452 KB Output is correct
39 Correct 6 ms 10532 KB Output is correct
40 Correct 6 ms 10452 KB Output is correct
41 Correct 7 ms 10836 KB Output is correct
42 Correct 7 ms 10860 KB Output is correct
43 Correct 7 ms 10836 KB Output is correct
44 Correct 7 ms 10964 KB Output is correct
45 Correct 20 ms 17868 KB Output is correct
46 Correct 19 ms 17896 KB Output is correct
47 Correct 20 ms 17876 KB Output is correct
48 Correct 22 ms 17868 KB Output is correct
49 Correct 26 ms 17852 KB Output is correct
50 Correct 28 ms 23492 KB Output is correct
51 Correct 28 ms 24064 KB Output is correct
52 Correct 67 ms 32332 KB Output is correct
53 Correct 65 ms 31692 KB Output is correct
54 Correct 62 ms 31012 KB Output is correct
55 Incorrect 99 ms 38220 KB Output isn't correct
56 Halted 0 ms 0 KB -