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[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. int n = 4;
  14. int[] start = {1, 3, 2, 5};
  15. int[] end = {3, 5, 3, 6};
  16. int k = 2;
  17. int[] q_start = {1, 3};
  18. int[] q_end = {4, 6};
  19.  
  20. int limit = 0;
  21.  
  22. for(int i = 0; i < n; i++){
  23. limit = Math.max(limit, end[i]);
  24. }
  25.  
  26. int[] temp = new int[limit + 2];
  27.  
  28. for(int i = 0; i < n; i++){
  29. int l = start[i];
  30. int r = end[i];
  31.  
  32. temp[l] = 1;
  33. temp[r+1] = -1;
  34. }
  35.  
  36. int[] preSum = new int[limit + 2];
  37.  
  38. preSum[0] = temp[0];
  39.  
  40. for(int i = 1; i < limit + 2; i++){
  41. preSum[i] = preSum[i-1] + temp[i];
  42. }
  43.  
  44. for(int i = 0; i < preSum.length; i++){
  45. if(preSum[i] < k){
  46. preSum[i] = 0;
  47. }else{
  48. preSum[i] = 1;
  49. }
  50. }
  51.  
  52. int[] preSum2 = new int[limit + 2];
  53.  
  54. preSum2[0] = preSum[0];
  55.  
  56. for(int i = 1; i < preSum2.length; i++){
  57. preSum2[i] = preSum2[i-1] + preSum[i];
  58. }
  59.  
  60. for(int i = 0; i < q_start.length; i++){
  61. int l = q_start[i];
  62. int r = q_end[i];
  63.  
  64. System.out.println(preSum2[r] - preSum2[l-1]);
  65. }
  66. }
  67. }
Success #stdin #stdout 0.09s 54588KB
stdin
Standard input is empty
stdout
3
4