fork download
  1. import java.io.*;
  2. import java.util.*;
  3. import java.math.*;
  4.  
  5.  
  6. class Query implements Comparable<Query>
  7. {
  8. int index;
  9. int left;
  10. int right;
  11.  
  12. public Query()
  13. {
  14. index = 0;
  15. left = 0;
  16. right = 0;
  17. }
  18. public int compareTo(Query q)
  19. {
  20. int left1 = this.left/dquery.blocksize;
  21. int left2 = q.left/dquery.blocksize;
  22.  
  23. if(left1-left2==0)
  24. return this.right-q.right;
  25. else
  26. return left1-left2;
  27. }
  28. }
  29.  
  30. class dquery
  31. {
  32. public static int blocksize = 0;
  33. public static void main(String[] args) throws IOException
  34. {
  35.  
  36. Reader obj =new Reader();
  37. int n = obj.nextInt();
  38. int[] arr = new int[n];
  39. for(int i=0;i<n;i++)
  40. arr[i] = obj.nextInt();
  41.  
  42. ArrayList<Query> list = new ArrayList<Query>();
  43. int nq = obj.nextInt();
  44.  
  45. for(int i=0;i<nq;i++)
  46. {
  47. Query qn = new Query();
  48. qn.index = i;
  49. qn.left = obj.nextInt()-1;
  50. qn.right = obj.nextInt()-1;
  51. list.add(qn);
  52. }
  53.  
  54. blocksize = (int)Math.sqrt(n);
  55. // System.out.println("block size "+blocksize);
  56. Collections.sort(list);
  57.  
  58. int[] count = new int[(int)1e6+1];
  59. int[] answer = new int[nq];
  60.  
  61. int start = list.get(0).left;
  62. int end = start;
  63. count[arr[start]]++;
  64. int ans = 1;
  65. for(Query q:list)
  66. {
  67. while(start<q.left)
  68. {
  69. count[arr[start]]--;
  70. if(count[arr[start]]==0)
  71. ans--;
  72. start++;
  73. }
  74. while(start>q.left)
  75. {
  76. start--;
  77. count[arr[start]]++;
  78. if(count[arr[start]]==1)
  79. ans++;
  80. }
  81. while(end<q.right)
  82. {
  83. end++;
  84. count[arr[end]]++;
  85. if(count[arr[end]]==1)
  86. ans++;
  87. }
  88. while(end>q.right)
  89. {
  90. count[arr[end]]--;
  91. if(count[arr[end]]==0)
  92. ans--;
  93. end--;
  94. }
  95. answer[q.index] = ans;
  96. }
  97. for(int i=0;i<nq;i++)
  98. out.write(answer[i]+"\n");
  99. out.close();
  100. }
  101.  
  102. static class Reader
  103. {
  104. final private int BUFFER_SIZE = 1 << 16;
  105. private DataInputStream din;
  106. private byte[] buffer;
  107. private int bufferPointer, bytesRead;
  108.  
  109. public Reader()
  110. {
  111. din = new DataInputStream(System.in);
  112. buffer = new byte[BUFFER_SIZE];
  113. bufferPointer = bytesRead = 0;
  114. }
  115.  
  116. public Reader(String file_name) throws IOException
  117. {
  118. din = new DataInputStream(new FileInputStream(file_name));
  119. buffer = new byte[BUFFER_SIZE];
  120. bufferPointer = bytesRead = 0;
  121. }
  122.  
  123. public String readLine() throws IOException
  124. {
  125. byte[] buf = new byte[64]; // line length
  126. int cnt = 0, c;
  127. while ((c = read()) != -1)
  128. {
  129. if (c == '\n')
  130. break;
  131. buf[cnt++] = (byte) c;
  132. }
  133. return new String(buf, 0, cnt);
  134. }
  135.  
  136. public int nextInt() throws IOException
  137. {
  138. int ret = 0;
  139. byte c = read();
  140. while (c <= ' ')
  141. c = read();
  142. boolean neg = (c == '-');
  143. if (neg)
  144. c = read();
  145. do
  146. {
  147. ret = ret * 10 + c - '0';
  148. } while ((c = read()) >= '0' && c <= '9');
  149.  
  150. if (neg)
  151. return -ret;
  152. return ret;
  153. }
  154.  
  155. public long nextLong() throws IOException
  156. {
  157. long ret = 0;
  158. byte c = read();
  159. while (c <= ' ')
  160. c = read();
  161. boolean neg = (c == '-');
  162. if (neg)
  163. c = read();
  164. do {
  165. ret = ret * 10 + c - '0';
  166. }
  167. while ((c = read()) >= '0' && c <= '9');
  168. if (neg)
  169. return -ret;
  170. return ret;
  171. }
  172.  
  173. public double nextDouble() throws IOException
  174. {
  175. double ret = 0, div = 1;
  176. byte c = read();
  177. while (c <= ' ')
  178. c = read();
  179. boolean neg = (c == '-');
  180. if (neg)
  181. c = read();
  182.  
  183. do {
  184. ret = ret * 10 + c - '0';
  185. }
  186. while ((c = read()) >= '0' && c <= '9');
  187.  
  188. if (c == '.')
  189. {
  190. while ((c = read()) >= '0' && c <= '9')
  191. {
  192. ret += (c - '0') / (div *= 10);
  193. }
  194. }
  195.  
  196. if (neg)
  197. return -ret;
  198. return ret;
  199. }
  200.  
  201. private void fillBuffer() throws IOException
  202. {
  203. bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
  204. if (bytesRead == -1)
  205. buffer[0] = -1;
  206. }
  207.  
  208. private byte read() throws IOException
  209. {
  210. if (bufferPointer == bytesRead)
  211. fillBuffer();
  212. return buffer[bufferPointer++];
  213. }
  214.  
  215. public void close() throws IOException
  216. {
  217. if (din == null)
  218. return;
  219. din.close();
  220. }
  221. }
  222.  
  223. }
  224.  
Success #stdin #stdout 0.04s 4386816KB
stdin
5
1 1 2 1 3
3
1 5
2 4
3 5
stdout
3
2
3