fork download
  1. import java.util.*;
  2. class Quad2P {
  3. public static void main(String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5.  
  6. long n = sc.nextLong();
  7. long k1 = sc.nextLong();
  8. long k2 = sc.nextLong();
  9.  
  10. long[] a = new long[(int)n + 2]; // n+1 size with 1-based indexing
  11.  
  12. for (int i = 1; i <= n; i++) {
  13. a[i] = sc.nextLong();
  14. }
  15.  
  16. long c = 0;
  17. for(int i = 2; i<=n-2; i++){
  18. long c1=0;
  19. // count curr elem compatibility with all prev elem
  20. // we dont do j-i+1 coz single elem not counted(no +1)
  21. // and its valid only if k,l are there to.. so i,j need to be valid
  22. //first.. (i,j) <k is one prob and if a pair is true then see (k,l) if 0 k,l then 0*c2
  23. int j = 1;
  24. while(j<=(i-1)){
  25. if(a[i]+a[j]>k1){
  26. c1++;
  27. }
  28. j++;
  29. }
  30. long c2=0;
  31. int k = i+1, l = (int)n;
  32. while(k<l){
  33. if(a[k]+a[l]>k2){
  34. c2 += l-k;
  35. l--;
  36. // check for a lesser window now.. we know that k.. l true, move on and see if k.. l-1
  37. }
  38. else{
  39. k++; // we need a greater elem to pair w l and make it >k2
  40. }
  41. }
  42. c+= c1*c2;
  43. }
  44.  
  45. System.out.println(c);
  46. }
  47. }
  48.  
Success #stdin #stdout 0.13s 56660KB
stdin
5 7 10
1 3 5 7 9
stdout
1