Submission #125506

#TimeUsernameProblemLanguageResultExecution timeMemory
125506choikiwonRailway Trip (JOI17_railway_trip)C++17
100 / 100
273 ms18608 KiB
#include<bits/stdc++.h>
using namespace std;
 
typedef pair<int, int> pii;
 
const int MN = 100010;
 
int N, K, Q;
int L[MN];
int le[20][MN], ri[20][MN];
 
int main() {
    scanf("%d %d %d", &N, &K, &Q);
 
    for(int i = 0; i < N; i++) {
        scanf("%d", &L[i]);
        L[i]--;
    }
 
    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));
    }
 
    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]);
        }
    }
 
    for(int i = 0; i < Q; i++) {
        int a, b; scanf("%d %d", &a, &b);
        a--; b--;
        if(a > b) swap(a, b);
 
        int ans = 0;
        int l = a;
        int r = a;
        for(int j = 20; j--;) {
            if(max(ri[j][l], ri[j][r]) < b) {
                ans += (1 << j);
                int nl = min(le[j][l], le[j][r]);
                int nr = max(ri[j][l], ri[j][r]);
                l = nl;
                r = nr;
            }
        }
 
        a = r;
        l = b;
        r = b;
        for(int j = 20; j--;) {
            if(min(le[j][l], le[j][r]) > a) {
                ans += (1 << j);
                int nl = min(le[j][l], le[j][r]);
                int nr = max(ri[j][l], ri[j][r]);
                l = nl;
                r = nr;
            }
        }
        printf("%d\n", ans);
    }
}

Compilation message (stderr)

railway_trip.cpp: In function 'int main()':
railway_trip.cpp:13: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:16: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:47: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);
                   ~~~~~^~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...