Submission #860545


Source Code Expand

Copy
#include <iostream>
using namespace std;

#define MAX_N 100000

int tab[40][MAX_N];

// mid日でaからbまで行けるか
bool C(int a, int b, int mid){
	int pos = a;
	int cnt = 0;
	while(mid != 0){
		if(mid & 1){
			pos = tab[cnt][pos];
		}
		mid >>= 1;
		cnt++;
	}
	if(pos >= b) return true;
	else return false;
}


int solve(int a, int b){
	if(a > b) swap(a, b);

	int ub = 100000;
	int lb = 0;
	while(ub - lb > 1){
		int mid = lb + (ub - lb) / 2;
		if(C(a, b, mid)) ub = mid;
		else lb = mid;
	}
	return ub;
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);

	int N, L, Q;
	int x[MAX_N];

	cin >> N;
	for(int i=0; i<N; i++) cin >> x[i];
	cin >> L >> Q;


	int y[MAX_N];

	for(int i=0; i<N; i++){
		tab[0][i]  = upper_bound(x, x+N, x[i]+L) - x - 1;
	}

	for(int k=0; k<40; k++){
		for(int i=0; i<N; i++){
			tab[k+1][i] = tab[k][min(N-1, tab[k][i])];
		}
	}

	for(int i=0; i<Q; i++){
		int a, b;
		cin >> a >> b;
		a--; b--;
		cout << solve(a, b) << endl;
	}

	return 0;
}

Submission Info

Submission Time
Task E - Tak and Hotels
User suzume
Language C++14 (GCC 5.4.1)
Score 0
Code Size 1055 Byte
Status CE

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:52:42: error: ‘upper_bound’ was not declared in this scope
   tab[0][i]  = upper_bound(x, x+N, x[i]+L) - x - 1;
                                          ^