Submission #1273040

#TimeUsernameProblemLanguageResultExecution timeMemory
1273040icebearRailway Trip (JOI17_railway_trip)C++20
100 / 100
105 ms17508 KiB
// ~~ icebear ~~
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;

template<class T>
    bool minimize(T &a, const T &b) {
        if (a > b) return a = b, true;
        return false;
    }

template<class T>
    bool maximize(T &a, const T &b) {
        if (a < b) return a = b, true;
        return false;
    }

#define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
#define FORR(i,a,b) for(int i=(a); i>=(b); --i)
#define REP(i, n) for(int i=0; i<(n); ++i)
#define RED(i, n) for(int i=(n)-1; i>=0; --i)
#define MASK(i) (1LL << (i))
#define BIT(S, i) (((S) >> (i)) & 1)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define task "icebear"
#define prev __prev
#define next __next

const int MOD = 1e9 + 7;
const int inf = 1e9 + 27092008;
const ll INF = 1e18 + 27092008;
const int N = 2e5 + 5;
int n, k, q, l[N];
int prev[N][20], next[N][20];

void init(void) {
    cin >> n >> k >> q;
    FOR(i, 1, n) cin >> l[i];
}

void process(void) {
    stack<int> st;
    FOR(i, 1, n) {
        while(!st.empty() && l[st.top()] < l[i]) st.pop();
        prev[i][0] = (st.empty() ? 1 : st.top());
        st.push(i);
    }

    st = stack<int>();
    FORR(i, n, 1) {
        while(!st.empty() && l[st.top()] < l[i]) st.pop();
        next[i][0] = (st.empty() ? n : st.top());
        st.push(i);
    }

    FOR(j, 1, 19) FOR(i, 1, n) {
        int u = prev[i][j - 1], v = next[i][j - 1];
        prev[i][j] = min(prev[u][j - 1], prev[v][j - 1]);
        next[i][j] = max(next[u][j - 1], next[v][j - 1]);
    }

    while(q--) {
        int a, b;
        cin >> a >> b;
        if (a > b) swap(a, b);
        int l_a, r_a, l_b, r_b;
        l_a = r_a = a;
        l_b = r_b = b;

        int res = 0;
        RED(j, 20) if (max(next[l_a][j], next[r_a][j]) < b) {
            res += MASK(j);
            int nextLa = min(prev[l_a][j], prev[r_a][j]);
            int nextRa = max(next[l_a][j], next[r_a][j]);
            l_a = nextLa;
            r_a = nextRa;
        }

        RED(j, 20) if (min(prev[l_b][j], prev[r_b][j]) > r_a) {
            res += MASK(j);
            int nextLb = min(prev[l_b][j], prev[r_b][j]);
            int nextRb = max(next[l_b][j], next[r_b][j]);
            l_b = nextLb;
            r_b = nextRb;
        }

        cout << res << '\n';
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    if (fopen(task".inp", "r")) {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    int tc = 1;
//    cin >> tc;
    while(tc--) {
        init();
        process();
    }
    return 0;
}

Compilation message (stderr)

railway_trip.cpp: In function 'int main()':
railway_trip.cpp:102:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  102 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
railway_trip.cpp:103:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  103 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...