Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <ctime>
  6. #include <random>
  7.  
  8. #define f first
  9. #define s second
  10. #define pb push_back
  11. using namespace std;
  12.  
  13. template<typename T1, typename T2> inline void chkmin(T1 & x, T2 y) {if (x > y) x = y;}
  14. template<typename T1, typename T2> inline void chkmax(T1 & x, T2 y) {if (x < y) x = y;}
  15.  
  16. typedef long long ll;
  17.  
  18. const int MAXN = 3e5 + 1, BLOCK = 550, INF = 1e9;
  19. int a[MAXN], cnt[MAXN], ans1[MAXN];
  20. set<pair<int, int>> s;
  21.  
  22. bool cmp(pair<pair<int, int>, pair<int, int>> & a, pair<pair<int, int>, pair<int, int>> & b) {
  23.     int bla = a.f.f / BLOCK, blb = b.f.f / BLOCK;
  24.     if (bla != blb) return bla < blb;
  25.     return a.f.s < b.f.s;
  26. }
  27.  
  28. int main() {
  29.     ios_base::sync_with_stdio(false);
  30.     cin.tie(0), cout.tie(0);
  31.     // freopen("input.txt", "r", stdin);
  32.     mt19937 shit(time(0));
  33.     int n, q;
  34.     cin >> n >> q;
  35.     for (int i = 0; i < n; ++i) {
  36.         cin >> a[i];
  37.     }
  38.     vector<pair<pair<int, int>, pair<int, int>>> reqs;
  39.     reqs.reserve(q);
  40.     for (int i = 0; i < q; ++i) {
  41.         int l, r, k;
  42.         cin >> l >> r >> k, --l, --r;
  43.         reqs.pb({{l, r}, {k, i}});
  44.     }
  45.     // cout << "done\n";
  46.     sort(reqs.begin(), reqs.end(), cmp);
  47.     int l = 0, r = -1;
  48.     for (auto i : reqs) {
  49.         while (r < i.f.s)
  50.             ++cnt[a[++r]];
  51.         while (r > i.f.s)
  52.             --cnt[a[r--]];
  53.         while (l < i.f.f)
  54.             --cnt[a[l++]];
  55.         while (l > i.f.f)
  56.             ++cnt[a[--l]];
  57.        
  58.         int ans = INF;
  59.         int mn_met = (i.f.s - i.f.f + 1) / i.s.f + 1;
  60.         // cout << i.s.s << ' ' << mn_met << endl;
  61.         int len = i.f.s - i.f.f + 1;
  62.         for (int j = 0; j < 100; ++j) {
  63.             int kek = a[i.f.f + shit() % len];
  64.             if (cnt[kek] >= mn_met)
  65.                 chkmin(ans, kek);
  66.         }
  67.         if (ans == INF)
  68.             ans = -1;
  69.         ans1[i.s.s] = ans;
  70.     }
  71.     // cout << endl << endl;
  72.     for (int i = 0; i < q; ++i)
  73.         cout << ans1[i] << '\n';
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement