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’;
}
}

#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’;
}
}

More from author

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related posts

Advertismentspot_img

Latest posts

7 Reasons This Viral Kitchen Gadgets Is Worth Every Single Penny

📢 As an Amazon Associate, I earn from qualifying purchases.There comes a point when you realise that tolerating a frustrating situation is costing you...

Hong kong banks back student housing property boom

In an evolving commercial real estate landscape, Hong Kong banks are increasingly shifting their focus toward a rapidly growing asset class: student housing. Driven...

7 Reasons This Viral Kitchen Gadgets Is Worth Every Single Penny

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

Want to stay up to date with the latest news?

We would love to hear from you! Please fill in your details and we will stay in touch. It's that simple!