fork download
  1. import java.io.DataInputStream;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5.  
  6.  
  7. class KQUERY {
  8. static int a[];
  9. public static void main(String[] args) throws IOException {
  10. // TODO Auto-generated method stub
  11. Reader r=new Reader();
  12. StringBuilder sb=new StringBuilder();
  13. int n=r.nextInt();
  14. a=new int[n+1];
  15. for(int i=1;i<=n;i++)a[i]=r.nextInt();
  16. Segment_Tree st=new Segment_Tree(1,n);
  17. int q=r.nextInt();
  18. for(int i=0;i<q;i++){
  19. int start=r.nextInt();
  20. int end=r.nextInt();
  21. int k=r.nextInt();
  22. int ans=st.binarySearch(start,end, k);
  23. sb.append(ans+"\n");
  24.  
  25. }
  26. pr.write(sb.toString());
  27. pr.flush();
  28. }
  29.  
  30. static class Segment_Tree{
  31. int start,end;
  32. Segment_Tree left_child,right_child;
  33. int array[];
  34. public Segment_Tree(){}
  35. public Segment_Tree(int a,int b){
  36. start=a;end=b;
  37. if(start!=end){
  38. int mid=(start+end)>>1;
  39. left_child=new Segment_Tree(a,mid);
  40. right_child=new Segment_Tree(mid+1,b);
  41. join(this, left_child, right_child);
  42. }
  43. else {
  44. array=new int[1];
  45. array[0]=KQUERY.a[start];
  46. }
  47. }
  48. public static void join(Segment_Tree parent,Segment_Tree left_child,Segment_Tree right_child){
  49. parent.array=new int[left_child.array.length+right_child.array.length];
  50. int left_len=left_child.array.length;
  51. int right_len=right_child.array.length;
  52. int i=0,j=0,k=0;
  53. for(;i<left_len && j<right_len; ){
  54. parent.array[k++]=left_child.array[i]<right_child.array[j]?left_child.array[i++]:right_child.array[j++];
  55. }
  56. for(;i<left_len;i++)parent.array[k++]=left_child.array[i];
  57. for(;j<right_len;j++)parent.array[k++]=right_child.array[j];
  58. }
  59. public Segment_Tree query(int a,int b){
  60. if(a>b || a>end || b<start)return null;
  61. if(start==a && end==b)return this;
  62. int mid=(start+end)>>1;
  63. if(a>mid)return right_child.query(a, b);
  64. else if(b<=mid)return left_child.query(a, b);
  65. Segment_Tree ans=new Segment_Tree();
  66. Segment_Tree left=left_child.query(a,mid);
  67. Segment_Tree right=right_child.query(mid+1,b);
  68. join(ans,left,right);
  69. return ans;
  70. }
  71. public int binarySearch(int start,int end,int k){
  72. Segment_Tree ans=this.query(start, end);
  73. //printPostOrder(ans);
  74. int low=0,high=ans.array.length-1,mid=(low+(high-low+1)>>1);
  75.  
  76. while(low<high){
  77. mid=(low+((high-low+1)>>1));
  78. if(ans.array[mid]>k)high=mid-1;
  79. else if(ans.array[mid]==k)low=mid;
  80. else low=mid+1;
  81. }
  82. mid=(low+((high-low+1)>>1));
  83.  
  84. if(ans.array[mid]>k)return ans.array.length-mid;
  85. else return ans.array.length-mid-1;
  86. }
  87. }
  88. static class Reader {
  89. final private int BUFFER_SIZE = 1 << 16;
  90. private DataInputStream din;
  91. private byte[] buffer;
  92. private int bufferPointer, bytesRead;
  93.  
  94. public Reader() {
  95. din = new DataInputStream(System.in);
  96. buffer = new byte[BUFFER_SIZE];
  97. bufferPointer = bytesRead = 0;
  98. }
  99.  
  100. public Reader(String file_name) throws IOException {
  101. din = new DataInputStream(new FileInputStream(file_name));
  102. buffer = new byte[BUFFER_SIZE];
  103. bufferPointer = bytesRead = 0;
  104. }
  105.  
  106. public String readLine() throws IOException {
  107. byte[] buf = new byte[64];
  108. int cnt = 0, c;
  109. while ((c = read()) != -1) {
  110. if (c == '\n')
  111. break;
  112. buf[cnt++] = (byte) c;
  113. }
  114. return new String(buf, 0, cnt);
  115. }
  116.  
  117. public int nextInt() throws IOException {
  118. int ret = 0;
  119. byte c = read();
  120. while (c <= ' ')
  121. c = read();
  122. boolean neg = (c == '-');
  123. if (neg)
  124. c = read();
  125. do {
  126. ret = ret * 10 + c - '0';
  127. } while ((c = read()) >= '0' && c <= '9');
  128. if (neg)
  129. return -ret;
  130. return ret;
  131. }
  132.  
  133. public long nextLong() throws IOException {
  134. long ret = 0;
  135. byte c = read();
  136. while (c <= ' ')
  137. c = read();
  138. boolean neg = (c == '-');
  139. if (neg)
  140. c = read();
  141. do {
  142. ret = ret * 10 + c - '0';
  143. } while ((c = read()) >= '0' && c <= '9');
  144. if (neg)
  145. return -ret;
  146. return ret;
  147. }
  148.  
  149. public double nextDouble() throws IOException {
  150. double ret = 0, div = 1;
  151. byte c = read();
  152. while (c <= ' ')
  153. c = read();
  154. boolean neg = (c == '-');
  155. if (neg)
  156. c = read();
  157. do {
  158. ret = ret * 10 + c - '0';
  159. } while ((c = read()) >= '0' && c <= '9');
  160. if (c == '.')
  161. while ((c = read()) >= '0' && c <= '9')
  162. ret += (c - '0') / (div *= 10);
  163. if (neg)
  164. return -ret;
  165. return ret;
  166. }
  167.  
  168. private void fillBuffer() throws IOException {
  169. bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
  170. if (bytesRead == -1)
  171. buffer[0] = -1;
  172. }
  173.  
  174. private byte read() throws IOException {
  175. if (bufferPointer == bytesRead)
  176. fillBuffer();
  177. return buffer[bufferPointer++];
  178. }
  179.  
  180. public void close() throws IOException {
  181. if (din == null)
  182. return;
  183. din.close();
  184. }
  185. }
  186.  
  187. }
  188.  
  189.  
Success #stdin #stdout 0.07s 380224KB
stdin
10
10 1 9 2 8 3 7 4 6 5
5
1 2 2
1 4 4
1 6 6
1 8 4
1 7 3
stdout
1
2
3
4
4