Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <set>
- #include <vector>
- #include <algorithm>
- #include <ctime>
- #include <random>
- #define f first
- #define s second
- #define pb push_back
- using namespace std;
- template<typename T1, typename T2> inline void chkmin(T1 & x, T2 y) {if (x > y) x = y;}
- template<typename T1, typename T2> inline void chkmax(T1 & x, T2 y) {if (x < y) x = y;}
- typedef long long ll;
- const int MAXN = 3e5 + 1, BLOCK = 550, INF = 1e9;
- int a[MAXN], cnt[MAXN], ans1[MAXN];
- set<pair<int, int>> s;
- bool cmp(pair<pair<int, int>, pair<int, int>> & a, pair<pair<int, int>, pair<int, int>> & b) {
- int bla = a.f.f / BLOCK, blb = b.f.f / BLOCK;
- if (bla != blb) return bla < blb;
- return a.f.s < b.f.s;
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(0), cout.tie(0);
- // freopen("input.txt", "r", stdin);
- mt19937 shit(time(0));
- int n, q;
- cin >> n >> q;
- for (int i = 0; i < n; ++i) {
- cin >> a[i];
- }
- vector<pair<pair<int, int>, pair<int, int>>> reqs;
- reqs.reserve(q);
- for (int i = 0; i < q; ++i) {
- int l, r, k;
- cin >> l >> r >> k, --l, --r;
- reqs.pb({{l, r}, {k, i}});
- }
- // cout << "done\n";
- sort(reqs.begin(), reqs.end(), cmp);
- int l = 0, r = -1;
- for (auto i : reqs) {
- while (r < i.f.s)
- ++cnt[a[++r]];
- while (r > i.f.s)
- --cnt[a[r--]];
- while (l < i.f.f)
- --cnt[a[l++]];
- while (l > i.f.f)
- ++cnt[a[--l]];
- int ans = INF;
- int mn_met = (i.f.s - i.f.f + 1) / i.s.f + 1;
- // cout << i.s.s << ' ' << mn_met << endl;
- int len = i.f.s - i.f.f + 1;
- for (int j = 0; j < 100; ++j) {
- int kek = a[i.f.f + shit() % len];
- if (cnt[kek] >= mn_met)
- chkmin(ans, kek);
- }
- if (ans == INF)
- ans = -1;
- ans1[i.s.s] = ans;
- }
- // cout << endl << endl;
- for (int i = 0; i < q; ++i)
- cout << ans1[i] << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement