Submission #400198

# Submission time Handle Problem Language Result Execution time Memory
400198 2021-05-07T14:50:58 Z GioChkhaidze Railway Trip (JOI17_railway_trip) C++14
100 / 100
214 ms 18140 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 5;

stack < int > st;
int a[N], L[N][19], R[N][19];

main () {
	ios::sync_with_stdio(false);
	cin.tie(NULL), cout.tie(NULL);
	int n, k, q;
	cin >> n >> k >> q;
	for (int i = 1; i <= n; ++i) {
		cin >> a[i];
	}
	
	stack < int > st;
	st.push(0);
	a[0] = k + 1;
	for (int i = 1; i <= n; ++i) {
		while (a[st.top()] < a[i]) {
			st.pop();
		}
		L[i][0] = st.top();
		st.push(i);
	}
	
	while (st.size())	
		st.pop();
	
	st.push(n + 1);
	a[n + 1] = k + 1;
	for (int i = n; i >= 1; --i) {
		while (a[st.top()] < a[i]) {
			st.pop();
		}
		R[i][0] = st.top();
		st.push(i);
	}
	
	while (st.size())	
		st.pop();
	
	for (int i = 1; i <= n; ++i) {
		if (L[i][0] == 0) {
			L[i][0] = i;
		}
		
		if (R[i][0] == n + 1) {
			R[i][0] = i;
		}
	}
	
	for (int j = 1; j <= 17; ++j) {
		for (int i = 1; i <= n; ++i) {
			L[i][j] = min(L[L[i][j - 1]][j - 1], L[R[i][j - 1]][j - 1]);
			R[i][j] = max(R[L[i][j - 1]][j - 1], R[R[i][j - 1]][j - 1]);
		}
	}
	
	for (int i = 1; i <= q; ++i) {
		int a, b, lt, rt, l, r, c, ans = 0;
		cin >> a >> b;
		if (a > b) swap(a, b);
		
		l = r = a;
		for (int j = 17; j >= 0; --j) {
			if (max(R[l][j], R[r][j]) < b) {
				ans += (1 << j);
				lt = l, rt = r;
				l = min(L[lt][j], L[rt][j]);
				r = max(R[lt][j], R[rt][j]);
			}
		}
		
		c = r, l = r = b;
		for (int j = 17; j >= 0; --j) {
			if (c < min(L[l][j], L[r][j])) {
				ans += (1 << j);
				lt = l, rt = r;
				l = min(L[lt][j], L[rt][j]);
				r = max(R[lt][j], R[rt][j]);
			}
		}
		
		cout << ans << "\n";
	}
}

Compilation message

railway_trip.cpp:9:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
    9 | main () {
      |       ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 328 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 53 ms 15820 KB Output is correct
3 Correct 46 ms 15848 KB Output is correct
4 Correct 43 ms 15852 KB Output is correct
5 Correct 43 ms 15860 KB Output is correct
6 Correct 43 ms 15948 KB Output is correct
7 Correct 47 ms 16160 KB Output is correct
8 Correct 42 ms 16132 KB Output is correct
9 Correct 49 ms 16368 KB Output is correct
10 Correct 47 ms 16204 KB Output is correct
11 Correct 53 ms 16212 KB Output is correct
12 Correct 54 ms 16236 KB Output is correct
13 Correct 57 ms 16280 KB Output is correct
14 Correct 52 ms 16236 KB Output is correct
15 Correct 52 ms 16288 KB Output is correct
16 Correct 52 ms 16276 KB Output is correct
17 Correct 45 ms 16120 KB Output is correct
18 Correct 45 ms 16132 KB Output is correct
19 Correct 41 ms 16076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 139 ms 17400 KB Output is correct
2 Correct 143 ms 17348 KB Output is correct
3 Correct 133 ms 17348 KB Output is correct
4 Correct 136 ms 17360 KB Output is correct
5 Correct 133 ms 17380 KB Output is correct
6 Correct 139 ms 17448 KB Output is correct
7 Correct 140 ms 17404 KB Output is correct
8 Correct 170 ms 17604 KB Output is correct
9 Correct 197 ms 17764 KB Output is correct
10 Correct 209 ms 17808 KB Output is correct
11 Correct 203 ms 17848 KB Output is correct
12 Correct 210 ms 17856 KB Output is correct
13 Correct 200 ms 17860 KB Output is correct
14 Correct 193 ms 17516 KB Output is correct
15 Correct 161 ms 17252 KB Output is correct
16 Correct 138 ms 17348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 118 ms 17572 KB Output is correct
2 Correct 119 ms 17476 KB Output is correct
3 Correct 124 ms 17472 KB Output is correct
4 Correct 118 ms 17552 KB Output is correct
5 Correct 214 ms 17808 KB Output is correct
6 Correct 178 ms 18132 KB Output is correct
7 Correct 181 ms 17988 KB Output is correct
8 Correct 191 ms 17908 KB Output is correct
9 Correct 172 ms 17860 KB Output is correct
10 Correct 168 ms 17936 KB Output is correct
11 Correct 168 ms 17900 KB Output is correct
12 Correct 183 ms 17964 KB Output is correct
13 Correct 167 ms 17860 KB Output is correct
14 Correct 188 ms 17948 KB Output is correct
15 Correct 191 ms 17988 KB Output is correct
16 Correct 190 ms 18140 KB Output is correct
17 Correct 175 ms 17868 KB Output is correct
18 Correct 176 ms 17856 KB Output is correct
19 Correct 181 ms 17988 KB Output is correct
20 Correct 160 ms 17632 KB Output is correct
21 Correct 130 ms 17588 KB Output is correct
22 Correct 125 ms 17520 KB Output is correct
23 Correct 112 ms 17476 KB Output is correct