Submission #60815

# Submission time Handle Problem Language Result Execution time Memory
60815 2018-07-24T17:33:56 Z SpaimaCarpatilor Railway Trip (JOI17_railway_trip) C++17
100 / 100
502 ms 40392 KB
#include<bits/stdc++.h>

using namespace std;

int N, K, Q, stk[100009], a[100009], lg[100009];
pair < int, int > dp[17][100009];

void init ()
{
    scanf ("%d %d %d", &N, &K, &Q);
    for (int i=1; i<=N; i++)
        scanf ("%d", &a[i]);
    int nr = 0;
    for (int i=1; i<=N; i++)
    {
        while (nr > 0 && a[stk[nr]] < a[i]) nr --;
        if (nr != 0) dp[0][i].first = stk[nr];
        else dp[0][i].first = i;
        stk[++nr] = i;
    }
    nr = 0;
    for (int i=N; i>=1; i--)
    {
        while (nr > 0 && a[stk[nr]] < a[i]) nr --;
        if (nr != 0) dp[0][i].second = stk[nr];
        else dp[0][i].second = i;
        stk[++nr] = i;
    }
}

void buildDp ()
{
    for (int i=2; i<=N; i++)
        lg[i] = lg[i >> 1] + 1;
    for (int i=1; i<=lg[N]; i++)
        for (int j=1; j<=N; j++)
        {
            auto f = dp[i - 1][j];
            dp[i][j].first = min (dp[i - 1][f.first].first, dp[i - 1][f.second].first);
            dp[i][j].second = max (dp[i - 1][f.first].second, dp[i - 1][f.second].second);
        }
}

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

init ();
buildDp ();
while (Q --)
{
    int L, R, ans = 0;
    scanf ("%d %d", &L, &R);
    if (L > R) swap (L, R);
    int x = L, y = L;
    for (int i=lg[R - L + 1]; i>=0; i--)
        if (max (dp[i][x].second, dp[i][y].second) < R)
        {
            int oldX = x, oldY = y;
            ans += 1 << i;
            x = min (dp[i][oldX].first, dp[i][oldY].first);
            y = max (dp[i][oldX].second, dp[i][oldY].second);
        }
    L = y;
    x = y = R;
    for (int i=lg[R - L + 1]; i>=0; i--)
        if (min (dp[i][x].first, dp[i][y].first) > L)
        {
            int oldX = x, oldY = y;
            ans += 1 << i;
            x = min (dp[i][oldX].first, dp[i][oldY].first);
            y = max (dp[i][oldX].second, dp[i][oldY].second);
        }
    printf ("%d\n", ans);
}

return 0;
}

Compilation message

railway_trip.cpp: In function 'void init()':
railway_trip.cpp:10:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf ("%d %d %d", &N, &K, &Q);
     ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
railway_trip.cpp:12:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf ("%d", &a[i]);
         ~~~~~~^~~~~~~~~~~~~
railway_trip.cpp: In function 'int main()':
railway_trip.cpp:54:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf ("%d %d", &L, &R);
     ~~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 484 KB Output is correct
3 Correct 3 ms 484 KB Output is correct
4 Correct 4 ms 516 KB Output is correct
5 Correct 3 ms 516 KB Output is correct
6 Correct 2 ms 684 KB Output is correct
7 Correct 3 ms 684 KB Output is correct
8 Correct 2 ms 684 KB Output is correct
9 Correct 4 ms 684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 700 KB Output is correct
2 Correct 34 ms 14780 KB Output is correct
3 Correct 43 ms 15128 KB Output is correct
4 Correct 50 ms 15332 KB Output is correct
5 Correct 51 ms 15656 KB Output is correct
6 Correct 48 ms 16128 KB Output is correct
7 Correct 50 ms 16556 KB Output is correct
8 Correct 35 ms 17188 KB Output is correct
9 Correct 45 ms 17564 KB Output is correct
10 Correct 34 ms 17644 KB Output is correct
11 Correct 38 ms 17644 KB Output is correct
12 Correct 36 ms 17644 KB Output is correct
13 Correct 37 ms 17644 KB Output is correct
14 Correct 41 ms 17644 KB Output is correct
15 Correct 36 ms 17644 KB Output is correct
16 Correct 36 ms 17644 KB Output is correct
17 Correct 40 ms 17644 KB Output is correct
18 Correct 34 ms 17644 KB Output is correct
19 Correct 40 ms 17644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 261 ms 17848 KB Output is correct
2 Correct 314 ms 19256 KB Output is correct
3 Correct 310 ms 20612 KB Output is correct
4 Correct 247 ms 22180 KB Output is correct
5 Correct 274 ms 23536 KB Output is correct
6 Correct 296 ms 24872 KB Output is correct
7 Correct 322 ms 26324 KB Output is correct
8 Correct 329 ms 27984 KB Output is correct
9 Correct 464 ms 29460 KB Output is correct
10 Correct 397 ms 30824 KB Output is correct
11 Correct 425 ms 32292 KB Output is correct
12 Correct 433 ms 33588 KB Output is correct
13 Correct 411 ms 34924 KB Output is correct
14 Correct 353 ms 36076 KB Output is correct
15 Correct 251 ms 37052 KB Output is correct
16 Correct 236 ms 37504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 200 ms 37832 KB Output is correct
2 Correct 200 ms 37832 KB Output is correct
3 Correct 268 ms 37832 KB Output is correct
4 Correct 254 ms 37832 KB Output is correct
5 Correct 502 ms 38356 KB Output is correct
6 Correct 306 ms 39236 KB Output is correct
7 Correct 341 ms 40388 KB Output is correct
8 Correct 389 ms 40388 KB Output is correct
9 Correct 353 ms 40388 KB Output is correct
10 Correct 320 ms 40388 KB Output is correct
11 Correct 338 ms 40388 KB Output is correct
12 Correct 353 ms 40388 KB Output is correct
13 Correct 325 ms 40392 KB Output is correct
14 Correct 332 ms 40392 KB Output is correct
15 Correct 381 ms 40392 KB Output is correct
16 Correct 319 ms 40392 KB Output is correct
17 Correct 244 ms 40392 KB Output is correct
18 Correct 313 ms 40392 KB Output is correct
19 Correct 279 ms 40392 KB Output is correct
20 Correct 263 ms 40392 KB Output is correct
21 Correct 246 ms 40392 KB Output is correct
22 Correct 226 ms 40392 KB Output is correct
23 Correct 177 ms 40392 KB Output is correct