Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define boAshraf ios_base::sync_with_stdio(false); cin.tie(NULL);
- #define ll long long
- #define sz(s) (int)(s).size()
- #define all(s) (s).begin(),(s).end()
- using namespace std;
- void File();
- void sol();
- const int N = 2e5 + 5;
- int freq[N], a[N];
- vector<pair<int, int>> best(3, {-1, -1});
- struct Mo {
- int n, sq;
- Mo(int n) : n(n), sq(sqrt(n)) {}
- struct query {
- int l, r, id, block_id;
- query(int l, int r, int id, int sq) : l(l), r(r), id(id), block_id(l / sq) {}
- bool operator<(const query &other) const {
- if (block_id == other.block_id) return r < other.r;
- return block_id < other.block_id;
- }
- };
- vector<query> Q;
- void addQuery(int l, int r, int id) {
- Q.emplace_back(l, r, id, sq);
- }
- void add(int i) {
- if (!i) return;
- int x = a[i];
- int f = ++freq[x];
- bool found = 0;
- for (auto &[f2, x2]: best) {
- if (x2 == x) {
- found = 1;
- f2 = f;
- }
- }
- if (!found)best.emplace_back(f, x);
- sort(all(best), greater());
- if (sz(best) > 3)best.pop_back();
- }
- void remove(int i) {
- if (!i) return;
- int x = a[i];
- int f = --freq[x];
- bool found = 0;
- for (auto &[f2, x2]: best) {
- if (x2 == x) {
- found = 1;
- f2 = f;
- }
- }
- if (!found)best.emplace_back(f, x);
- sort(all(best), greater());
- if (sz(best) > 3)best.pop_back();
- }
- vector<pair<int, int>> process() {
- vector<pair<int, int>> ans(sz(Q),{-1,-1});
- sort(all(Q));
- int curL = 0, curR = -1; // 1-based
- for (auto [l, r, id, block_Id]: Q) {
- int need = (r - l + 1) / 3;
- while (curL > l) add(--curL);
- while (curR < r) add(++curR);
- while (curL < l) remove(curL++);
- while (curR > r) remove(curR--);
- sort(all(best), greater());
- int f1 = -1, x1 = -1;
- int f2 = -1, x2 = -1;
- tie(f1, x1) = best[0];
- tie(f2, x2) = best[1];
- if (f1 > need) ans[id].first = x1;
- if (f2 > need) ans[id].second = x2;
- assert(best[2].first <= need);
- }
- return ans;
- }
- };
- int main() {
- boAshraf
- File();
- int t = 1;
- cin >> t;
- while (t--) sol();
- return 0;
- }
- void sol() {
- int n, q;
- cin >> n >> q;
- vector<int> ord;
- for (int i = 1; i <= n; ++i) {
- cin >> a[i];
- ord.emplace_back(a[i]);
- }
- sort(all(ord));
- ord.erase(unique(all(ord)), ord.end());
- int D = sz(ord);
- for (int i = 1; i <= n; ++i) {
- a[i] = (int) (lower_bound(all(ord), a[i]) - ord.begin());
- }
- Mo mo(n);
- for (int i = 0; i < q; ++i) {
- int l, r;
- cin >> l >> r;
- mo.addQuery(l, r, i);
- }
- auto ans = mo.process();
- for (auto [id1, id2]: ans) {
- int ans1 = -1, ans2 = -1;
- if (id1 != -1) ans1 = ord[id1];
- if (id2 != -1) ans2 = ord[id2];
- if (ans1 > ans2) swap(ans1, ans2);
- if (ans1 == -1 && ans2 == -1) {
- cout << -1 << '\n';
- } else if (ans1 == -1) {
- assert(~ans2);
- cout << ans2 << '\n';
- } else {
- assert(ans1 != ans2);
- assert(~ans1);
- assert(~ans2);
- cout << ans1 << ' ' << ans2 << '\n';
- }
- }
- best = vector<pair<int, int>>(3, {-1, -1});
- for (int i = 0; i < D; ++i) freq[i] = 0;
- }
- void File() {
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- }
Advertisement
Add Comment
Please, Sign In to add comment