fork download
  1. import java.io.DataInputStream;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.util.Arrays;
  6. import java.util.Comparator;
  7.  
  8.  
  9. class DQUERY_Solution_2 {
  10. static int index[];
  11. static int n;
  12. static int a[];
  13. static long tree[];
  14. static long read[];
  15. public static void main(String[] args) throws IOException {
  16. // TODO Auto-generated method stub
  17. Reader r=new Reader();
  18. n=r.nextInt();
  19. a=new int[n+1];
  20. index=new int[100001];
  21. tree=new long[n+1];
  22. read=new long[n+1];
  23. Arrays.fill(read, -1);
  24. for(int i=1;i<=n;i++)a[i]=r.nextInt();
  25. int q=r.nextInt();
  26. int queries[][]=new int[q][3];
  27. long answers[][]=new long[q][2];
  28. for(int i=0;i<q;i++){
  29. queries[i][0]=i;
  30. answers[i][0]=i;
  31. queries[i][1]=r.nextInt();
  32. queries[i][2]=r.nextInt();
  33. }
  34.  
  35. Arrays.sort(queries, new Comparator<int []>() {
  36.  
  37. @Override
  38. public int compare(int[] arg0, int[] arg1) {
  39. // TODO Auto-generated method stub
  40. return Integer.valueOf(arg0[2]-arg1[2]);
  41. }
  42. });
  43. int j=1;
  44. //for(int i=1;i<=q;i++)System.out.println(queries[i][0]+" "+queries[i][1]+" "+queries[i][2]);
  45. StringBuilder sb=new StringBuilder();
  46. int prev_num=-1;
  47. long readnum=-1;
  48. for(int i=0;i<q;i++){
  49. int num=queries[i][2];
  50. while(j<=queries[i][2]){
  51. if(index[a[j]]==0){
  52. update(j, 1);
  53. }
  54. else{
  55. update(index[a[j]], -1);
  56. update(j,1);
  57. }
  58. index[a[j]]=j;
  59. j++;
  60. //System.out.println(Arrays.toString(tree));
  61. }
  62. if(prev_num!=num)
  63. readnum=read(num);
  64. answers[queries[i][0]][1]=readnum-read(queries[i][1]-1);
  65. prev_num=num;
  66. }
  67. Arrays.sort(answers,new Comparator<long []>() {
  68.  
  69. @Override
  70. public int compare(long[] o1, long[] o2) {
  71. // TODO Auto-generated method stub
  72. return Integer.valueOf((int) (o1[0]-o2[0]));
  73. }
  74. });
  75. for(int i=0;i<q;i++){
  76. sb.append(answers[i][1]+"\n");
  77. }
  78. pr.write(sb.toString());
  79. pr.flush();
  80. }
  81. public static long read(int idx){
  82. long sum=0;
  83. while(idx>0){
  84. sum+=tree[idx];
  85. idx -= idx & -idx;
  86. }
  87. return sum;
  88. }
  89. public static void update(int idx,int val){
  90. while(idx<tree.length){
  91. tree[idx]+=val;
  92. idx += idx & -idx;
  93. }
  94. }
  95. static class Reader {
  96. final private int BUFFER_SIZE = 1 << 16;
  97. private DataInputStream din;
  98. private byte[] buffer;
  99. private int bufferPointer, bytesRead;
  100.  
  101. public Reader() {
  102. din = new DataInputStream(System.in);
  103. buffer = new byte[BUFFER_SIZE];
  104. bufferPointer = bytesRead = 0;
  105. }
  106.  
  107. public Reader(String file_name) throws IOException {
  108. din = new DataInputStream(new FileInputStream(file_name));
  109. buffer = new byte[BUFFER_SIZE];
  110. bufferPointer = bytesRead = 0;
  111. }
  112.  
  113. public String readLine() throws IOException {
  114. byte[] buf = new byte[64];
  115. int cnt = 0, c;
  116. while ((c = read()) != -1) {
  117. if (c == '\n')
  118. break;
  119. buf[cnt++] = (byte) c;
  120. }
  121. return new String(buf, 0, cnt);
  122. }
  123.  
  124. public int nextInt() throws IOException {
  125. int ret = 0;
  126. byte c = read();
  127. while (c <= ' ')
  128. c = read();
  129. boolean neg = (c == '-');
  130. if (neg)
  131. c = read();
  132. do {
  133. ret = ret * 10 + c - '0';
  134. } while ((c = read()) >= '0' && c <= '9');
  135. if (neg)
  136. return -ret;
  137. return ret;
  138. }
  139.  
  140. public long nextLong() throws IOException {
  141. long ret = 0;
  142. byte c = read();
  143. while (c <= ' ')
  144. c = read();
  145. boolean neg = (c == '-');
  146. if (neg)
  147. c = read();
  148. do {
  149. ret = ret * 10 + c - '0';
  150. } while ((c = read()) >= '0' && c <= '9');
  151. if (neg)
  152. return -ret;
  153. return ret;
  154. }
  155.  
  156. public double nextDouble() throws IOException {
  157. double ret = 0, div = 1;
  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. } while ((c = read()) >= '0' && c <= '9');
  167. if (c == '.')
  168. while ((c = read()) >= '0' && c <= '9')
  169. ret += (c - '0') / (div *= 10);
  170. if (neg)
  171. return -ret;
  172. return ret;
  173. }
  174.  
  175. private void fillBuffer() throws IOException {
  176. bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
  177. if (bytesRead == -1)
  178. buffer[0] = -1;
  179. }
  180.  
  181. private byte read() throws IOException {
  182. if (bufferPointer == bytesRead)
  183. fillBuffer();
  184. return buffer[bufferPointer++];
  185. }
  186.  
  187. public void close() throws IOException {
  188. if (din == null)
  189. return;
  190. din.close();
  191. }
  192. }
  193.  
  194. }
  195.  
Success #stdin #stdout 0.07s 380160KB
stdin
5
1 2 3 4 5
10
1 1
1 2
1 3
1 4
1 5
2 4
2 5
3 5
4 5
5 5

stdout
1
2
3
4
5
3
4
3
2
1