Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. typedef long long ll;
  4.  
  5. #define sc second
  6. #define fs first
  7. #define pb push_back
  8. #define all(x) x.begin(), x.end()
  9. #define rall(x) x.rbegin(), x.rend()
  10.  
  11. using namespace std;
  12.  
  13. struct z{
  14. ll l, r, p;
  15. z(ll nl, ll nr, ll np){
  16. p = np;
  17. l = nl;
  18. r = nr;
  19. }
  20. };
  21.  
  22. bool comp(z a, z b){
  23. return a.r < b.r;
  24. }
  25.  
  26. int main()
  27. {
  28. ifstream cin("input.txt");
  29. ofstream cout("output.txt");
  30. ll n, m;
  31. cin >> n;
  32. vector<pair<ll, ll> >mas(n);
  33. vector<ll>b(n), ma1(n, 0), ma2(n, 0);
  34.  
  35. for(int i = 0; i < n; i++){
  36. cin >> mas[i].fs;
  37. mas[i].sc = i;
  38. }
  39.  
  40. sort(all(mas));
  41.  
  42. int k = 0;
  43. b[mas[0].sc] = 0;
  44. for(int i = 1; i < n; i++){
  45. if(mas[i].fs != mas[i-1].fs)
  46. k++;
  47. b[mas[i].sc] = k;
  48. }
  49.  
  50. ll p = sqrt(n+2);
  51. p++;
  52. cin >> m;
  53. vector<vector<z> > a(p);
  54. vector<ll>ans(m);
  55.  
  56. for(int i = 0; i < m; i++){
  57. ll l, r;
  58. cin >> l >> r;
  59. l--; r--;
  60. if(r-l+1 <= p){
  61. ll an = 0;
  62. for(int j = l; j <= r; j++){
  63. ma1[b[j]]++;
  64. an = max(ma1[b[j]], an);
  65. }
  66. ans[i] = an;
  67. for(int j = l; j <= r; j++){
  68. --ma1[b[j]];
  69. }
  70. }
  71. else{
  72. a[l/p].pb(z(l, r, i));
  73. }
  74. }
  75.  
  76. for(int i = 0; i < p; i++){
  77. sort(all(a[i]), comp);
  78. ll pp = p * (i+1);
  79. if(a[i].size() == 0)
  80. continue;
  81. ll max1 = 0, max2 = 0;
  82. ll z = pp - 1;
  83.  
  84. for(int o = 0; o < a[i].size(); o++){
  85. for(int j = z + 1; j <= a[i][o].r; j++){
  86. max1 = max(++ma1[b[j]], max1);
  87. ma2[b[j]]++;
  88. }
  89. max2 = max1;
  90. z = a[i][o].r;
  91. for(int j = a[i][o].l; j < pp; j++){
  92. max2 = max(++ma2[b[j]], max2);
  93. }
  94. for(int j = a[i][o].l; j < pp; j++){
  95. --ma2[b[j]];
  96. }
  97. ans[a[i][o].p] = max2;
  98. max2 = max1;
  99. }
  100.  
  101. for(int j = pp; j <= a[i][a[i].size()-1].r; j++){
  102. ma1[b[j]] = 0;
  103. ma2[b[j]] = 0;
  104. }
  105.  
  106. }
  107.  
  108. for(int i = 0; i < m; i++)
  109. cout << ans[i] << "\n";
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement