Advertisement
Anon2005

Untitled

Oct 10th, 2020
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. #include <fstream>
  2. #include <algorithm>
  3. #include <cmath>
  4. using namespace std;
  5. ifstream cin("rangemode.in");
  6. ofstream cout("rangemode.out");
  7. int v[100005];
  8. int rez[100005];
  9. int f[100005];
  10. struct qu
  11. {
  12. int st,dr,poz,b;
  13. } q[100005];
  14. int max1,val;
  15. bool xort(qu a, qu b)
  16. {
  17. if(a.b<b.b)
  18. return true;
  19. if(a.b>b.b)
  20. return false;
  21. if(a.dr<b.dr)
  22. return true;
  23. return false;
  24. }
  25. void act(int poz, int care)
  26. {
  27. f[v[poz]]++;
  28. if(f[v[poz]] == max1 && v[poz] < val)
  29. val = v[poz];
  30. if(f[v[poz]]>max1)
  31. {
  32. max1 = f[v[poz]];
  33. val = v[poz];
  34. }
  35. }
  36. int main()
  37. {
  38. int n,m,i,k,poz,j,pozcazan;
  39. cin>>n>>m;
  40. for(i = 1; i <= n; i++)
  41. cin >> v[i];
  42. k = 320;
  43. for(i = 1; i <= m; i++)
  44. {
  45. cin >> q[i].st >> q[i].dr;
  46. q[i].poz = i;
  47. q[i].b = q[i].st/k+1;
  48. }
  49. sort(q + 1, q + m + 1, xort);
  50. i=1;
  51. while(i<=m)
  52. {
  53. poz = q[i].b*k;
  54. j=i;
  55. while(j<=m&&q[j].b == q[i].b)
  56. {
  57. max1=0;
  58. val=0;
  59. while(poz <= q[j].dr)
  60. {
  61. act(poz,1);
  62. poz++;
  63. }
  64. pozcazan = min(q[j].b*k - 1, q[j].dr);
  65. for(int x = q[j].st; x <= pozcazan; x++)
  66. act(x,2);
  67. for(int x = q[j].st ; x <= pozcazan; x++)
  68. f[v[x]]--;
  69. rez[q[j++].poz]=val;
  70. }
  71. i=j;
  72. for(j = 1; j <= 100000; j++)
  73. f[j]=0;
  74. }
  75. for(i = 1; i <= m; i++)
  76. cout<<rez[i]<<'\n';
  77. return 0;
  78. }
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement