답안 #125500

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
125500 2019-07-05T13:45:32 Z choikiwon Railway Trip (JOI17_railway_trip) C++17
0 / 100
350 ms 23436 KB
#include<bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;

const int MN = 100010;

int N, K, Q;
int L[MN];
vector<int> V[MN];
int le[20][MN], ri[20][MN];
int le2[MN], ri2[MN];

struct BIT {
    vector<int> tree;
    void init() {
        tree = vector<int>(4 * N);
        build(0, N - 1, 1);
    }
    void build(int l, int r, int n) {
        if(l == r) {
            tree[n] = L[l];
            return;
        }
        int m = (l + r)>>1;
        build(l, m, 2*n);
        build(m + 1, r, 2*n + 1);
        tree[n] = max(tree[2*n], tree[2*n + 1]);
    }
    int quer(int a, int b, int l, int r, int n) {
        if(b < l || r < a) return -1e9;
        if(a <= l && r <= b) return tree[n];
        int m = (l + r)>>1;
        int L = quer(a, b, l, m, 2*n);
        int R = quer(a, b, m + 1, r, 2*n + 1);
        return max(L, R);
    }
} bit;

int calc(int u, int v) {
    if(u == v) return 0;
    int ret = 0;
    if(u < v) {
        int l = u;
        int r = u;
        for(int i = 20; i--;) {
            if(max(ri[i][l], ri[i][r]) < v) {
                ret += (1 << i);
                int nl = min(le[i][l], le[i][r]);
                int nr = max(ri[i][l], ri[i][r]);
                l = nl;
                r = nr;
            }
        }
    }
    else {
        int l = u;
        int r = u;
        for(int i = 20; i--;) {
            if(min(le[i][l], le[i][r]) > v) {
                ret += (1 << i);
                int nl = min(le[i][l], le[i][r]);
                int nr = max(ri[i][l], ri[i][r]);
                l = nl;
                r = nr;
            }
        }
    }
    return ret + 1;
}

int main() {
    scanf("%d %d %d", &N, &K, &Q);

    for(int i = 0; i < N; i++) {
        scanf("%d", &L[i]);
        V[ --L[i] ].push_back(i);
    }

    memset(le2, -1, sizeof(le2));
    memset(ri2, -1, sizeof(ri2));

    stack<pii> stk;
    for(int i = 0; i < N; i++) {
        while(!stk.empty() && stk.top().first < L[i]) stk.pop();
        if(!stk.empty()) le[0][i] = stk.top().second;
        stk.push(pii(L[i], i));
    }
    while(!stk.empty()) stk.pop();

    for(int i = N - 1; i >= 0; i--) {
        while(!stk.empty() && stk.top().first < L[i]) stk.pop();
        if(!stk.empty()) ri[0][i] = stk.top().second;
        stk.push(pii(L[i], i));
    }
    while(!stk.empty()) stk.pop();

    for(int i = 0; i < N; i++) {
        while(!stk.empty() && stk.top().first <= L[i]) stk.pop();
        if(!stk.empty()) le2[i] = stk.top().second;
        stk.push(pii(L[i], i));
    }
    while(!stk.empty()) stk.pop();

    for(int i = N - 1; i >= 0; i--) {
        while(!stk.empty() && stk.top().first <= L[i]) stk.pop();
        if(!stk.empty()) ri2[i] = stk.top().second;
        stk.push(pii(L[i], i));
    }
    while(!stk.empty()) stk.pop();

    le[0][0] = 0;
    ri[0][N - 1] = N - 1;
    for(int i = 1; i < 20; i++) {
        for(int j = 0; j < N; j++) {
            int t = le[i - 1][j];
            int d = ri[i - 1][j];
            le[i][j] = min(le[i - 1][t], le[i - 1][d]);
            ri[i][j] = max(ri[i - 1][t], ri[i - 1][d]);
        }
    }

    bit.init();
    for(int i = 0; i < Q; i++) {
        int a, b; scanf("%d %d", &a, &b);
        a--; b--;
        if(a > b) swap(a, b);

        int x = bit.quer(a, b, 0, N - 1, 1);
        int t = lower_bound(V[x].begin(), V[x].end(), a) - V[x].begin();
        
        int ans = 1e9;
        if(le2[t] != -1 && ri2[t] != -1) {
            ans = min(ans, calc(a, le2[t]) + calc(b, ri2[t]) + 1);
        }

        ans = min(ans, calc(a, V[x][t]) + calc(b, V[x][t]));

        printf("%d\n", ans - 1);
    }
}

Compilation message

railway_trip.cpp: In function 'int main()':
railway_trip.cpp:73:10: 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:76:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &L[i]);
         ~~~~~^~~~~~~~~~~~~
railway_trip.cpp:125:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         int a, b; scanf("%d %d", &a, &b);
                   ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 3576 KB Output is correct
2 Incorrect 5 ms 3704 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 3832 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 350 ms 22032 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 269 ms 23436 KB Output isn't correct
2 Halted 0 ms 0 KB -