fork download
  1. import java.util.*;
  2. import java.io.*;
  3. import java.math.*;
  4. class smack {
  5. static long mod=pow(10,9)+7;
  6. static class Query implements Comparable<Query>
  7. {
  8. private int l,r,in,size,k;
  9. public Query(int l, int r,int k,int in)
  10. {
  11. this.l = l;
  12. this.r = r;
  13. this.in=in;
  14. this.k=k;
  15. this.size=l/block;
  16. }
  17. @Override
  18. public int compareTo(Query o)
  19. {
  20. // TODO Auto-generated method stub
  21. if(this.size!=o.size)
  22. return this.size-o.size;
  23. else
  24. return this.r-o.r;
  25. }
  26. }
  27. static int block,freq[],above[];
  28. void solve()
  29. {
  30. int t=1;
  31. while(t--!=0)
  32. {
  33. int n=ni();
  34.  
  35. int a[]=na(n);
  36. int q=ni();
  37. HashMap<Integer,Integer> hm=new HashMap<Integer,Integer>();
  38. int temp[]=a.clone();
  39. Arrays.sort(temp);
  40. int count=0;
  41. hm.put(temp[0],++count);
  42. for(int i=1;i<n;i++)
  43. {
  44. if(temp[i]!=temp[i-1])
  45. hm.put(temp[i],++count);
  46. }
  47. for(int i=0;i<n;i++)
  48. a[i]=hm.get(a[i]);
  49. freq=new int[n+1];
  50. above=new int[n+1];
  51. block=(int)Math.sqrt(n);
  52. Query p[]=new Query[q];
  53. for(int i=0;i<q;i++)
  54. p[i]=new Query(ni()-1,ni()-1,1,i);
  55. Arrays.sort(p);
  56. int ans[]=new int[q];
  57. int curl=0,curr=-1;
  58. for(int i=0;i<q;i++)
  59. {
  60. int l=p[i].l;
  61. int r=p[i].r;
  62. while(curr>r)
  63. {
  64. delete(a[curr]);
  65. curr--;
  66. }
  67. while(curr<r)
  68. {
  69. curr++;
  70. add(a[curr]);
  71. }
  72. while(curl<l)
  73. {
  74. delete(a[curl]);
  75. curl++;
  76. }
  77. while(curl>l)
  78. {
  79. curl--;
  80. add(a[curl]);
  81. }
  82. ans[p[i].in]=above[0]-above[p[i].k];
  83. }
  84. for(int i=0;i<q;i++)
  85. out.println(ans[i]);
  86. }
  87. }
  88. public static void delete(int n)
  89. {
  90. if(freq[n]!=0){
  91. freq[n]--;
  92. above[freq[n]]--;
  93. }
  94. }
  95. public static void add(int n)
  96. {
  97. above[freq[n]]++;
  98. freq[n]++;
  99. }
  100. static long d, x, y;
  101. void extendedEuclid(long A, long B) {
  102. if(B == 0) {
  103. d = A;
  104. x = 1;
  105. y = 0;
  106. }
  107. else {
  108. extendedEuclid(B, A%B);
  109. long temp = x;
  110. x = y;
  111. y = temp - (A/B)*y;
  112. }
  113. }
  114. long modInverse(long A,long M) //A and M are coprime
  115. {
  116. extendedEuclid(A,M);
  117. return (x%M+M)%M; //x may be negative
  118. }
  119. public static void mergeSort(int[] arr, int l ,int r){
  120. if((r-l)>=1){
  121. int mid = (l+r)/2;
  122. mergeSort(arr,l,mid);
  123. mergeSort(arr,mid+1,r);
  124. merge(arr,l,r,mid);
  125. }
  126. }
  127. public static void merge(int arr[], int l, int r, int mid){
  128. int n1 = (mid-l+1), n2 = (r-mid);
  129. int left[] = new int[n1];
  130. int right[] = new int[n2];
  131. for(int i =0 ;i<n1;i++) left[i] = arr[l+i];
  132. for(int i =0 ;i<n2;i++) right[i] = arr[mid+1+i];
  133. int i =0, j =0, k = l;
  134. while(i<n1 && j<n2){
  135. if(left[i]>right[j]){
  136. arr[k++] = right[j++];
  137. }
  138. else{
  139. arr[k++] = left[i++];
  140. }
  141. }
  142. while(i<n1) arr[k++] = left[i++];
  143. while(j<n2) arr[k++] = right[j++];
  144. }
  145. public static void mergeSort(long[] arr, int l ,int r){
  146. if((r-l)>=1){
  147. int mid = (l+r)/2;
  148. mergeSort(arr,l,mid);
  149. mergeSort(arr,mid+1,r);
  150. merge(arr,l,r,mid);
  151. }
  152. }
  153. public static void merge(long arr[], int l, int r, int mid){
  154. int n1 = (mid-l+1), n2 = (r-mid);
  155. long left[] = new long[n1];
  156. long right[] = new long[n2];
  157. for(int i =0 ;i<n1;i++) left[i] = arr[l+i];
  158. for(int i =0 ;i<n2;i++) right[i] = arr[mid+1+i];
  159. int i =0, j =0, k = l;
  160. while(i<n1 && j<n2){
  161. if(left[i]>right[j]){
  162. arr[k++] = right[j++];
  163. }
  164. else{
  165. arr[k++] = left[i++];
  166. }
  167. }
  168. while(i<n1) arr[k++] = left[i++];
  169. while(j<n2) arr[k++] = right[j++];
  170. }
  171. static class Pair implements Comparable<Pair>{
  172.  
  173. int x,y,k,i;
  174.  
  175. Pair (int x,int y){
  176. this.x=x;
  177. this.y=y;
  178. }
  179.  
  180. public int compareTo(Pair o) {
  181. return this.x-o.x;
  182. }
  183.  
  184. public boolean equals(Object o) {
  185. if (o instanceof Pair) {
  186. Pair p = (Pair)o;
  187. return p.x == x && p.y == y && p.k==k;
  188. }
  189. return false;
  190. }
  191.  
  192.  
  193. @Override
  194. public String toString() {
  195. return "("+x + " " + y +" "+k+" "+i+" )";
  196. }
  197.  
  198. }
  199.  
  200. public static boolean isPal(String s){
  201. for(int i=0, j=s.length()-1;i<=j;i++,j--){
  202. if(s.charAt(i)!=s.charAt(j)) return false;
  203. }
  204. return true;
  205. }
  206. public static String rev(String s){
  207. StringBuilder sb=new StringBuilder(s);
  208. sb.reverse();
  209. return sb.toString();
  210. }
  211.  
  212. public static long gcd(long x,long y){
  213. if(x%y==0)
  214. return y;
  215. else
  216. return gcd(y,x%y);
  217. }
  218.  
  219. public static int gcd(int x,int y){
  220. if(x%y==0)
  221. return y;
  222. else
  223. return gcd(y,x%y);
  224. }
  225.  
  226. public static long gcdExtended(long a,long b,long[] x){
  227.  
  228. if(a==0){
  229. x[0]=0;
  230. x[1]=1;
  231. return b;
  232. }
  233. long[] y=new long[2];
  234. long gcd=gcdExtended(b%a, a, y);
  235.  
  236. x[0]=y[1]-(b/a)*y[0];
  237. x[1]=y[0];
  238.  
  239. return gcd;
  240. }
  241.  
  242. public static int abs(int a,int b){
  243. return (int)Math.abs(a-b);
  244. }
  245.  
  246. public static long abs(long a,long b){
  247. return (long)Math.abs(a-b);
  248. }
  249.  
  250. public static int max(int a,int b){
  251. if(a>b)
  252. return a;
  253. else
  254. return b;
  255. }
  256.  
  257. public static int min(int a,int b){
  258. if(a>b)
  259. return b;
  260. else
  261. return a;
  262. }
  263.  
  264. public static long max(long a,long b){
  265. if(a>b)
  266. return a;
  267. else
  268. return b;
  269. }
  270.  
  271. public static long min(long a,long b){
  272. if(a>b)
  273. return b;
  274. else
  275. return a;
  276. }
  277.  
  278. public static long pow(long n,long p,long m){
  279. long result = 1;
  280. if(p==0)
  281. return 1;
  282. if (p==1)
  283. return n;
  284. while(p!=0)
  285. {
  286. if(p%2==1)
  287. result *= n;
  288. if(result>=m)
  289. result%=m;
  290. p >>=1;
  291. n*=n;
  292. if(n>=m)
  293. n%=m;
  294. }
  295. return result;
  296. }
  297.  
  298. public static long pow(long n,long p){
  299. long result = 1;
  300. if(p==0)
  301. return 1;
  302. if (p==1)
  303. return n;
  304. while(p!=0)
  305. {
  306. if(p%2==1)
  307. result *= n;
  308. p >>=1;
  309. n*=n;
  310. }
  311. return result;
  312. }
  313. public static void debug(Object... o) {
  314. System.out.println(Arrays.deepToString(o));
  315. }
  316. void run() throws Exception {
  317. is = System.in;
  318. out = new PrintWriter(System.out);
  319. solve();
  320. out.flush();
  321. }
  322.  
  323. public static void main(String[] args) throws Exception {
  324. new Thread(null, new Runnable() {
  325. public void run() {
  326. try {
  327. new smack().run();
  328. } catch (Exception e) {
  329. e.printStackTrace();
  330. }
  331. }
  332. }, "1", 1 << 26).start();
  333. }
  334. private byte[] inbuf = new byte[1024];
  335. public int lenbuf = 0, ptrbuf = 0;
  336.  
  337. private int readByte() {
  338. if (lenbuf == -1)
  339. throw new InputMismatchException();
  340. if (ptrbuf >= lenbuf) {
  341. ptrbuf = 0;
  342. try {
  343. lenbuf = is.read(inbuf);
  344. } catch (IOException e) {
  345. throw new InputMismatchException();
  346. }
  347. if (lenbuf <= 0)
  348. return -1;
  349. }
  350. return inbuf[ptrbuf++];
  351. }
  352.  
  353. private boolean isSpaceChar(int c) {
  354. return !(c >= 33 && c <= 126);
  355. }
  356.  
  357. private int skip() {
  358. int b;
  359. while ((b = readByte()) != -1 && isSpaceChar(b));
  360. return b;
  361. }
  362.  
  363. private double nd() {
  364. return Double.parseDouble(ns());
  365. }
  366.  
  367. private char nc() {
  368. return (char) skip();
  369. }
  370.  
  371. private String ns() {
  372. int b = skip();
  373. StringBuilder sb = new StringBuilder();
  374. while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
  375. sb.appendCodePoint(b);
  376. b = readByte();
  377. }
  378. return sb.toString();
  379. }
  380.  
  381. private char[] ns(int n) {
  382. char[] buf = new char[n];
  383. int b = skip(), p = 0;
  384. while (p < n && !(isSpaceChar(b))) {
  385. buf[p++] = (char) b;
  386. b = readByte();
  387. }
  388. return n == p ? buf : Arrays.copyOf(buf, p);
  389. }
  390.  
  391. private char[][] nm(int n, int m) {
  392. char[][] map = new char[n][];
  393. for (int i = 0; i < n; i++)
  394. map[i] = ns(m);
  395. return map;
  396. }
  397.  
  398. private int[] na(int n) {
  399. int[] a = new int[n];
  400. for (int i = 0; i < n; i++)
  401. a[i] = ni();
  402. return a;
  403. }
  404.  
  405. private int ni() {
  406. int num = 0, b;
  407. boolean minus = false;
  408. while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
  409. ;
  410. if (b == '-') {
  411. minus = true;
  412. b = readByte();
  413. }
  414.  
  415. while (true) {
  416. if (b >= '0' && b <= '9') {
  417. num = num * 10 + (b - '0');
  418. } else {
  419. return minus ? -num : num;
  420. }
  421. b = readByte();
  422. }
  423. }
  424.  
  425. private long nl() {
  426. long num = 0;
  427. int b;
  428. boolean minus = false;
  429. while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
  430. ;
  431. if (b == '-') {
  432. minus = true;
  433. b = readByte();
  434. }
  435.  
  436. while (true) {
  437. if (b >= '0' && b <= '9') {
  438. num = num * 10 + (b - '0');
  439. } else {
  440. return minus ? -num : num;
  441. }
  442. b = readByte();
  443. }
  444. }
  445.  
  446. }
Success #stdin #stdout #stderr 0.06s 28008KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
java.util.InputMismatchException
	at smack.readByte(Main.java:341)
	at smack.ni(Main.java:410)
	at smack.solve(Main.java:38)
	at smack.run(Main.java:321)
	at smack$1.run(Main.java:329)
	at java.lang.Thread.run(Thread.java:745)