840D - Destiny - CodeForces Solution


data structures probabilities *2500

Please click on ads to support us..

C++ Code:

// v.resize(unique(all(v)) - v.begin());
// - That's what happens when you sit behind a desk.
// You forget things.
// Like the weight in the hand of a gun that's loaded and one that's not. -
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define ppb pop_back
#define fast_io ios::sync_with_stdio(false);cin.tie(NULL);
#define file_io freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define FOR(i,k,n) for(int i = k; i < n; ++ i)
#define debf cout<<"(0-0)\n";
#define all(x) x.begin(), x.end()
#define dec(x) cout << fixed << setprecision(x);
#define pf push_front
#define ppf pop_front
#define dash " ------- "
#define what(x) cerr << #x << " is " << x << endl;
#define eb emplace_back
//#define int short int
//#define int long long
#define sz(s) (int) (s.size())
#define fl cout.flush()

using namespace std;

typedef long long ll;
typedef pair <int, int> pii;
typedef pair <int, pii> pip;
typedef pair <pii, int> ppi;
typedef pair <ll, ll> pll;
typedef unsigned long long ull;
typedef long double ld;

template <class T> using max_heap = priority_queue <T, vector <T>, less <T> >;
template <class T> using min_heap = priority_queue <T, vector <T>, greater <T> >;

constexpr int MOD = 1e9 + 7, N = 3e5 + 8, M = 1e4 + 8, INF = 1e9 + 8, LGN = 20, mod = 998244353;
constexpr long double eps = 1e-8, pi = 3.14159265359;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n, q, a[N], vis[N], rn[200];
vector <int> tmp;
vector <int> v[N];

int main(){
  fast_io;
  cin >> n >> q;
  FOR (i, 1, n+1){
    cin >> a[i];
    v[a[i]].pb(i);
  }
  FOR (i, 0, 70){
    rn[i] = rng() % MOD;
  }
  while (q --){
    int l, r, k;
    cin >> l >> r >> k;
    int ans = INF;
    int cnst = (r - l + 1) / k;
    for (int i = 0; i < 70; ++ i){
      int x = a[rn[i] % (r-l+1) + l];
      if (x >= ans || vis[x] || v[x].size() <= (r - l + 1) / k){
        continue;
      }
      tmp.pb(x);
      vis[x] = 1;
      int sz = upper_bound(all(v[x]), r) - lower_bound(all(v[x]), l);
      if (sz > cnst){
        ans = x;
      }
    }
    cout << (ans == INF ? -1 : ans) << '\n';
    for (int i : tmp){
      vis[i] = 0;
    }
    tmp.clear();
  }
  return 0;
}

// Yesterday is history
// Tomorrow is a mystery
// but today is a gift
// That is why it is called the present


Comments

Submit
0 Comments
More Questions

22E - Scheme
1566A - Median Maximization
1278A - Shuffle Hashing
1666F - Fancy Stack
1354A - Alarm Clock
1543B - Customising the Track
1337A - Ichihime and Triangle
1366A - Shovels and Swords
919A - Supermarket
630C - Lucky Numbers
1208B - Uniqueness
1384A - Common Prefixes
371A - K-Periodic Array
1542A - Odd Set
1567B - MEXor Mixup
669A - Little Artem and Presents
691B - s-palindrome
851A - Arpa and a research in Mexican wave
811A - Vladik and Courtesy
1006B - Polycarp's Practice
1422A - Fence
21D - Traveling Graph
1559B - Mocha and Red and Blue
1579C - Ticks
268B - Buttons
898A - Rounding
1372B - Omkar and Last Class of Math
1025D - Recovering BST
439A - Devu the Singer and Churu the Joker
1323A - Even Subset Sum Problem