Submission #47011

# Submission time Handle Problem Language Result Execution time Memory
47011 2018-04-26T10:42:02 Z SpaimaCarpatilor Abduction 2 (JOI17_abduction2) C++17
100 / 100
2244 ms 109272 KB
#include<bits/stdc++.h>

using namespace std;

int H, W, Q, L[50009], C[50009], lg[50009], rmqL[16][50009], rmqC[16][50009];
unordered_map < long long, long long > 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);
}

long long solve (int i, int j)
{
    long long currPos = 1LL * i * W + j;
    if (mp.count (currPos))
        return mp[currPos];
    long long ans = 0;
    if (L[i] > C[j])
    {
        int j1, j2;
        getJ1J2 (j, j1, j2, L[i]);

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

        if (j2 == W + 1) ans = max (ans, (long long) W - j);
        else
        {
            long long 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, (long long) i - 1);
        else
        {
            long long curr = solve (i1, j) + i - i1;
            ans = max (ans, curr);
        }

        if (i2 == H + 1) ans = max (ans, (long long) H - i);
        else
        {
            long long curr = solve (i2, j) + i2 - i;
            ans = max (ans, curr);
        }
    }
    mp[currPos] = 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;
    long long ans = 0;
    scanf ("%d %d", &x, &y);
    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])
            {
                long long curr = solve (i, j) + steps;
                if (curr > ans)
                    ans = curr;
                break;
            }
            if (steps > ans)
                ans = steps;
            steps ++, i += dx[k], j += dy[k];
        }
    }
    printf ("%lld\n", ans);
}

return 0;
}

Compilation message

abduction2.cpp: In function 'void read()':
abduction2.cpp:80: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:82: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:84: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:117:11: 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 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 428 KB Output is correct
4 Correct 2 ms 480 KB Output is correct
5 Correct 2 ms 488 KB Output is correct
6 Correct 2 ms 488 KB Output is correct
7 Correct 2 ms 672 KB Output is correct
8 Correct 2 ms 672 KB Output is correct
9 Correct 2 ms 672 KB Output is correct
10 Correct 2 ms 672 KB Output is correct
11 Correct 2 ms 672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 428 KB Output is correct
4 Correct 2 ms 480 KB Output is correct
5 Correct 2 ms 488 KB Output is correct
6 Correct 2 ms 488 KB Output is correct
7 Correct 2 ms 672 KB Output is correct
8 Correct 2 ms 672 KB Output is correct
9 Correct 2 ms 672 KB Output is correct
10 Correct 2 ms 672 KB Output is correct
11 Correct 2 ms 672 KB Output is correct
12 Correct 3 ms 876 KB Output is correct
13 Correct 3 ms 876 KB Output is correct
14 Correct 3 ms 876 KB Output is correct
15 Correct 4 ms 892 KB Output is correct
16 Correct 3 ms 892 KB Output is correct
17 Correct 3 ms 892 KB Output is correct
18 Correct 3 ms 892 KB Output is correct
19 Correct 5 ms 1020 KB Output is correct
20 Correct 4 ms 1148 KB Output is correct
21 Correct 4 ms 1148 KB Output is correct
22 Correct 4 ms 1276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 428 KB Output is correct
4 Correct 2 ms 480 KB Output is correct
5 Correct 2 ms 488 KB Output is correct
6 Correct 2 ms 488 KB Output is correct
7 Correct 2 ms 672 KB Output is correct
8 Correct 2 ms 672 KB Output is correct
9 Correct 2 ms 672 KB Output is correct
10 Correct 2 ms 672 KB Output is correct
11 Correct 2 ms 672 KB Output is correct
12 Correct 3 ms 876 KB Output is correct
13 Correct 3 ms 876 KB Output is correct
14 Correct 3 ms 876 KB Output is correct
15 Correct 4 ms 892 KB Output is correct
16 Correct 3 ms 892 KB Output is correct
17 Correct 3 ms 892 KB Output is correct
18 Correct 3 ms 892 KB Output is correct
19 Correct 5 ms 1020 KB Output is correct
20 Correct 4 ms 1148 KB Output is correct
21 Correct 4 ms 1148 KB Output is correct
22 Correct 4 ms 1276 KB Output is correct
23 Correct 29 ms 6912 KB Output is correct
24 Correct 27 ms 6912 KB Output is correct
25 Correct 44 ms 6912 KB Output is correct
26 Correct 39 ms 6912 KB Output is correct
27 Correct 28 ms 6912 KB Output is correct
28 Correct 41 ms 11516 KB Output is correct
29 Correct 26 ms 11516 KB Output is correct
30 Correct 86 ms 11780 KB Output is correct
31 Correct 92 ms 12868 KB Output is correct
32 Correct 26 ms 12868 KB Output is correct
33 Correct 36 ms 12868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 12868 KB Output is correct
2 Correct 4 ms 12868 KB Output is correct
3 Correct 4 ms 12868 KB Output is correct
4 Correct 5 ms 12868 KB Output is correct
5 Correct 4 ms 12868 KB Output is correct
6 Correct 4 ms 12868 KB Output is correct
7 Correct 4 ms 12868 KB Output is correct
8 Correct 5 ms 12868 KB Output is correct
9 Correct 7 ms 12868 KB Output is correct
10 Correct 5 ms 12868 KB Output is correct
11 Correct 9 ms 12868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 428 KB Output is correct
4 Correct 2 ms 480 KB Output is correct
5 Correct 2 ms 488 KB Output is correct
6 Correct 2 ms 488 KB Output is correct
7 Correct 2 ms 672 KB Output is correct
8 Correct 2 ms 672 KB Output is correct
9 Correct 2 ms 672 KB Output is correct
10 Correct 2 ms 672 KB Output is correct
11 Correct 2 ms 672 KB Output is correct
12 Correct 3 ms 876 KB Output is correct
13 Correct 3 ms 876 KB Output is correct
14 Correct 3 ms 876 KB Output is correct
15 Correct 4 ms 892 KB Output is correct
16 Correct 3 ms 892 KB Output is correct
17 Correct 3 ms 892 KB Output is correct
18 Correct 3 ms 892 KB Output is correct
19 Correct 5 ms 1020 KB Output is correct
20 Correct 4 ms 1148 KB Output is correct
21 Correct 4 ms 1148 KB Output is correct
22 Correct 4 ms 1276 KB Output is correct
23 Correct 29 ms 6912 KB Output is correct
24 Correct 27 ms 6912 KB Output is correct
25 Correct 44 ms 6912 KB Output is correct
26 Correct 39 ms 6912 KB Output is correct
27 Correct 28 ms 6912 KB Output is correct
28 Correct 41 ms 11516 KB Output is correct
29 Correct 26 ms 11516 KB Output is correct
30 Correct 86 ms 11780 KB Output is correct
31 Correct 92 ms 12868 KB Output is correct
32 Correct 26 ms 12868 KB Output is correct
33 Correct 36 ms 12868 KB Output is correct
34 Correct 4 ms 12868 KB Output is correct
35 Correct 4 ms 12868 KB Output is correct
36 Correct 4 ms 12868 KB Output is correct
37 Correct 5 ms 12868 KB Output is correct
38 Correct 4 ms 12868 KB Output is correct
39 Correct 4 ms 12868 KB Output is correct
40 Correct 4 ms 12868 KB Output is correct
41 Correct 5 ms 12868 KB Output is correct
42 Correct 7 ms 12868 KB Output is correct
43 Correct 5 ms 12868 KB Output is correct
44 Correct 9 ms 12868 KB Output is correct
45 Correct 47 ms 12868 KB Output is correct
46 Correct 28 ms 12868 KB Output is correct
47 Correct 32 ms 12868 KB Output is correct
48 Correct 29 ms 12868 KB Output is correct
49 Correct 30 ms 12868 KB Output is correct
50 Correct 77 ms 12868 KB Output is correct
51 Correct 55 ms 12868 KB Output is correct
52 Correct 126 ms 14396 KB Output is correct
53 Correct 127 ms 14396 KB Output is correct
54 Correct 115 ms 14396 KB Output is correct
55 Correct 147 ms 17064 KB Output is correct
56 Correct 2244 ms 109272 KB Output is correct
57 Correct 514 ms 109272 KB Output is correct
58 Correct 434 ms 109272 KB Output is correct
59 Correct 435 ms 109272 KB Output is correct