fork(2) download
  1. // July Challenge Problem - 3 //
  2. #include <cstdio>
  3. #include<vector>
  4. #include<algorithm>
  5. using namespace std;
  6. // Our utility Functions //
  7. vector<long int> v;
  8. bool myfunction (long int i,long int j) { return (v[i]<v[j]); }
  9. // Main Starts here //
  10. int main() {
  11.  
  12. vector<long int> vcopy;
  13.  
  14. long int k,n;
  15. long int p,it;
  16. long int A,B;
  17. long int mins,maxs;
  18. long int ad=0;
  19. long int seclast;
  20. // --------- Variable Declarations Over ------ //
  21. scanf("%ld",&n);
  22. scanf("%ld",&k);
  23. scanf("%ld",&p);
  24. v.resize(n);
  25. vcopy.resize(n);
  26. for(it=0;it<n;it++)
  27. {
  28. scanf("%ld",&v[it]);
  29. vcopy[it]=it;
  30. }
  31. // Pre processing steps //
  32. sort(vcopy.begin(),vcopy.end(),myfunction); // sort with respect to v ( nlogn algorithm)
  33. it=0;
  34. ad=0;
  35. seclast=v[vcopy[n-2]];
  36.  
  37. for(it=0;it<(n-1);it++)
  38. {
  39. if((v[vcopy[it+1]]-v[vcopy[it]])<=k)
  40. {
  41. v[vcopy[it]]=ad;
  42. //it++;
  43. }
  44. else
  45. {
  46. v[vcopy[it]]=ad;
  47. // it++;
  48.  
  49. ad++;
  50. }
  51. }
  52. if((v[vcopy[n-1]]-seclast)<=k)
  53. {
  54. v[vcopy[n-1]]=ad;
  55. }
  56. else
  57. {
  58. v[vcopy[n-1]]=ad+1;
  59. }
  60.  
  61.  
  62. // Time for answering the queries in O(1) after O(nlogn +n) algorithms //
  63. while(p--)
  64. {
  65. scanf("%ld",&A);
  66. A--;
  67. scanf("%ld",&B);
  68. B--;
  69. if(v[A]==v[B])
  70. {
  71. printf("Yes\n");
  72. }
  73. else
  74. {
  75. printf("No\n");
  76. }
  77. }
  78. return 0;
  79. }
Success #stdin #stdout 0s 3436KB
stdin
5 3 3
0 3 8 5 12
1 2
1 3
2 5
stdout
Yes
Yes
No