Submission #47010

# Submission time Handle Problem Language Result Execution time Memory
47010 2018-04-26T10:39:41 Z SpaimaCarpatilor Abduction 2 (JOI17_abduction2) C++17
44 / 100
193 ms 16700 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, 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)
{
    long long currPos = 1LL * i * W + j;
    if (mp.count (currPos))
        return mp[currPos];
    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[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, 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])
            {
                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: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:116: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 408 KB Output is correct
4 Correct 2 ms 408 KB Output is correct
5 Correct 2 ms 536 KB Output is correct
6 Correct 2 ms 536 KB Output is correct
7 Correct 2 ms 628 KB Output is correct
8 Correct 2 ms 700 KB Output is correct
9 Correct 2 ms 700 KB Output is correct
10 Correct 2 ms 700 KB Output is correct
11 Correct 2 ms 700 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 408 KB Output is correct
4 Correct 2 ms 408 KB Output is correct
5 Correct 2 ms 536 KB Output is correct
6 Correct 2 ms 536 KB Output is correct
7 Correct 2 ms 628 KB Output is correct
8 Correct 2 ms 700 KB Output is correct
9 Correct 2 ms 700 KB Output is correct
10 Correct 2 ms 700 KB Output is correct
11 Correct 2 ms 700 KB Output is correct
12 Correct 3 ms 848 KB Output is correct
13 Correct 4 ms 848 KB Output is correct
14 Correct 3 ms 892 KB Output is correct
15 Correct 3 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 4 ms 1020 KB Output is correct
20 Correct 4 ms 1020 KB Output is correct
21 Correct 6 ms 1020 KB Output is correct
22 Correct 5 ms 1148 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 408 KB Output is correct
4 Correct 2 ms 408 KB Output is correct
5 Correct 2 ms 536 KB Output is correct
6 Correct 2 ms 536 KB Output is correct
7 Correct 2 ms 628 KB Output is correct
8 Correct 2 ms 700 KB Output is correct
9 Correct 2 ms 700 KB Output is correct
10 Correct 2 ms 700 KB Output is correct
11 Correct 2 ms 700 KB Output is correct
12 Correct 3 ms 848 KB Output is correct
13 Correct 4 ms 848 KB Output is correct
14 Correct 3 ms 892 KB Output is correct
15 Correct 3 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 4 ms 1020 KB Output is correct
20 Correct 4 ms 1020 KB Output is correct
21 Correct 6 ms 1020 KB Output is correct
22 Correct 5 ms 1148 KB Output is correct
23 Correct 27 ms 6908 KB Output is correct
24 Correct 33 ms 6908 KB Output is correct
25 Correct 27 ms 6908 KB Output is correct
26 Correct 36 ms 6964 KB Output is correct
27 Correct 43 ms 6964 KB Output is correct
28 Correct 41 ms 11052 KB Output is correct
29 Correct 27 ms 11052 KB Output is correct
30 Correct 63 ms 11532 KB Output is correct
31 Correct 93 ms 12624 KB Output is correct
32 Correct 26 ms 12624 KB Output is correct
33 Correct 53 ms 12624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 12624 KB Output is correct
2 Correct 4 ms 12624 KB Output is correct
3 Correct 6 ms 12624 KB Output is correct
4 Correct 4 ms 12624 KB Output is correct
5 Correct 6 ms 12624 KB Output is correct
6 Correct 9 ms 12624 KB Output is correct
7 Correct 5 ms 12624 KB Output is correct
8 Correct 6 ms 12624 KB Output is correct
9 Correct 6 ms 12624 KB Output is correct
10 Correct 6 ms 12624 KB Output is correct
11 Correct 8 ms 12624 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 408 KB Output is correct
4 Correct 2 ms 408 KB Output is correct
5 Correct 2 ms 536 KB Output is correct
6 Correct 2 ms 536 KB Output is correct
7 Correct 2 ms 628 KB Output is correct
8 Correct 2 ms 700 KB Output is correct
9 Correct 2 ms 700 KB Output is correct
10 Correct 2 ms 700 KB Output is correct
11 Correct 2 ms 700 KB Output is correct
12 Correct 3 ms 848 KB Output is correct
13 Correct 4 ms 848 KB Output is correct
14 Correct 3 ms 892 KB Output is correct
15 Correct 3 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 4 ms 1020 KB Output is correct
20 Correct 4 ms 1020 KB Output is correct
21 Correct 6 ms 1020 KB Output is correct
22 Correct 5 ms 1148 KB Output is correct
23 Correct 27 ms 6908 KB Output is correct
24 Correct 33 ms 6908 KB Output is correct
25 Correct 27 ms 6908 KB Output is correct
26 Correct 36 ms 6964 KB Output is correct
27 Correct 43 ms 6964 KB Output is correct
28 Correct 41 ms 11052 KB Output is correct
29 Correct 27 ms 11052 KB Output is correct
30 Correct 63 ms 11532 KB Output is correct
31 Correct 93 ms 12624 KB Output is correct
32 Correct 26 ms 12624 KB Output is correct
33 Correct 53 ms 12624 KB Output is correct
34 Correct 4 ms 12624 KB Output is correct
35 Correct 4 ms 12624 KB Output is correct
36 Correct 6 ms 12624 KB Output is correct
37 Correct 4 ms 12624 KB Output is correct
38 Correct 6 ms 12624 KB Output is correct
39 Correct 9 ms 12624 KB Output is correct
40 Correct 5 ms 12624 KB Output is correct
41 Correct 6 ms 12624 KB Output is correct
42 Correct 6 ms 12624 KB Output is correct
43 Correct 6 ms 12624 KB Output is correct
44 Correct 8 ms 12624 KB Output is correct
45 Correct 29 ms 12624 KB Output is correct
46 Correct 38 ms 12624 KB Output is correct
47 Correct 31 ms 12624 KB Output is correct
48 Correct 42 ms 12624 KB Output is correct
49 Correct 32 ms 12624 KB Output is correct
50 Correct 52 ms 12624 KB Output is correct
51 Correct 64 ms 12624 KB Output is correct
52 Correct 126 ms 14268 KB Output is correct
53 Correct 127 ms 14268 KB Output is correct
54 Correct 115 ms 14268 KB Output is correct
55 Incorrect 193 ms 16700 KB Output isn't correct
56 Halted 0 ms 0 KB -