Submission #125491

#TimeUsernameProblemLanguageResultExecution timeMemory
125491choikiwonRailway Trip (JOI17_railway_trip)C++17
0 / 100
395 ms23544 KiB
#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); l = min(le[i][l], le[i][r]); r = max(ri[i][l], ri[i][r]); } } } else { int l = u; int r = u; for(int i = 20; i--;) { if(min(le[i][l], le[i][r]) > v) { ret += (1 << i); l = min(le[i][l], le[i][r]); r = max(ri[i][l], ri[i][r]); } } } 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 ans = 1e9; if(le2[i] != -1 && ri2[i] != -1) { ans = min(ans, calc(a, le2[i]) + calc(b, ri2[i]) + 1); } int t = lower_bound(V[x].begin(), V[x].end(), a) - V[x].begin(); ans = min(ans, calc(a, V[x][t]) + calc(b, V[x][t])); printf("%d\n", ans - 1); } }

Compilation message (stderr)

railway_trip.cpp: In function 'int main()':
railway_trip.cpp:69: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:72: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:121: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...