Advertisement
hkshakib

Untitled

Mar 26th, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int maxn = 1000005;
  5.  
  6. int ara[maxn], ans[maxn], cnt[maxn], n, q, block_size;
  7.  
  8. struct Query {
  9. int index, L, R;
  10. bool operator<(const Query& other) {
  11. return L/block_size < other.L/block_size ||
  12. (L/block_size == other.L/block_size && R<other.R);
  13. }
  14.  
  15. } query[maxn];
  16.  
  17. int distinct_elements;
  18.  
  19. void add(int idx) {
  20. cnt[ara[idx]]++;
  21. if(cnt[ara[idx]]==1)
  22. distinct_elements++;
  23. }
  24. void del(int idx) {
  25. cnt[ara[idx]]--;
  26. if(cnt[ara[idx]]==0)
  27. distinct_elements--;
  28. }
  29. int main()
  30. {
  31. int t,cas=0;
  32. scanf("%d",&t);
  33.  
  34. while(t--)
  35. {
  36. memset(ans,0,sizeof(ans));
  37. memset(cnt,0,sizeof(cnt));
  38. scanf("%d %d", &n,&q);
  39. for(int i=1; i<=n; i++)
  40. {
  41. scanf("%d", &arr[i]);
  42. }
  43. block = sqrt(n);
  44. for(int i=0; i<q; i++)
  45. {
  46. scanf("%d %d", &query[i].L, &query[i].R);
  47. query[i].index = i;
  48. }
  49. sort(query, query+q);
  50. int curL = 0, curR = -1;
  51. dis = 0;
  52. for(int i=0; i<q; i++)
  53. {
  54. int query_index = query[i].index;
  55. int l = query[i].L;
  56. int r = query[i].R;
  57.  
  58. while(curL<l)
  59. del(curL++);
  60. while(curL>l)
  61. add(--curL);
  62. while(curR<r)
  63. add(++curR);
  64. while(curR>r)
  65. del(curR--);
  66. ans[query_index] = distinct_elements;
  67. }
  68. printf("Case %d:\n",cas++);
  69. for(int i=0; i<q; i++)
  70. {
  71. printf("%d\n", ans[i]);
  72. }
  73. }
  74. return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement