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