AhmedAshraff

Untitled

Sep 25th, 2025
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.79 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define boAshraf ios_base::sync_with_stdio(false); cin.tie(NULL);
  4. #define ll long long
  5. #define sz(s) (int)(s).size()
  6. #define all(s) (s).begin(),(s).end()
  7. using namespace std;
  8.  
  9. void File();
  10.  
  11. void sol();
  12.  
  13. const int N = 2e5 + 5;
  14. int freq[N], a[N];
  15.  
  16. vector<pair<int, int>> best(3, {-1, -1});
  17.  
  18. struct Mo {
  19.     int n, sq;
  20.  
  21.     Mo(int n) : n(n), sq(sqrt(n)) {}
  22.  
  23.     struct query {
  24.         int l, r, id, block_id;
  25.  
  26.         query(int l, int r, int id, int sq) : l(l), r(r), id(id), block_id(l / sq) {}
  27.  
  28.         bool operator<(const query &other) const {
  29.             if (block_id == other.block_id) return r < other.r;
  30.             return block_id < other.block_id;
  31.         }
  32.     };
  33.  
  34.     vector<query> Q;
  35.  
  36.     void addQuery(int l, int r, int id) {
  37.         Q.emplace_back(l, r, id, sq);
  38.     }
  39.  
  40.     void add(int i) {
  41.         if (!i) return;
  42.         int x = a[i];
  43.         int f = ++freq[x];
  44.         bool found = 0;
  45.         for (auto &[f2, x2]: best) {
  46.             if (x2 == x) {
  47.                 found = 1;
  48.                 f2 = f;
  49.             }
  50.         }
  51.         if (!found)best.emplace_back(f, x);
  52.  
  53.         sort(all(best), greater());
  54.         if (sz(best) > 3)best.pop_back();
  55.     }
  56.  
  57.     void remove(int i) {
  58.         if (!i) return;
  59.         int x = a[i];
  60.         int f = --freq[x];
  61.         bool found = 0;
  62.         for (auto &[f2, x2]: best) {
  63.             if (x2 == x) {
  64.                 found = 1;
  65.                 f2 = f;
  66.             }
  67.         }
  68.         if (!found)best.emplace_back(f, x);
  69.  
  70.         sort(all(best), greater());
  71.         if (sz(best) > 3)best.pop_back();
  72.     }
  73.  
  74.     vector<pair<int, int>> process() {
  75.         vector<pair<int, int>> ans(sz(Q),{-1,-1});
  76.         sort(all(Q));
  77.         int curL = 0, curR = -1; // 1-based
  78.         for (auto [l, r, id, block_Id]: Q) {
  79.             int need = (r - l + 1) / 3;
  80.             while (curL > l) add(--curL);
  81.             while (curR < r) add(++curR);
  82.             while (curL < l) remove(curL++);
  83.             while (curR > r) remove(curR--);
  84.  
  85.             sort(all(best), greater());
  86.  
  87.             int f1 = -1, x1 = -1;
  88.             int f2 = -1, x2 = -1;
  89.             tie(f1, x1) = best[0];
  90.             tie(f2, x2) = best[1];
  91.             if (f1 > need) ans[id].first = x1;
  92.             if (f2 > need) ans[id].second = x2;
  93.             assert(best[2].first <= need);
  94.         }
  95.         return ans;
  96.     }
  97. };
  98.  
  99. int main() {
  100.     boAshraf
  101.     File();
  102.     int t = 1;
  103.     cin >> t;
  104.     while (t--) sol();
  105.     return 0;
  106. }
  107.  
  108. void sol() {
  109.     int n, q;
  110.     cin >> n >> q;
  111.  
  112.     vector<int> ord;
  113.     for (int i = 1; i <= n; ++i) {
  114.         cin >> a[i];
  115.         ord.emplace_back(a[i]);
  116.     }
  117.     sort(all(ord));
  118.     ord.erase(unique(all(ord)), ord.end());
  119.  
  120.     int D = sz(ord);
  121.     for (int i = 1; i <= n; ++i) {
  122.         a[i] = (int) (lower_bound(all(ord), a[i]) - ord.begin());
  123.     }
  124.  
  125.     Mo mo(n);
  126.     for (int i = 0; i < q; ++i) {
  127.         int l, r;
  128.         cin >> l >> r;
  129.         mo.addQuery(l, r, i);
  130.     }
  131.  
  132.     auto ans = mo.process();
  133.  
  134.     for (auto [id1, id2]: ans) {
  135.         int ans1 = -1, ans2 = -1;
  136.         if (id1 != -1) ans1 = ord[id1];
  137.         if (id2 != -1) ans2 = ord[id2];
  138.         if (ans1 > ans2) swap(ans1, ans2);
  139.  
  140.         if (ans1 == -1 && ans2 == -1) {
  141.             cout << -1 << '\n';
  142.         } else if (ans1 == -1) {
  143.             assert(~ans2);
  144.             cout << ans2 << '\n';
  145.         } else {
  146.             assert(ans1 != ans2);
  147.             assert(~ans1);
  148.             assert(~ans2);
  149.             cout << ans1 << ' ' << ans2 << '\n';
  150.         }
  151.     }
  152.     best = vector<pair<int, int>>(3, {-1, -1});
  153.     for (int i = 0; i < D; ++i) freq[i] = 0;
  154. }
  155.  
  156. void File() {
  157. #ifndef ONLINE_JUDGE
  158.     freopen("input.txt", "r", stdin);
  159.     freopen("output.txt", "w", stdout);
  160. #endif
  161. }
  162.  
Advertisement
Add Comment
Please, Sign In to add comment