-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathIPL.cpp
More file actions
47 lines (42 loc) · 1.24 KB
/
IPL.cpp
File metadata and controls
47 lines (42 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <bits/stdc++.h>
#include <stdint.h>
using namespace std ;
typedef vector<int>::iterator vi ;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int N, K, P ;
cin >> N >> K >> P ;
vector<int> vec(N+1), vec1(N) ; // 1 extra in vec to avoid array out of bounds
map<int, int> v ; // Default order in a map is increasing order
int temp ;
for(int i=0; i<N; ++i) {
cin >> temp ;
vec[i] = temp ;
v.insert(pair<int, int>(i + 1, temp));
}
vec[N] = INT32_MAX ;
sort(vec.begin(), vec.end()) ;
int i=0, j=0 ;
for(; i<N; ++i) {
for (j=max(j, i); j < N; ++j) {
if (vec[j+1]-vec[j] > K){ // Problem of index out of bounds at i=N solved using infinity
vec1[i] = j ;
break ;
}
}
}
int A, B ;
while(P--){
cin >> A >> B ;
vi h = lower_bound(vec.begin(), vec.end(), v[A]) ;
vi g = lower_bound(vec.begin(), vec.end(), v[B]) ;
vi l = *g>*h ? h : g; // l points to element with min value
vi m = l==h ? g : h ;
if (vec1[distance(vec.begin(), l)]>=distance(vec.begin(), m)){
cout << "Yes\n" ;
} else {
cout << "No\n" ;
}
}
}