| # | Time | Username | Problem | Language | Result | Execution time | Memory | 
|---|---|---|---|---|---|---|---|
| 290675 | maximath_1 | Railway Trip (JOI17_railway_trip) | C++11 | 226 ms | 17272 KiB | 
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <array>
#include <vector>
#include <algorithm>
#include <string.h>
#include <set>
#include <math.h>
#include <numeric>
#include <assert.h>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <bitset>
#include <queue>
using namespace std;
#define ll long long
#define ld long double
#define endl "\n"
const ll inf = 1e9 + 69;
const ld pi = 3.14159265358979323L;
const ld eps = 1e-15;
const int N = 1e5 + 5;
void setIn(string s){freopen(s.c_str(), "r", stdin);}
void setOut(string s){freopen(s.c_str(), "w", stdout);}
void unsyncIO(){cin.tie(0) -> sync_with_stdio(0);}
void setIO(string s = ""){
	unsyncIO();
	if(s.size()){
		setIn(s + ".in");
		setOut(s + ".out");
	}
}
int n, k, q, v[100005];
int ancL[100005][18], ancR[100005][18];
int main(){
	setIO();
	cin >> n >> k >> q;
	for(int i = 1; i <= n; i ++) cin >> v[i];
	for(int i = 1; i <= n; i ++){
		ancL[i][0] = i - 1;
		while(ancL[i][0] >= 1 && v[ancL[i][0]] < v[i])
			ancL[i][0] = ancL[ancL[i][0]][0];
	}
	ancL[0][0] = 1;
	for(int i = n; i >= 1; i --){
		ancR[i][0] = i + 1;
		while(ancR[i][0] <= n && v[ancR[i][0]] < v[i])
			ancR[i][0] = ancR[ancR[i][0]][0];
	}
	ancR[n][0] = n;
	for(int j = 1; j < 18; j ++){
		for(int i = 1; i <= n; i ++){
			ancL[i][j] = min(ancL[ancL[i][j - 1]][j - 1], ancL[ancR[i][j - 1]][j - 1]);
			ancR[i][j] = max(ancR[ancL[i][j - 1]][j - 1], ancR[ancR[i][j - 1]][j - 1]);
		}
	}
	for(int u, v; q --;){
		cin >> u >> v; if(u > v) swap(u, v);
		int ans = 0, c;
		int l = u, r = u;
		for(int i = 17; i >= 0; i --){
			if(max(ancR[l][i], ancR[r][i]) < v){
				int tl = min(ancL[l][i], ancL[r][i]);
				int tr = max(ancR[l][i], ancR[r][i]);
				l = tl, r = tr;
				ans += (1 << i);
			}
		}
		c = r;
		l = v, r = v;
		for(int i = 17; i >= 0; i --){
			if(min(ancL[l][i], ancL[r][i]) > c){
				int tl = min(ancL[l][i], ancL[r][i]);
				int tr = max(ancR[l][i], ancR[r][i]);
				l = tl, r = tr;
				ans += (1 << i);
			}
		}
		cout << ans << endl;
	}
	return 0;
}
Compilation message (stderr)
| # | Verdict | Execution time | Memory | Grader output | 
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output | 
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output | 
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output | 
|---|---|---|---|---|
| Fetching results... | ||||
