Submission #233002

# Submission time Handle Problem Language Result Execution time Memory
233002 2020-05-18T22:15:30 Z thebes Abduction 2 (JOI17_abduction2) C++14
44 / 100
5000 ms 18676 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>,int> 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;

int solve(int x,int y,int d){
    if(dp.count(mt(x,y,d))) return dp[mt(x,y,d)];
    int ans = 0;
    if(a[x]>b[y]){ // horizontal line
        if(d!=0){
            int nxt = C.lquery(y,a[x]);
            if(~nxt) ans=max(ans,y-nxt+solve(x,nxt,1));
            else ans=max(ans,y);
        }
        if(d!=1){
            int nxt = C.rquery(y,a[x]);
            if(~nxt) ans=max(ans,nxt-y+solve(x,nxt,0));
            else ans=max(ans,M-1-y);
        }
    }
    else{ // vertical line
        if(d!=2){
            int nxt = R.lquery(x,b[y]);
            if(~nxt) ans=max(ans,x-nxt+solve(nxt,y,3));
            else ans=max(ans,x);
        }
        if(d!=3){
            int nxt = R.rquery(x,b[y]);
            if(~nxt) ans=max(ans,nxt-x+solve(nxt,y,2));
            else ans=max(ans,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);
        dp.clear();
        int 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,solve(nx-1,ny-1,i)+1);
        }
        printf("%d\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 384 KB Output is correct
3 Correct 5 ms 256 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 4 ms 256 KB Output is correct
6 Correct 5 ms 256 KB Output is correct
7 Correct 6 ms 256 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 256 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 256 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 4 ms 256 KB Output is correct
6 Correct 5 ms 256 KB Output is correct
7 Correct 6 ms 256 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 6 ms 384 KB Output is correct
13 Correct 7 ms 512 KB Output is correct
14 Correct 5 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 6 ms 436 KB Output is correct
19 Correct 7 ms 640 KB Output is correct
20 Correct 9 ms 896 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 384 KB Output is correct
3 Correct 5 ms 256 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 4 ms 256 KB Output is correct
6 Correct 5 ms 256 KB Output is correct
7 Correct 6 ms 256 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 6 ms 384 KB Output is correct
13 Correct 7 ms 512 KB Output is correct
14 Correct 5 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 6 ms 436 KB Output is correct
19 Correct 7 ms 640 KB Output is correct
20 Correct 9 ms 896 KB Output is correct
21 Correct 8 ms 768 KB Output is correct
22 Correct 10 ms 1024 KB Output is correct
23 Correct 24 ms 3196 KB Output is correct
24 Correct 25 ms 3288 KB Output is correct
25 Correct 24 ms 3196 KB Output is correct
26 Correct 26 ms 3188 KB Output is correct
27 Correct 24 ms 3316 KB Output is correct
28 Correct 43 ms 11884 KB Output is correct
29 Correct 25 ms 4084 KB Output is correct
30 Correct 98 ms 11116 KB Output is correct
31 Correct 122 ms 13480 KB Output is correct
32 Correct 26 ms 3196 KB Output is correct
33 Correct 43 ms 5480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 384 KB Output is correct
2 Correct 8 ms 384 KB Output is correct
3 Correct 8 ms 384 KB Output is correct
4 Correct 8 ms 384 KB Output is correct
5 Correct 8 ms 384 KB Output is correct
6 Correct 31 ms 896 KB Output is correct
7 Correct 24 ms 896 KB Output is correct
8 Correct 144 ms 1016 KB Output is correct
9 Correct 157 ms 1144 KB Output is correct
10 Correct 171 ms 1144 KB Output is correct
11 Correct 225 ms 1272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 256 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 256 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 4 ms 256 KB Output is correct
6 Correct 5 ms 256 KB Output is correct
7 Correct 6 ms 256 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 6 ms 384 KB Output is correct
13 Correct 7 ms 512 KB Output is correct
14 Correct 5 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 6 ms 436 KB Output is correct
19 Correct 7 ms 640 KB Output is correct
20 Correct 9 ms 896 KB Output is correct
21 Correct 8 ms 768 KB Output is correct
22 Correct 10 ms 1024 KB Output is correct
23 Correct 24 ms 3196 KB Output is correct
24 Correct 25 ms 3288 KB Output is correct
25 Correct 24 ms 3196 KB Output is correct
26 Correct 26 ms 3188 KB Output is correct
27 Correct 24 ms 3316 KB Output is correct
28 Correct 43 ms 11884 KB Output is correct
29 Correct 25 ms 4084 KB Output is correct
30 Correct 98 ms 11116 KB Output is correct
31 Correct 122 ms 13480 KB Output is correct
32 Correct 26 ms 3196 KB Output is correct
33 Correct 43 ms 5480 KB Output is correct
34 Correct 8 ms 384 KB Output is correct
35 Correct 8 ms 384 KB Output is correct
36 Correct 8 ms 384 KB Output is correct
37 Correct 8 ms 384 KB Output is correct
38 Correct 8 ms 384 KB Output is correct
39 Correct 31 ms 896 KB Output is correct
40 Correct 24 ms 896 KB Output is correct
41 Correct 144 ms 1016 KB Output is correct
42 Correct 157 ms 1144 KB Output is correct
43 Correct 171 ms 1144 KB Output is correct
44 Correct 225 ms 1272 KB Output is correct
45 Correct 31 ms 3188 KB Output is correct
46 Correct 29 ms 3196 KB Output is correct
47 Correct 29 ms 3316 KB Output is correct
48 Correct 30 ms 3196 KB Output is correct
49 Correct 29 ms 3196 KB Output is correct
50 Correct 685 ms 15212 KB Output is correct
51 Correct 604 ms 16104 KB Output is correct
52 Execution timed out 5050 ms 18676 KB Time limit exceeded
53 Halted 0 ms 0 KB -