fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main(String arg[]){
  11. Scanner scanner = new Scanner(System.in);
  12. int T = scanner.nextInt();
  13. while (T-- > 0){
  14. long n = scanner.nextInt();
  15. long m = scanner.nextInt();
  16. List<Long> listL = new ArrayList<>();
  17. List<Long> listR = new ArrayList<>();
  18. for (int i=0; i<n; i++){
  19. listL.add(scanner.nextLong());
  20. listR.add(scanner.nextLong());
  21. }
  22. Collections.sort(listL);
  23. Collections.sort(listR);
  24. for (int i=0; i<m; i++){
  25. long x = scanner.nextLong();
  26. int indx = Collections.binarySearch(listL,x);
  27. if (indx>=0)
  28. System.out.println(0);
  29. else if (x>=listR.get((int) (n-1)))
  30. System.out.println(-1);
  31. else if (x < listL.get(0))
  32. System.out.println(listL.get(0)-x);
  33. else {
  34. int leftIndex = (-indx - 2);
  35. int rightIndex = leftIndex +1;
  36. if (listR.get(leftIndex) > x)
  37. System.out.println(0);
  38. else
  39. System.out.println(listL.get(rightIndex)-x);
  40. }
  41. }
  42. }
  43. }
  44. }
Success #stdin #stdout 0.13s 2184192KB
stdin
1
4 5
5 7
9 10
2 3
20 30
5
6
7
35
1
stdout
0
0
2
-1
1