Submission #290675

# Submission time Handle Problem Language Result Execution time Memory
290675 2020-09-04T10:15:33 Z maximath_1 Railway Trip (JOI17_railway_trip) C++11
100 / 100
226 ms 17272 KB
#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

railway_trip.cpp: In function 'void setIn(std::string)':
railway_trip.cpp:25:29: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   25 | void setIn(string s){freopen(s.c_str(), "r", stdin);}
      |                      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
railway_trip.cpp: In function 'void setOut(std::string)':
railway_trip.cpp:26:30: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   26 | void setOut(string s){freopen(s.c_str(), "w", stdout);}
      |                       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 46 ms 15076 KB Output is correct
3 Correct 43 ms 15104 KB Output is correct
4 Correct 41 ms 15148 KB Output is correct
5 Correct 41 ms 15104 KB Output is correct
6 Correct 40 ms 15252 KB Output is correct
7 Correct 42 ms 15360 KB Output is correct
8 Correct 38 ms 15052 KB Output is correct
9 Correct 44 ms 15360 KB Output is correct
10 Correct 41 ms 15232 KB Output is correct
11 Correct 51 ms 15352 KB Output is correct
12 Correct 54 ms 15360 KB Output is correct
13 Correct 54 ms 15360 KB Output is correct
14 Correct 52 ms 15360 KB Output is correct
15 Correct 51 ms 15480 KB Output is correct
16 Correct 51 ms 15360 KB Output is correct
17 Correct 41 ms 15360 KB Output is correct
18 Correct 42 ms 15608 KB Output is correct
19 Correct 39 ms 15384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 146 ms 16632 KB Output is correct
2 Correct 153 ms 16632 KB Output is correct
3 Correct 145 ms 16760 KB Output is correct
4 Correct 139 ms 16676 KB Output is correct
5 Correct 144 ms 16688 KB Output is correct
6 Correct 143 ms 16760 KB Output is correct
7 Correct 137 ms 16632 KB Output is correct
8 Correct 173 ms 16760 KB Output is correct
9 Correct 220 ms 17016 KB Output is correct
10 Correct 219 ms 16788 KB Output is correct
11 Correct 215 ms 16760 KB Output is correct
12 Correct 226 ms 16760 KB Output is correct
13 Correct 221 ms 16760 KB Output is correct
14 Correct 197 ms 16760 KB Output is correct
15 Correct 158 ms 16632 KB Output is correct
16 Correct 142 ms 16632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 118 ms 16888 KB Output is correct
2 Correct 119 ms 16888 KB Output is correct
3 Correct 121 ms 16888 KB Output is correct
4 Correct 116 ms 16760 KB Output is correct
5 Correct 222 ms 16784 KB Output is correct
6 Correct 185 ms 17144 KB Output is correct
7 Correct 218 ms 17144 KB Output is correct
8 Correct 191 ms 17016 KB Output is correct
9 Correct 174 ms 17116 KB Output is correct
10 Correct 175 ms 17016 KB Output is correct
11 Correct 177 ms 17016 KB Output is correct
12 Correct 179 ms 17144 KB Output is correct
13 Correct 179 ms 17016 KB Output is correct
14 Correct 189 ms 17144 KB Output is correct
15 Correct 191 ms 17144 KB Output is correct
16 Correct 192 ms 17272 KB Output is correct
17 Correct 171 ms 17144 KB Output is correct
18 Correct 173 ms 17144 KB Output is correct
19 Correct 180 ms 17172 KB Output is correct
20 Correct 146 ms 17008 KB Output is correct
21 Correct 118 ms 16888 KB Output is correct
22 Correct 118 ms 16888 KB Output is correct
23 Correct 115 ms 16760 KB Output is correct