Advertisement
IanisBelu

median_query

Nov 23rd, 2023
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.15 KB | Source Code | 0 0
  1. #include <unordered_map>
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <fstream>
  5. #include <vector>
  6.  
  7. #define sz(a) ((int)(a).size())
  8. #define all(a) (a).begin(), (a).end()
  9.  
  10. #define fi first
  11. #define se second
  12.  
  13. #define lsb(x) (x & (-x))
  14.  
  15. using namespace std;
  16.  
  17. #ifdef LOCAL
  18. ifstream fin("input.txt");
  19. #define fout cout
  20. #else
  21. #define FILE_NAME "median_query"
  22. class InParser {
  23. private:
  24.     FILE *fin;
  25.     char *buff;
  26.     int sp;
  27.  
  28.     char read_ch() {
  29.         ++sp;
  30.         if (sp == 4096) {
  31.             sp = 0;
  32.             fread(buff, 1, 4096, fin);
  33.         }
  34.         return buff[sp];
  35.     }
  36.  
  37. public:
  38.     InParser(const char* nume) {
  39.         fin = fopen(nume, "r");
  40.         buff = new char[4096]();
  41.         sp = 4095;
  42.     }
  43.  
  44.     InParser& operator >> (int &n) {
  45.         char c;
  46.         while (!isdigit(c = read_ch()) && c != '-');
  47.         int sgn = 1;
  48.         if (c == '-') {
  49.             n = 0;
  50.             sgn = -1;
  51.         } else {
  52.             n = c - '0';
  53.         }
  54.         while (isdigit(c = read_ch())) {
  55.             n = 10 * n + c - '0';
  56.         }
  57.         n *= sgn;
  58.         return *this;
  59.     }
  60.  
  61.     InParser& operator >> (long long &n) {
  62.         char c;
  63.         n = 0;
  64.         while (!isdigit(c = read_ch()) && c != '-');
  65.         long long sgn = 1;
  66.         if (c == '-') {
  67.             n = 0;
  68.             sgn = -1;
  69.         } else {
  70.             n = c - '0';
  71.         }
  72.         while (isdigit(c = read_ch())) {
  73.             n = 10 * n + c - '0';
  74.         }
  75.         n *= sgn;
  76.         return *this;
  77.     }
  78. };
  79. InParser fin(FILE_NAME ".in");
  80. ofstream fout(FILE_NAME ".out");
  81. #define endl '\n'
  82. #endif
  83.  
  84. const int NMAX = 1e5+5;
  85. const int INF = 1e9+5;
  86.  
  87. struct Query {
  88.     int l, r, idx;
  89. };
  90.  
  91. int n, m;
  92. int a[NMAX];
  93. Query q[NMAX];
  94. int aib[NMAX];
  95. int ans[NMAX];
  96. int bucket[NMAX];
  97. unordered_map<int, int> val;
  98.  
  99. void aib_update(int pos, int val) {
  100.     for (int i = pos; i <= n; i += lsb(i))
  101.             aib[i] += val;
  102. }
  103.  
  104. int aib_query(int pos) {
  105.     int ret = 0;
  106.     for (int i = pos; i >= 1; i -= lsb(i))
  107.         ret += aib[i];
  108.     return ret;
  109. }
  110.  
  111. void read() {
  112.     fin >> n >> m;
  113.     for (int i = 1; i <= n; i++)
  114.         fin >> a[i];
  115.     for (int i = 1; i <= m; i++) {
  116.         fin >> q[i].l >> q[i].r;
  117.         q[i].idx = i;
  118.     }
  119. }
  120.  
  121. void precalc() {
  122.     int bsz = 2 * sqrt(n);
  123.     for (int i = 1; i <= n; i++)
  124.         bucket[i] = (i - 1) / bsz + 1;
  125.  
  126.     vector<int*> v;
  127.     for (int i = 1; i <= n; i++)
  128.         v.push_back(a + i);
  129.    
  130.     sort(all(v), [](const int *a, const int *b) {
  131.         return *a < *b;
  132.     });
  133.  
  134.     val[1] = *v[0];
  135.     *v[0] = 1;
  136.     int mx_val = 1;
  137.  
  138.     for (int i = 1; i < n; i++) {
  139.         mx_val += *v[i] != *v[i - 1];
  140.         val[mx_val] = *v[i];
  141.         *v[i] = mx_val;
  142.     }
  143. }
  144.  
  145. bool cmp_mo(const Query &a, const Query &b) {
  146.     if (bucket[a.l] == bucket[b.l]) return a.r < b.r;
  147.     return a.l < b.l;
  148. }
  149.  
  150. int bs(int len) {
  151.     int l = 1, r = n;
  152.     while (l <= r) {
  153.         int mid = (l + r) >> 1;
  154.         if (2 * aib_query(mid) < len)
  155.             l = mid + 1;
  156.         else
  157.             r = mid - 1;
  158.     }
  159.     return r + 1;
  160. }
  161.  
  162. void solve() {
  163.     sort(q + 1, q + m + 1, cmp_mo);
  164.  
  165.     int l = 1, r = 1;
  166.     aib_update(a[1], 1);
  167.  
  168.     for (int i = 1; i <= m; i++) {
  169.         int ql = q[i].l, qr = q[i].r;
  170.  
  171.         while (l < ql) aib_update(a[l++], -1);
  172.         while (qr < r) aib_update(a[r--], -1);
  173.         while (ql < l) aib_update(a[--l], 1);
  174.         while (r < qr) aib_update(a[++r], 1);
  175.  
  176.         ans[q[i].idx] = val[bs(r - l + 1)];
  177.     }
  178.  
  179.     for (int i = 1; i <= m; i++)
  180.         fout << ans[i] << endl;
  181. }
  182.  
  183. int main() {
  184.     read();
  185.     precalc();
  186.     solve();
  187.     return 0;
  188. }
  189.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement