Submission #233006

# Submission time Handle Problem Language Result Execution time Memory
233006 2020-05-18T22:19:18 Z thebes Abduction 2 (JOI17_abduction2) C++14
100 / 100
1899 ms 128644 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<double,double> pdd;
typedef vector<int> vi;
#define F first
#define S second
#define pb push_back
#define mt make_tuple

const int MN = 1e5+5;
int N, M, Q, i, j, x, y, dx[]={0,0,1,-1}, dy[]={1,-1,0,0};
map<tuple<int,int,int>,ll> dp; vi a, b;

struct MaxSegmentTree{
    int st[2*MN], n;
    void build(int i,int s,int e,vi &vec){
        if(s!=e){
            build(2*i,s,(s+e)/2,vec);
            build(2*i+1,(s+e)/2+1,e,vec);
            st[i]=max(st[2*i],st[2*i+1]);
        }
        else st[i]=vec[s];
    }
    void init(vi vec){
        n = (int)vec.size();
        build(1,0,n-1,vec);
    }
    int lquery(int i,int s,int e,int ss,int se,int val){
        if(ss>se) return -1;
        if(s>=ss&&e<=se){
            if(st[i]<=val) return -1;
            else if(s==e) return e;
        }
        else if((s+e)/2<ss) return lquery(2*i+1,(s+e)/2+1,e,ss,se,val);
        else if((s+e)/2>=se) return lquery(2*i,s,(s+e)/2,ss,se,val);
        int r = lquery(2*i+1,(s+e)/2+1,e,ss,se,val);
        return r==-1?lquery(2*i,s,(s+e)/2,ss,se,val):r;
    }
    int lquery(int idx,int val){
        return lquery(1,0,n-1,0,idx-1,val);
    }
    int rquery(int i,int s,int e,int ss,int se,int val){
        if(ss>se) return -1;
        if(s>=ss&&e<=se){
            if(st[i]<=val) return -1;
            else if(s==e) return s;
        }
        else if((s+e)/2<ss) return rquery(2*i+1,(s+e)/2+1,e,ss,se,val);
        else if((s+e)/2>=se) return rquery(2*i,s,(s+e)/2,ss,se,val);
        int r = rquery(2*i,s,(s+e)/2,ss,se,val);
        return r==-1?rquery(2*i+1,(s+e)/2+1,e,ss,se,val):r;
    }
    int rquery(int idx,int val){
        return rquery(1,0,n-1,idx+1,n,val);
    }
}R,C;

ll solve(int x,int y,int d){
    if(dp.count(mt(x,y,d))) return dp[mt(x,y,d)];
    ll ans = 0;
    if(a[x]>b[y]){
        if(d!=0){
            int nxt = C.lquery(y,a[x]);
            if(~nxt) ans=max(ans,(ll)y-nxt+(ll)solve(x,nxt,1));
            else ans=max(ans,(ll)y);
        }
        if(d!=1){
            int nxt = C.rquery(y,a[x]);
            if(~nxt) ans=max(ans,(ll)nxt-y+(ll)solve(x,nxt,0));
            else ans=max(ans,(ll)M-1-y);
        }
    }
    else{
        if(d!=2){
            int nxt = R.lquery(x,b[y]);
            if(~nxt) ans=max(ans,(ll)x-nxt+(ll)solve(nxt,y,3));
            else ans=max(ans,(ll)x);
        }
        if(d!=3){
            int nxt = R.rquery(x,b[y]);
            if(~nxt) ans=max(ans,(ll)nxt-x+(ll)solve(nxt,y,2));
            else ans=max(ans,(ll)N-1-x);
        }
    }
    return dp[mt(x,y,d)]=ans;
}

int main(){
    scanf("%d%d%d",&N,&M,&Q);
    for(i=1;i<=N;i++){
        scanf("%d",&x);
        a.pb(x);
    }
    for(i=1;i<=M;i++){
        scanf("%d",&x);
        b.pb(x);
    }
    R.init(a), C.init(b);
    for(;Q;Q--){
        scanf("%d%d",&x,&y);
        ll sna = 0;
        for(i=0;i<4;i++){
            int nx=x+dx[i], ny=y+dy[i];
            if(nx<1||nx>N||ny<1||ny>M) continue;
            sna=max(sna,(ll)solve(nx-1,ny-1,i)+1);
        }
        printf("%lld\n",sna);
    }
    return 0;
}

Compilation message

abduction2.cpp: In function 'int main()':
abduction2.cpp:92:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d%d",&N,&M,&Q);
     ~~~~~^~~~~~~~~~~~~~~~~~~
abduction2.cpp:94:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d",&x);
         ~~~~~^~~~~~~~~
abduction2.cpp:98:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d",&x);
         ~~~~~^~~~~~~~~
abduction2.cpp:103:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d",&x,&y);
         ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 256 KB Output is correct
2 Correct 4 ms 256 KB Output is correct
3 Correct 4 ms 256 KB Output is correct
4 Correct 4 ms 256 KB Output is correct
5 Correct 5 ms 256 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 4 ms 256 KB Output is correct
9 Correct 4 ms 256 KB Output is correct
10 Correct 4 ms 256 KB Output is correct
11 Correct 5 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 256 KB Output is correct
2 Correct 4 ms 256 KB Output is correct
3 Correct 4 ms 256 KB Output is correct
4 Correct 4 ms 256 KB Output is correct
5 Correct 5 ms 256 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 4 ms 256 KB Output is correct
9 Correct 4 ms 256 KB Output is correct
10 Correct 4 ms 256 KB Output is correct
11 Correct 5 ms 256 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 6 ms 384 KB Output is correct
14 Correct 7 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 512 KB Output is correct
19 Correct 7 ms 640 KB Output is correct
20 Correct 5 ms 768 KB Output is correct
21 Correct 8 ms 768 KB Output is correct
22 Correct 10 ms 1024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 256 KB Output is correct
2 Correct 4 ms 256 KB Output is correct
3 Correct 4 ms 256 KB Output is correct
4 Correct 4 ms 256 KB Output is correct
5 Correct 5 ms 256 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 4 ms 256 KB Output is correct
9 Correct 4 ms 256 KB Output is correct
10 Correct 4 ms 256 KB Output is correct
11 Correct 5 ms 256 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 6 ms 384 KB Output is correct
14 Correct 7 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 512 KB Output is correct
19 Correct 7 ms 640 KB Output is correct
20 Correct 5 ms 768 KB Output is correct
21 Correct 8 ms 768 KB Output is correct
22 Correct 10 ms 1024 KB Output is correct
23 Correct 25 ms 2292 KB Output is correct
24 Correct 24 ms 2300 KB Output is correct
25 Correct 24 ms 2300 KB Output is correct
26 Correct 24 ms 2300 KB Output is correct
27 Correct 25 ms 2300 KB Output is correct
28 Correct 43 ms 12016 KB Output is correct
29 Correct 26 ms 3308 KB Output is correct
30 Correct 97 ms 10604 KB Output is correct
31 Correct 118 ms 13036 KB Output is correct
32 Correct 28 ms 2300 KB Output is correct
33 Correct 43 ms 4588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 512 KB Output is correct
2 Correct 8 ms 512 KB Output is correct
3 Correct 8 ms 512 KB Output is correct
4 Correct 8 ms 640 KB Output is correct
5 Correct 8 ms 512 KB Output is correct
6 Correct 6 ms 896 KB Output is correct
7 Correct 7 ms 768 KB Output is correct
8 Correct 11 ms 896 KB Output is correct
9 Correct 10 ms 1024 KB Output is correct
10 Correct 10 ms 896 KB Output is correct
11 Correct 11 ms 1024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 256 KB Output is correct
2 Correct 4 ms 256 KB Output is correct
3 Correct 4 ms 256 KB Output is correct
4 Correct 4 ms 256 KB Output is correct
5 Correct 5 ms 256 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 4 ms 256 KB Output is correct
9 Correct 4 ms 256 KB Output is correct
10 Correct 4 ms 256 KB Output is correct
11 Correct 5 ms 256 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 6 ms 384 KB Output is correct
14 Correct 7 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 512 KB Output is correct
19 Correct 7 ms 640 KB Output is correct
20 Correct 5 ms 768 KB Output is correct
21 Correct 8 ms 768 KB Output is correct
22 Correct 10 ms 1024 KB Output is correct
23 Correct 25 ms 2292 KB Output is correct
24 Correct 24 ms 2300 KB Output is correct
25 Correct 24 ms 2300 KB Output is correct
26 Correct 24 ms 2300 KB Output is correct
27 Correct 25 ms 2300 KB Output is correct
28 Correct 43 ms 12016 KB Output is correct
29 Correct 26 ms 3308 KB Output is correct
30 Correct 97 ms 10604 KB Output is correct
31 Correct 118 ms 13036 KB Output is correct
32 Correct 28 ms 2300 KB Output is correct
33 Correct 43 ms 4588 KB Output is correct
34 Correct 8 ms 512 KB Output is correct
35 Correct 8 ms 512 KB Output is correct
36 Correct 8 ms 512 KB Output is correct
37 Correct 8 ms 640 KB Output is correct
38 Correct 8 ms 512 KB Output is correct
39 Correct 6 ms 896 KB Output is correct
40 Correct 7 ms 768 KB Output is correct
41 Correct 11 ms 896 KB Output is correct
42 Correct 10 ms 1024 KB Output is correct
43 Correct 10 ms 896 KB Output is correct
44 Correct 11 ms 1024 KB Output is correct
45 Correct 29 ms 2300 KB Output is correct
46 Correct 29 ms 2300 KB Output is correct
47 Correct 29 ms 2292 KB Output is correct
48 Correct 29 ms 2300 KB Output is correct
49 Correct 29 ms 2292 KB Output is correct
50 Correct 50 ms 9324 KB Output is correct
51 Correct 52 ms 10604 KB Output is correct
52 Correct 159 ms 14060 KB Output is correct
53 Correct 153 ms 13428 KB Output is correct
54 Correct 149 ms 13296 KB Output is correct
55 Correct 239 ms 19824 KB Output is correct
56 Correct 1899 ms 128644 KB Output is correct
57 Correct 491 ms 35692 KB Output is correct
58 Correct 459 ms 34336 KB Output is correct
59 Correct 460 ms 33960 KB Output is correct