Submission #169559

# Submission time Handle Problem Language Result Execution time Memory
169559 2019-12-21T05:07:35 Z combi1k1 Railway Trip (JOI17_railway_trip) C++14
100 / 100
251 ms 18964 KB
#include<bits/stdc++.h>

using namespace std;

#define pb  emplace_back

#define X   first
#define Y   second

const int   N   = 1e5 + 1;

int lef[N][20];
int rig[N][20];

int main()  {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int n;  cin >> n;
    int k;  cin >> k;
    int q;  cin >> q;

    vector<int> a(n);

    for(int&x : a)  cin >> x;

    auto build = [&](bool inv)  {
        stack<int>  st;

        for(int i = 0 ; i < n ; ++i)    {
            while (st.size() && a[st.top()] < a[i])
                st.pop();

            if (st.size())  {
                int u = st.top();

                if (inv)    rig[n - i][0] = n - u;
                else        lef[i + 1][0] = u + 1;
            }

            st.push(i);
        }
        reverse(a.begin(),a.end());
    };

    build(0);
    build(1);   rig[n][0] = n;

    for(int i = 0 ; i < 16 ; ++i)
    for(int j = 1 ; j <= n ; ++j)   {
        int x = lef[j][i];
        int y = rig[j][i];

        lef[j][i + 1] = min(lef[x][i],lef[y][i]);
        rig[j][i + 1] = max(rig[x][i],rig[y][i]);
    }

    for(int i = 0 ; i < q ; ++i)    {
        int x;  cin >> x;
        int y;  cin >> y;

        if (x > y)  x ^= (y ^= (x ^= y));

        int ans = 0;

        int l = x;
        int r = x;

        for(int j = 16 ; j >= 0 ; --j)  if (max(rig[l][j],rig[r][j]) < y)   {
            ans += (1 << j);
            int pl = min(lef[l][j],lef[r][j]);
            int pr = max(rig[l][j],rig[r][j]);

            l = pl;
            r = pr;
        }
        x = r;
        l = y;
        r = y;

        for(int j = 16 ; j >= 0 ; --j)  if (min(lef[l][j],lef[r][j]) > x)   {
            ans += (1 << j);
            int pl = min(lef[l][j],lef[r][j]);
            int pr = max(rig[l][j],rig[r][j]);

            l = pl;
            r = pr;
        }

        cout << ans << "\n";
    }
}
/*
9 3 3
3 1 1 1 2 2 2 3 3
2 4
4 9
6 7
*/
# 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 380 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 42 ms 16504 KB Output is correct
3 Correct 43 ms 16376 KB Output is correct
4 Correct 42 ms 16376 KB Output is correct
5 Correct 43 ms 16376 KB Output is correct
6 Correct 42 ms 16376 KB Output is correct
7 Correct 44 ms 16376 KB Output is correct
8 Correct 47 ms 16984 KB Output is correct
9 Correct 50 ms 17196 KB Output is correct
10 Correct 49 ms 17104 KB Output is correct
11 Correct 47 ms 17244 KB Output is correct
12 Correct 49 ms 17112 KB Output is correct
13 Correct 48 ms 17132 KB Output is correct
14 Correct 51 ms 17116 KB Output is correct
15 Correct 49 ms 17176 KB Output is correct
16 Correct 49 ms 17116 KB Output is correct
17 Correct 45 ms 17016 KB Output is correct
18 Correct 46 ms 17016 KB Output is correct
19 Correct 52 ms 17016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 209 ms 16920 KB Output is correct
2 Correct 132 ms 18296 KB Output is correct
3 Correct 143 ms 18264 KB Output is correct
4 Correct 135 ms 18280 KB Output is correct
5 Correct 138 ms 18332 KB Output is correct
6 Correct 151 ms 18264 KB Output is correct
7 Correct 138 ms 18296 KB Output is correct
8 Correct 172 ms 18560 KB Output is correct
9 Correct 194 ms 18460 KB Output is correct
10 Correct 193 ms 18488 KB Output is correct
11 Correct 203 ms 18488 KB Output is correct
12 Correct 251 ms 18608 KB Output is correct
13 Correct 203 ms 18544 KB Output is correct
14 Correct 188 ms 18400 KB Output is correct
15 Correct 165 ms 18268 KB Output is correct
16 Correct 137 ms 18392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 115 ms 16632 KB Output is correct
2 Correct 114 ms 18424 KB Output is correct
3 Correct 115 ms 18296 KB Output is correct
4 Correct 111 ms 18396 KB Output is correct
5 Correct 202 ms 18488 KB Output is correct
6 Correct 162 ms 18740 KB Output is correct
7 Correct 159 ms 18904 KB Output is correct
8 Correct 175 ms 18608 KB Output is correct
9 Correct 158 ms 18748 KB Output is correct
10 Correct 146 ms 18748 KB Output is correct
11 Correct 147 ms 18876 KB Output is correct
12 Correct 147 ms 18852 KB Output is correct
13 Correct 148 ms 18748 KB Output is correct
14 Correct 176 ms 18868 KB Output is correct
15 Correct 158 ms 18880 KB Output is correct
16 Correct 160 ms 18928 KB Output is correct
17 Correct 167 ms 18856 KB Output is correct
18 Correct 154 ms 18756 KB Output is correct
19 Correct 160 ms 18964 KB Output is correct
20 Correct 146 ms 18552 KB Output is correct
21 Correct 119 ms 18456 KB Output is correct
22 Correct 116 ms 18396 KB Output is correct
23 Correct 110 ms 18424 KB Output is correct