a53

fsecv

a53
Jun 28th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. ifstream f("fsecv.in");
  5. ofstream g("fsecv.out");
  6.  
  7. const int NM = 100003;
  8. int n, block, q;
  9.  
  10. struct query{
  11. int l, r, k, p;
  12. bool operator < (const query &aux)const{
  13. if(l / block != aux.l / block) return l / block < aux.l / block;
  14. return r < aux.r;
  15. }
  16. } Q[NM];
  17. int a[NM], nr[NM], ap[2*NM], sol[NM];
  18.  
  19. void add(int i)
  20. {
  21. int x = a[i] + 100000;
  22. if(ap[x] > 0) --nr[ap[x]];
  23. ++ap[x];
  24. ++nr[ap[x]];
  25. }
  26.  
  27. void scoate(int i)
  28. {
  29. int x = a[i] + 100000;
  30. if(ap[x] > 0) --nr[ap[x]];
  31. --ap[x];
  32. ++nr[ap[x]];
  33. }
  34.  
  35. int main()
  36. {
  37. f >> n >> q;
  38.  
  39. for(int i = 1; i <= n; ++i)
  40. f >> a[i];
  41.  
  42. for(int i = 1; i <= q; ++i){
  43. f >> Q[i].l >> Q[i].r >> Q[i].k;
  44. Q[i].p = i;
  45. }
  46.  
  47. block = sqrt(n);
  48. sort(Q + 1, Q + q + 1);
  49.  
  50. int st = 1, dr = 0;
  51. for(int i = 1; i <= q; ++i){
  52.  
  53. while(st > Q[i].l) --st, add(st);
  54. while(dr < Q[i].r) ++dr, add(dr);
  55. while(st < Q[i].l) scoate(st), ++st;
  56. while(dr > Q[i].r) scoate(dr), --dr;
  57.  
  58. sol[Q[i].p] = nr[Q[i].k];
  59. }
  60.  
  61. for(int i = 1; i <= q; ++i)
  62. g << sol[i] << '\n';
  63.  
  64. return 0;
  65. }
Add Comment
Please, Sign In to add comment