답안 #47008

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
47008 2018-04-26T10:35:30 Z SpaimaCarpatilor 유괴 2 (JOI17_abduction2) C++17
44 / 100
5000 ms 46532 KB
#include<bits/stdc++.h>

using namespace std;

int H, W, Q, L[50009], C[50009], lg[50009], rmqL[16][50009], rmqC[16][50009];
map < pair < int, int >, int > mp;

void getJ1J2 (int j, int &j1, int &j2, int minVal)
{
    j1 = j, j2 = j;
    for (int k=lg[j]; k>=0; k--)
        if (j1 - (1 << k) + 1 >= 1 && rmqC[k][j1 - (1 << k) + 1] <= minVal)
            j1 -= (1 << k);
    for (int k=lg[W - j + 1]; k>=0; k--)
        if (j2 + (1 << k) - 1 <= W && rmqC[k][j2] <= minVal)
            j2 += (1 << k);
}

void getI1I2 (int i, int &i1, int &i2, int minVal)
{
    i1 = i, i2 = i;
    for (int k=lg[i]; k>=0; k--)
        if (i1 - (1 << k) + 1 >= 1 && rmqL[k][i1 - (1 << k) + 1] <= minVal)
            i1 -= (1 << k);
    for (int k=lg[H - i + 1]; k>=0; k--)
        if (i2 + (1 << k) - 1 <= H && rmqL[k][i2] <= minVal)
            i2 += (1 << k);
}

int solve (int i, int j)
{
    if (mp.count ({i, j}))
        return mp[{i, j}];
    int ans = 0;
    if (L[i] > C[j])
    {
        int j1, j2;
        getJ1J2 (j, j1, j2, L[i]);

        if (j1 == 0) ans = max (ans, j - 1);
        else
        {
            int curr = solve (i, j1) + j - j1;
            ans = max (ans, curr);
        }

        if (j2 == W + 1) ans = max (ans, W - j);
        else
        {
            int curr = solve (i, j2) + j2 - j;
            ans = max (ans, curr);
        }
    }
    else
    {
        int i1, i2;
        getI1I2 (i, i1, i2, C[j]);

        if (i1 == 0) ans = max (ans, i - 1);
        else
        {
            int curr = solve (i1, j) + i - i1;
            ans = max (ans, curr);
        }

        if (i2 == H + 1) ans = max (ans, H - i);
        else
        {
            int curr = solve (i2, j) + i2 - i;
            ans = max (ans, curr);
        }
    }
    mp[{i, j}] = ans;
    return ans;
}

void read ()
{
    scanf ("%d %d %d", &H, &W, &Q);
    for (int i=1; i<=H; i++)
        scanf ("%d", &L[i]), rmqL[0][i] = L[i];
    for (int j=1; j<=W; j++)
        scanf ("%d", &C[j]), rmqC[0][j] = C[j];
    for (int i=1; i<=max (H, W); i++)
    {
        lg[i] = lg[i - 1];
        if ((2 << lg[i]) <= i) lg[i] ++;
    }
    for (int i=1; i<=lg[H]; i++)
        for (int j=1; j + (1 << i) - 1 <= H; j++)
            rmqL[i][j] = max (rmqL[i - 1][j], rmqL[i - 1][j + (1 << (i - 1))]);
    for (int i=1; i<=lg[W]; i++)
        for (int j=1; j + (1 << i) - 1 <= W; j++)
            rmqC[i][j] = max (rmqC[i - 1][j], rmqC[i - 1][j + (1 << (i - 1))]);
}

int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
int oh[] = {0, 0, 1, 1};

bool valid (int i, int j)
{
    return (1 <= i && i <= H && 1 <= j && j <= W);
}

int main ()
{
//freopen ("input", "r", stdin);
//freopen ("output", "w", stdout);

read ();
while (Q --)
{
    int x, y, ans = 0;
    scanf ("%d %d", &x, &y);
    mp.clear ();
    for (int k=0; k<4; k++)
    {
        int i = x + dx[k], j = y + dy[k], steps = 1;
        while (1)
        {
            if (!valid (i, j)) break;
            if ((L[i] > C[j]) != oh[k])
            {
                int curr = solve (i, j) + steps;
                if (curr > ans)
                    ans = curr;
                break;
            }
            if (steps > ans)
                ans = steps;
            steps ++, i += dx[k], j += dy[k];
        }
    }
    printf ("%d\n", ans);
}

return 0;
}

Compilation message

abduction2.cpp: In function 'void read()':
abduction2.cpp:79:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf ("%d %d %d", &H, &W, &Q);
     ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
abduction2.cpp:81:28: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf ("%d", &L[i]), rmqL[0][i] = L[i];
         ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
abduction2.cpp:83:28: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf ("%d", &C[j]), rmqC[0][j] = C[j];
         ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
abduction2.cpp: In function 'int main()':
abduction2.cpp:115:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf ("%d %d", &x, &y);
     ~~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 488 KB Output is correct
3 Correct 2 ms 600 KB Output is correct
4 Correct 2 ms 668 KB Output is correct
5 Correct 2 ms 668 KB Output is correct
6 Correct 2 ms 708 KB Output is correct
7 Correct 2 ms 708 KB Output is correct
8 Correct 2 ms 708 KB Output is correct
9 Correct 2 ms 744 KB Output is correct
10 Correct 2 ms 752 KB Output is correct
11 Correct 2 ms 776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 488 KB Output is correct
3 Correct 2 ms 600 KB Output is correct
4 Correct 2 ms 668 KB Output is correct
5 Correct 2 ms 668 KB Output is correct
6 Correct 2 ms 708 KB Output is correct
7 Correct 2 ms 708 KB Output is correct
8 Correct 2 ms 708 KB Output is correct
9 Correct 2 ms 744 KB Output is correct
10 Correct 2 ms 752 KB Output is correct
11 Correct 2 ms 776 KB Output is correct
12 Correct 21 ms 1032 KB Output is correct
13 Correct 3 ms 1072 KB Output is correct
14 Correct 3 ms 1128 KB Output is correct
15 Correct 3 ms 1184 KB Output is correct
16 Correct 3 ms 1288 KB Output is correct
17 Correct 3 ms 1344 KB Output is correct
18 Correct 3 ms 1400 KB Output is correct
19 Correct 4 ms 1696 KB Output is correct
20 Correct 5 ms 1696 KB Output is correct
21 Correct 5 ms 1716 KB Output is correct
22 Correct 6 ms 2012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 488 KB Output is correct
3 Correct 2 ms 600 KB Output is correct
4 Correct 2 ms 668 KB Output is correct
5 Correct 2 ms 668 KB Output is correct
6 Correct 2 ms 708 KB Output is correct
7 Correct 2 ms 708 KB Output is correct
8 Correct 2 ms 708 KB Output is correct
9 Correct 2 ms 744 KB Output is correct
10 Correct 2 ms 752 KB Output is correct
11 Correct 2 ms 776 KB Output is correct
12 Correct 21 ms 1032 KB Output is correct
13 Correct 3 ms 1072 KB Output is correct
14 Correct 3 ms 1128 KB Output is correct
15 Correct 3 ms 1184 KB Output is correct
16 Correct 3 ms 1288 KB Output is correct
17 Correct 3 ms 1344 KB Output is correct
18 Correct 3 ms 1400 KB Output is correct
19 Correct 4 ms 1696 KB Output is correct
20 Correct 5 ms 1696 KB Output is correct
21 Correct 5 ms 1716 KB Output is correct
22 Correct 6 ms 2012 KB Output is correct
23 Correct 27 ms 8704 KB Output is correct
24 Correct 28 ms 9668 KB Output is correct
25 Correct 30 ms 10548 KB Output is correct
26 Correct 38 ms 11416 KB Output is correct
27 Correct 27 ms 12496 KB Output is correct
28 Correct 44 ms 18576 KB Output is correct
29 Correct 27 ms 18576 KB Output is correct
30 Correct 86 ms 21604 KB Output is correct
31 Correct 105 ms 24384 KB Output is correct
32 Correct 29 ms 24384 KB Output is correct
33 Correct 41 ms 24384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 24384 KB Output is correct
2 Correct 5 ms 24384 KB Output is correct
3 Correct 5 ms 24384 KB Output is correct
4 Correct 5 ms 24384 KB Output is correct
5 Correct 5 ms 24384 KB Output is correct
6 Correct 19 ms 24384 KB Output is correct
7 Correct 18 ms 24384 KB Output is correct
8 Correct 109 ms 24384 KB Output is correct
9 Correct 119 ms 24384 KB Output is correct
10 Correct 135 ms 24384 KB Output is correct
11 Correct 169 ms 24384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 488 KB Output is correct
3 Correct 2 ms 600 KB Output is correct
4 Correct 2 ms 668 KB Output is correct
5 Correct 2 ms 668 KB Output is correct
6 Correct 2 ms 708 KB Output is correct
7 Correct 2 ms 708 KB Output is correct
8 Correct 2 ms 708 KB Output is correct
9 Correct 2 ms 744 KB Output is correct
10 Correct 2 ms 752 KB Output is correct
11 Correct 2 ms 776 KB Output is correct
12 Correct 21 ms 1032 KB Output is correct
13 Correct 3 ms 1072 KB Output is correct
14 Correct 3 ms 1128 KB Output is correct
15 Correct 3 ms 1184 KB Output is correct
16 Correct 3 ms 1288 KB Output is correct
17 Correct 3 ms 1344 KB Output is correct
18 Correct 3 ms 1400 KB Output is correct
19 Correct 4 ms 1696 KB Output is correct
20 Correct 5 ms 1696 KB Output is correct
21 Correct 5 ms 1716 KB Output is correct
22 Correct 6 ms 2012 KB Output is correct
23 Correct 27 ms 8704 KB Output is correct
24 Correct 28 ms 9668 KB Output is correct
25 Correct 30 ms 10548 KB Output is correct
26 Correct 38 ms 11416 KB Output is correct
27 Correct 27 ms 12496 KB Output is correct
28 Correct 44 ms 18576 KB Output is correct
29 Correct 27 ms 18576 KB Output is correct
30 Correct 86 ms 21604 KB Output is correct
31 Correct 105 ms 24384 KB Output is correct
32 Correct 29 ms 24384 KB Output is correct
33 Correct 41 ms 24384 KB Output is correct
34 Correct 7 ms 24384 KB Output is correct
35 Correct 5 ms 24384 KB Output is correct
36 Correct 5 ms 24384 KB Output is correct
37 Correct 5 ms 24384 KB Output is correct
38 Correct 5 ms 24384 KB Output is correct
39 Correct 19 ms 24384 KB Output is correct
40 Correct 18 ms 24384 KB Output is correct
41 Correct 109 ms 24384 KB Output is correct
42 Correct 119 ms 24384 KB Output is correct
43 Correct 135 ms 24384 KB Output is correct
44 Correct 169 ms 24384 KB Output is correct
45 Correct 31 ms 24384 KB Output is correct
46 Correct 31 ms 24384 KB Output is correct
47 Correct 30 ms 24384 KB Output is correct
48 Correct 31 ms 24384 KB Output is correct
49 Correct 31 ms 24384 KB Output is correct
50 Correct 646 ms 31572 KB Output is correct
51 Correct 552 ms 32884 KB Output is correct
52 Correct 4914 ms 38560 KB Output is correct
53 Correct 4888 ms 38936 KB Output is correct
54 Correct 4965 ms 39236 KB Output is correct
55 Execution timed out 5025 ms 46532 KB Time limit exceeded
56 Halted 0 ms 0 KB -