Submission #860555


Source Code Expand

Copy
#include <iostream>
#include <algorithm>
using namespace std;

#define MAX_N 100000

int N, L, Q;
int x[MAX_N];
int tab[20][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);

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

	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<19; 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 700
Code Size 1053 Byte
Status AC
Exec Time 1051 ms
Memory 9088 KB

Judge Result

Set Name Sample Subtask1 All
Score / Max Score 0 / 0 200 / 200 500 / 500
Status
AC × 1
AC × 14
AC × 27
Set Name Test Cases
Sample example_01.txt
Subtask1 example_01.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt, subtask1_13.txt
All example_01.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt, subtask1_13.txt, subtask2_01.txt, subtask2_02.txt, subtask2_03.txt, subtask2_04.txt, subtask2_05.txt, subtask2_06.txt, subtask2_07.txt, subtask2_08.txt, subtask2_09.txt, subtask2_10.txt, subtask2_11.txt, subtask2_12.txt, subtask2_13.txt
Case Name Status Exec Time Memory
example_01.txt AC 4 ms 384 KB
subtask1_01.txt AC 5 ms 384 KB
subtask1_02.txt AC 4 ms 384 KB
subtask1_03.txt AC 12 ms 384 KB
subtask1_04.txt AC 13 ms 384 KB
subtask1_05.txt AC 13 ms 384 KB
subtask1_06.txt AC 9 ms 384 KB
subtask1_07.txt AC 8 ms 384 KB
subtask1_08.txt AC 13 ms 384 KB
subtask1_09.txt AC 13 ms 384 KB
subtask1_10.txt AC 13 ms 384 KB
subtask1_11.txt AC 11 ms 384 KB
subtask1_12.txt AC 16 ms 384 KB
subtask1_13.txt AC 12 ms 384 KB
subtask2_01.txt AC 872 ms 8960 KB
subtask2_02.txt AC 922 ms 8960 KB
subtask2_03.txt AC 818 ms 8832 KB
subtask2_04.txt AC 459 ms 5888 KB
subtask2_05.txt AC 573 ms 6016 KB
subtask2_06.txt AC 761 ms 8704 KB
subtask2_07.txt AC 1016 ms 8960 KB
subtask2_08.txt AC 1051 ms 9088 KB
subtask2_09.txt AC 964 ms 8960 KB
subtask2_10.txt AC 901 ms 8960 KB
subtask2_11.txt AC 1021 ms 8448 KB
subtask2_12.txt AC 810 ms 9088 KB
subtask2_13.txt AC 822 ms 8704 KB