Codeforces Round #429 (Div. 1), problem: (D) Destiny Solution In C/C++

#include <bits/stdc++.h>

using namespace std;

const int maxN = 3e5+10, base = 1e9+7;
int N, Q, ans, valmid, A[maxN];

bool cmp(int x) { return x <= valmid; }

struct wavelet_tree
{
int low, high;
wavelet_tree *L, *R;
vector <int> tmp;

wavelet_tree(int *u, int *v, int x, int y)
{
low = x; high = y;
if (low == high || u >= v) return;
valmid = (low + high)/2;

tmp.reserve(v-u+1);
tmp.push_back(0);
for (int* i=u; i != v; i++)
tmp.push_back(tmp.back() + (*i <= valmid));

int *p = stable_partition(u, v, cmp);
L = new wavelet_tree(u, p, x, valmid);
R = new wavelet_tree(p, v, valmid+1, y);
}

int occur(int l, int r, int num)
{
if (r-l+1 < num) return base;
if (low == high)
{
if (r-l+1 >= num) return low;
return base;
}
int lb = tmp[l-1], rb = tmp[r];
return min(this->L->occur(lb+1, rb, num), this->R->occur(l-lb, r-rb, num));
}
};

int main()
{
ios_base :: sync_with_stdio(0);
cin >> N >> Q;
for (int i=1; i <= N; i++) cin >> A[i];
wavelet_tree T(A+1, A+N+1, 1, N);
while (Q–)
{
int l, r, k;
cin >> l >> r >> k;
k = (r-l+1)/k + 1;
ans = T.occur(l, r, k);
if (ans == base) ans = -1;
cout << ans << ‘\n’;
}
}

Advertisement

spot_img

Is Viral Kitchen Gadgets...

📢 As an Amazon Associate, I earn from qualifying...

Power bank triggers fire...

A routine afternoon journey turned into a moment of...

The Real Truth About...

📢 As an Amazon Associate, I earn from qualifying...

Can a man kill...

Pop culture and action cinema often feed the dramatic...

7 Reasons This Viral...

📢 As an Amazon Associate, I earn from qualifying...

Can you have just...

Welcoming a pet ferret into your home is an...

Is Viral Kitchen Gadgets Worth Your Money Here Is Our Full Review

📢 As an Amazon Associate, I earn from qualifying purchases.If you had told me six months ago that a single purchase would change how...

Power bank triggers fire in Mumbai local train, no injury reported

A routine afternoon journey turned into a moment of acute terror for commuters on Central Railway after a portable power bank abruptly caught fire...

The Real Truth About Viral Kitchen Gadgets Before You Buy

📢 As an Amazon Associate, I earn from qualifying purchases.Most products in this category disappoint within weeks. Cheap materials, vague instructions, and results that...

Can a man kill a bear with his hands?

Pop culture and action cinema often feed the dramatic fantasy of human triumph over apex predators, sparking a persistent debate: can a man kill...

7 Reasons This Viral Kitchen Gadgets Is Worth Every Single Penny

📢 As an Amazon Associate, I earn from qualifying purchases.Most products in this category disappoint within weeks. Cheap materials, vague instructions, and results that...

Can you have just one ferret?

Welcoming a pet ferret into your home is an exciting decision, but it frequently prompts an essential question for prospective owners: can you have...

We Tested Viral Kitchen Gadgets Here Is What You Must Know

📢 As an Amazon Associate, I earn from qualifying purchases.If you had told me six months ago that a single purchase would change how...

7 Reasons This Viral Kitchen Gadgets Is Worth Every Single Penny

📢 As an Amazon Associate, I earn from qualifying purchases.After putting the Viral Kitchen Gadgets through a proper 30-day real-world test, I can tell...

Building India’s deep-tech enterprise pipeline: from IP to IPO

India’s startup ecosystem has earned global acclaim for its consumer tech and software-as-a-service (SaaS) platforms. However, the nation’s next major economic frontier lies in...