답안 #1077387

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1077387 2024-08-27T06:33:25 Z urd05 유괴 2 (JOI17_abduction2) C++17
44 / 100
471 ms 10060 KB
# pragma GCC optimize ("O3")
# pragma GCC optimize ("Ofast")
# pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;

int n,m,q;
int a[50005];
int b[50005];
typedef pair<int,int> P;
map<P,int> mp;
int as0=0;
int as1=0;
int bs0=0;
int bs1=0;
P va0[50005];
P va1[50005];
P vb0[50005];
P vb1[50005];
vector<P> press;
int dp[500005];
int sa0[50001];
int sa1[50001];
int sb0[50005];
int sb1[50005];
int cx,cy;

int ans(int x,int y,int d=-1) {
    int ind;
    if (a[x]<b[y]) {
        if (y>=cy) {
            ind=2*x+1;
        }
        else {
            ind=2*x;
        }
    }
    else {
        if (x>=cx) {
            ind=2*(n+5+y)+1;
        }
        else {
            ind=2*(n+5+y);
        }
    }
    if (dp[ind]!=-1) {
        return dp[ind];
    }
    bool flag;
    if (a[x]>b[y]) {
        flag=false;
    }
    else {
        flag=true;
    }
    int ret=0;
    if (!flag) {
        if (d!=1) {
            int i0=sb0[x];
            if (i0==bs0) {
                ret=max(ret,y-1);
            }
            else {
                ret=max(ret,y-vb0[i0].second+ans(x,vb0[i0].second));
            }
        }
        if (d!=3) {
            int i1=sb1[x];
            if (i1==bs1) {
                ret=max(ret,m-y);
            }
            else {
                ret=max(ret,vb1[i1].second-y+ans(x,vb1[i1].second));
            }
        }
    }
    else {
        if (d!=0) {
            int i0=sa0[y];
            if (i0==as0) {
                ret=max(ret,x-1);
            }
            else {
                ret=max(ret,x-va0[i0].second+ans(va0[i0].second,y));
            }
        }
        if (d!=2) {
            int i1=sa1[y];
            if (i1==as1) {
                ret=max(ret,n-x);
            }
            else {
                ret=max(ret,va1[i1].second-x+ans(va1[i1].second,y));
            }
        }
    }
    //printf(".%d %d %d\n",x,y,ret);
    return dp[ind]=ret;
}

int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
vector<P> av;
vector<P> bv;

int main(void) {
    scanf("%d %d %d",&n,&m,&q);
    for(int i=1;i<=n;i++) {
        scanf("%d",&a[i]);
        av.push_back(P(a[i],i));
    }
    for(int i=1;i<=m;i++) {
        scanf("%d",&b[i]);
        bv.push_back(P(b[i],i));
    }
    sort(av.begin(),av.end());
    sort(bv.begin(),bv.end());
    for(int ind=0;ind<q;ind++) {
        int x,y;
        scanf("%d %d",&x,&y);
        bool f=false;
        if (a[x]>b[y]) {
            f=true;
        }
        bool ff=false;
        int ret=0;
        for(int j=0;j<4;j++) {
            int nx=x+dx[j];
            int ny=y+dy[j];
            if (nx<=0||nx>n||ny<=0||ny>m) {
                continue;
            }
            bool nf=false;
            if ((f&&(j&1))||(!f&&(j==0||j==2))) {
                if (ff) {
                    continue;
                }
                nf=true;
                ff=true;
            }
            if (nf) {
                nx=x;
                ny=y;
            }
            cx=nx;
            cy=ny;
            int pr=-1;
            as0=0;
            as1=0;
            bs0=0;
            bs1=0;
            for(int i=nx;i<=n;i++) {
                if (a[i]>pr) {
                    va1[as1++]=(P(a[i],i));
                    pr=a[i];
                }
            }
            pr=-1;
            for(int i=nx;i>0;i--) {
                if (a[i]>pr) {
                    va0[as0++]=(P(a[i],i));
                    pr=a[i];
                }
            }
            pr=-1;
            for(int i=ny;i<=m;i++) {
                if (b[i]>pr) {
                    vb1[bs1++]=(P(b[i],i));
                    pr=b[i];
                }
            }
            pr=-1;
            for(int i=ny;i>0;i--) {
                if (b[i]>pr) {
                    vb0[bs0++]=(P(b[i],i));
                    pr=b[i];
                }
            }
            int i0=0;
            int i1=0;
            //sort(press.begin(),press.end());
            //press.erase(unique(press.begin(),press.end()),press.end());
            memset(dp,-1,sizeof(dp));
            i0=0;
            i1=0;
            for(int i=0;i<n;i++) {
                while (i0<bs0&&vb0[i0].first<av[i].first) {
                    i0++;
                }
                while (i1<bs1&&vb1[i1].first<av[i].first) {
                    i1++;
                }
                sb0[av[i].second]=i0;
                sb1[av[i].second]=i1;
            }
            i0=0;
            i1=0;
            for(int i=0;i<m;i++) {
                while (i0<as0&&va0[i0].first<bv[i].first) {
                    i0++;
                }
                while (i1<as1&&va1[i1].first<bv[i].first) {
                    i1++;
                }
                sa0[bv[i].second]=i0;
                sa1[bv[i].second]=i1;
            }
            if (!nf)
            ret=max(ret,ans(nx,ny,j)+1);
            else {
                ret=max(ret,ans(x,y,-1));
            }
            //printf(".\n");
        }
        printf("%d\n",ret);
    }
}

Compilation message

abduction2.cpp: In function 'int main()':
abduction2.cpp:107:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  107 |     scanf("%d %d %d",&n,&m,&q);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~
abduction2.cpp:109:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  109 |         scanf("%d",&a[i]);
      |         ~~~~~^~~~~~~~~~~~
abduction2.cpp:113:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  113 |         scanf("%d",&b[i]);
      |         ~~~~~^~~~~~~~~~~~
abduction2.cpp:120:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  120 |         scanf("%d %d",&x,&y);
      |         ~~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2248 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2400 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2248 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2400 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 2 ms 2396 KB Output is correct
13 Correct 2 ms 2396 KB Output is correct
14 Correct 2 ms 2396 KB Output is correct
15 Correct 2 ms 2396 KB Output is correct
16 Correct 2 ms 2396 KB Output is correct
17 Correct 2 ms 2404 KB Output is correct
18 Correct 2 ms 2396 KB Output is correct
19 Correct 2 ms 2396 KB Output is correct
20 Correct 2 ms 2652 KB Output is correct
21 Correct 2 ms 2396 KB Output is correct
22 Correct 2 ms 2652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2248 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2400 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 2 ms 2396 KB Output is correct
13 Correct 2 ms 2396 KB Output is correct
14 Correct 2 ms 2396 KB Output is correct
15 Correct 2 ms 2396 KB Output is correct
16 Correct 2 ms 2396 KB Output is correct
17 Correct 2 ms 2404 KB Output is correct
18 Correct 2 ms 2396 KB Output is correct
19 Correct 2 ms 2396 KB Output is correct
20 Correct 2 ms 2652 KB Output is correct
21 Correct 2 ms 2396 KB Output is correct
22 Correct 2 ms 2652 KB Output is correct
23 Correct 18 ms 5452 KB Output is correct
24 Correct 19 ms 5464 KB Output is correct
25 Correct 19 ms 5256 KB Output is correct
26 Correct 18 ms 5464 KB Output is correct
27 Correct 18 ms 5464 KB Output is correct
28 Correct 17 ms 8532 KB Output is correct
29 Correct 14 ms 5972 KB Output is correct
30 Correct 24 ms 7000 KB Output is correct
31 Correct 24 ms 7512 KB Output is correct
32 Correct 21 ms 5604 KB Output is correct
33 Correct 20 ms 6228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 2396 KB Output is correct
2 Correct 19 ms 2576 KB Output is correct
3 Correct 20 ms 2396 KB Output is correct
4 Correct 19 ms 2580 KB Output is correct
5 Correct 20 ms 2572 KB Output is correct
6 Correct 19 ms 2652 KB Output is correct
7 Correct 20 ms 2648 KB Output is correct
8 Correct 27 ms 2652 KB Output is correct
9 Correct 30 ms 2940 KB Output is correct
10 Correct 28 ms 2652 KB Output is correct
11 Correct 29 ms 2712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2248 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2400 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 2 ms 2396 KB Output is correct
13 Correct 2 ms 2396 KB Output is correct
14 Correct 2 ms 2396 KB Output is correct
15 Correct 2 ms 2396 KB Output is correct
16 Correct 2 ms 2396 KB Output is correct
17 Correct 2 ms 2404 KB Output is correct
18 Correct 2 ms 2396 KB Output is correct
19 Correct 2 ms 2396 KB Output is correct
20 Correct 2 ms 2652 KB Output is correct
21 Correct 2 ms 2396 KB Output is correct
22 Correct 2 ms 2652 KB Output is correct
23 Correct 18 ms 5452 KB Output is correct
24 Correct 19 ms 5464 KB Output is correct
25 Correct 19 ms 5256 KB Output is correct
26 Correct 18 ms 5464 KB Output is correct
27 Correct 18 ms 5464 KB Output is correct
28 Correct 17 ms 8532 KB Output is correct
29 Correct 14 ms 5972 KB Output is correct
30 Correct 24 ms 7000 KB Output is correct
31 Correct 24 ms 7512 KB Output is correct
32 Correct 21 ms 5604 KB Output is correct
33 Correct 20 ms 6228 KB Output is correct
34 Correct 19 ms 2396 KB Output is correct
35 Correct 19 ms 2576 KB Output is correct
36 Correct 20 ms 2396 KB Output is correct
37 Correct 19 ms 2580 KB Output is correct
38 Correct 20 ms 2572 KB Output is correct
39 Correct 19 ms 2652 KB Output is correct
40 Correct 20 ms 2648 KB Output is correct
41 Correct 27 ms 2652 KB Output is correct
42 Correct 30 ms 2940 KB Output is correct
43 Correct 28 ms 2652 KB Output is correct
44 Correct 29 ms 2712 KB Output is correct
45 Correct 161 ms 5464 KB Output is correct
46 Correct 169 ms 5264 KB Output is correct
47 Correct 163 ms 5712 KB Output is correct
48 Correct 156 ms 5468 KB Output is correct
49 Correct 165 ms 5460 KB Output is correct
50 Correct 264 ms 9804 KB Output is correct
51 Correct 267 ms 9808 KB Output is correct
52 Correct 470 ms 8524 KB Output is correct
53 Correct 443 ms 8272 KB Output is correct
54 Correct 471 ms 8340 KB Output is correct
55 Incorrect 403 ms 10060 KB Output isn't correct
56 Halted 0 ms 0 KB -