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