https://i2.wp.com/eblogarithm.com/wp-content/uploads/2017/08/Codeforces-Round-429-Div-1-problem-D-Destiny-Solution-In-CC1.png?fit=640%2C360

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

(Visited 17 times, 1 visits today)



There are no comments

Add yours

Leave a Reply

%d bloggers like this: