fork download
  1. import java.io.*;
  2. import java.util.*;
  3. class Ideone{
  4. static class Fast{
  5. Fast() throws IOException{
  6. st=new StringTokenizer(br.readLine());
  7.  
  8. }
  9. String next() throws IOException {
  10. return (st.hasMoreElements()?st.nextToken():(st=new StringTokenizer(br.readLine())).nextToken());
  11. }
  12. int nextInt() throws IOException{
  13. return Integer.parseInt(next());
  14. }
  15. long nextLong() throws IOException{
  16. return Long.parseLong(next());
  17. }
  18. void print(Object o) throws IOException{
  19. String s=String.valueOf(o);
  20. bw.write(s);
  21. bw.flush();
  22. }
  23. }
  24. public static void main(String args[]) throws IOException {
  25. Fast f=new Fast();
  26. // int t=f.nextInt();
  27. // while(t--!=0){
  28. int n=f.nextInt();
  29. HashMap<Integer,Integer> map=new HashMap<>();
  30. for(int i=0;i<n;i++){
  31. int x=f.nextInt();
  32. map.put(x,map.containsKey(x)?map.get(x)+1:1);
  33. int y=f.nextInt();
  34. map.put(y+1,map.containsKey(y+1)?map.get(y+1)-1:-1);
  35. }
  36. int q=f.nextInt();
  37. int qr[]=new int[q];
  38. for(int i=0;i<q;i++){
  39. qr[i]=f.nextInt();
  40. }
  41. ArrayList<Integer> list=new ArrayList<>(map.keySet());
  42. int max=list.get(list.size()-1);
  43. int min=list.get(0);
  44. int sum=0;
  45. for(Map.Entry<Integer,Integer> entry:map.entrySet()){
  46. sum+=entry.getValue();
  47. entry.setValue(sum);
  48. }
  49.  
  50. for(int i=0;i<q;i++){
  51. if(qr[i]<=max && qr[i]>=min){
  52. if(list.contains(qr[i])){
  53. f.print(map.get(qr[i]) + "\n");
  54. }
  55. else{
  56. int start=0;
  57. int end=list.size()-1;
  58. int ans=-1;
  59. while(start<=end){
  60. int mid=(start+end)/2;
  61. if(list.get(mid)>=qr[i]){
  62. end=mid-1;
  63. }
  64. else{
  65. ans=mid;
  66. start=mid+1;
  67. }
  68. }
  69. f.print(map.get(list.get(ans)) + "\n");
  70. }
  71. }
  72. else{
  73. f.print(0 + "\n");
  74. }
  75. }
  76. }
  77. }
  78.  
Success #stdin #stdout 0.12s 50280KB
stdin
4
1 3
1 6
3 5
5 9
4
3
6
4
2
stdout
3
2
2
2