F / CCD 的不难题
挺套路的。
思路
naive 解法
-
第一步,按照每个数在序列中出现的次数平衡规划。
-
如果询问的出现次数大于
B 。因为所有出现次数大于B 的数至多有\frac{N}{B} 个,预处理的时候将这些数全部取出来,查询时挨个挨个枚举 check 是否满足当前询问的条件,从满足条件的数中取最大值即可。 -
-
如果询问的出现次数小于
B 。- 对序列分块,设块长为
B_1 ,当前询问为(L,R,K) 。 - 若
L,R 同块,暴力枚举区间内每一个元素,直接统计答案,O(B_1) 。 - 若
L,R 不同块,设询问区间中数列整块编号为[S,T] :- 预处理
cnt_{i,j} 表示前i 个数列整块,数字j 的出现次数。 - 设
tcnt_{i} 表示数字i 在当前询问数列散块中出现次数,这个可以每次询问暴力求。 - 此时一个数
x 在询问区间中出现次数可以表示为cnt_{T,x} - cnt_{S - 1, x} + tcnt_{x} ,也就是说,我们可以O(1) check 一个数的出现次数是否合法,此时的问题就在于如何快速找到一个合法的数。(暴力枚举每一个数去 check 是O(N) 的) - 对值域分块,设块长为
B_2 ,则: - 我们设
tnum_{i} 表示在i 号值域块中的数存在几个数在该询问区间中合法。此时,我们从大到小枚举每一个值域块,如果当前值域块的tnum 有值,就暴力枚举该值域块中所有O(B_2) 个数,用上文办法 check,即可做到在O(\frac{V}{B_2} + B2) 的时间复杂度内回答询问。 - 问题转为快速处理
tnum 。不难发现,如果我们可以快速求出只考虑数列整块[S,T] 内整数时的tnum ,通过维护数列散块内元素的变化量,就可以在O(B_1) 的时间复杂度内处理出tnum 。 - 此时问题简化为求在数列整块
[S,T] 中,数的大小在值域整块x 内,出现K 次的数的个数。 - 预处理
Rt_{S,T,K} 表示一颗线段树的根,该线段树第x 个元素的值表示在数列整块[S,T] 中,数的大小在值域整块x 内,出现K 次的数的个数,如果将该线段树可持久化,即所有S 相同的Rt_{S,T,K} 使用一颗可持久化线段树(值域大小为O(\frac{V}{B_2}) ),即可查询。
- 预处理
- 对序列分块,设块长为
inline void upd(int &x, int l, int r, int now, int c); // 在以 x 为根的可持久化线段树上,now 位置的值加上 c。
REP(i, 1, W1) {
REP(j, 1, n) tcnt[j] = 0;
REP(j, 1, W2) upd(Rt[i][i][0], 1, W2, j, 1);
REP(j, i, W1) {
REP(k, 1, B) Rt[i][j][k] = Rt[i][max(i, j - 1)][k];
REP(k, L1[j], R1[j]) {
if (tcnt[a[k]] <= B) upd(Rt[i][j][tcnt[a[k]]], 1, W2, bel2[a[k]], -1);
++tcnt[a[k]];
if (tcnt[a[k]] <= B) upd(Rt[i][j][tcnt[a[k]]], 1, W2, bel2[a[k]], 1);
}
}
}
通过调整块长,可以做到
优化
发现可持久化线段树非常不平衡,考虑将对可持久化线段树的操作(所有对
// 代替原线段树操作
inline void upd(int Rt, int now, int c) {tr[Rt].emplace_back(now, c);}
// 修改
memset(tcnt, 0, sizeof(tcnt));
REP(i, 1, W1) {
REP(j, 1, n) tcnt[j] = 0;
REP(j, i, W1) {
REP(k, 1, B) Rt[i][j][k] = ++Trc;
REP(k, L1[j], R1[j]) {
if (tcnt[a[k]] <= B) upd(Rt[i][j][tcnt[a[k]]], bel2[a[k]], -1); // 提前存操作
++tcnt[a[k]];
if (tcnt[a[k]] <= B) upd(Rt[i][j][tcnt[a[k]]], bel2[a[k]], 1); // 提前存操作
}
}
}
memset(tcnt, 0, sizeof(tcnt));
REP(i, 1, W1) {
REP(k, 1, B) { // 对于相同 S,K 线段树的操作。
REP(j, 1, W2) tcnt[j] = 0;
int _ = 0, __ = 0;
REP(j, i, W1) {
for (auto &P : tr[Rt[i][j][k]]) tcnt[P.first] += P.second, ++_;
if (_ - __ >= B3) { // 对操作分块
bel3[Rt[i][j][k]] = ++W3, __ = _;
REP(it, 1, W2) blcnt[W3][it] = tcnt[it]; // 预处理 tnum。
}
}
}
}
// 查询
PER(i, T - 1, S + 1) {
if (bel3[Rt[S + 1][i][K]]) { // 前缀操作整块贡献
REP(j, 1, W2) tnum[j] += blcnt[bel3[Rt[S + 1][i][K]]][j];
break;
}
for (auto &P : tr[Rt[S + 1][i][K]]) tnum[P.first] += P.second; // 操作散块贡献
}
至此,优化到
AC Code
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define REP(i, l, r) for (int i = l; i <= r; ++i)
#define PER(i, l, r) for (int i = l; i >= r; --i)
using namespace std;
namespace fast_IO{
...
}using namespace fast_IO;
const int N = 50000 + 7;
const int B = 120;
const int B1 = 800;
const int B2 = 800;
const int B3 = 500;
const int W = 400;
namespace rpftexam {
int n, q, K, a[N], bel1[N], bel2[N], bel3[W * W * B], L1[N], R1[N], L2[N], R2[N], W1, W2, W3 = 0, blcnt[N][W], Trc = 0;
int Rt[W][W][B + 3], cnt[W][N], bgs[N][N / B + 7], tcnt[N], isbg[N], tnum[N];
vector<int> P;
vector<pair<int, int> > tr[(N / B1 + 2) * (N / B1 + 2) * B];
inline void upd(int Rt, int now, int c) {tr[Rt].emplace_back(now, c);}
signed main() {
ld >> n;
REP(i, 1, n) ld >> a[i], bel1[i] = (i - 1) / B1 + 1, bel2[i] = (i - 1) / B2 + 1, R1[bel1[i]] = i, R2[bel2[i]] = i;
W1 = bel1[n], W2 = bel2[n];
REP(i, 1, W1) L1[i] = R1[i - 1] + 1;
REP(i, 1, W2) L2[i] = R2[i - 1] + 1;
REP(i, 1, W1) {
REP(j, 1, n) cnt[i][j] = cnt[i - 1][j];
REP(j, L1[i], R1[i]) ++cnt[i][a[j]];
}
REP(i, 1, n) {
if (cnt[W1][i] <= B) continue;
P.emplace_back(i), isbg[i] = 1;
REP(j, 1, n) bgs[j][P.size()] = bgs[j - 1][P.size()] + (a[j] == i);
}
REP(i, 1, W1) {
REP(j, 1, n) tcnt[j] = 0;
REP(j, i, W1) {
REP(k, 1, B) Rt[i][j][k] = ++Trc;
REP(k, L1[j], R1[j]) {
if (tcnt[a[k]] <= B) upd(Rt[i][j][tcnt[a[k]]], bel2[a[k]], -1);
++tcnt[a[k]];
if (tcnt[a[k]] <= B) upd(Rt[i][j][tcnt[a[k]]], bel2[a[k]], 1);
}
}
}
memset(tcnt, 0, sizeof(tcnt));
REP(i, 1, W1) {
REP(k, 1, B) {
REP(j, 1, W2) tcnt[j] = 0;
int _ = 0, __ = 0;
REP(j, i, W1) {
for (auto &P : tr[Rt[i][j][k]]) tcnt[P.first] += P.second, ++_;
if (_ - __ >= B3) {
bel3[Rt[i][j][k]] = ++W3, __ = _;
REP(it, 1, W2) blcnt[W3][it] = tcnt[it];
}
}
}
}
memset(tcnt, 0, sizeof(tcnt));
int l, r, LA = 0; ld >> q;
REP(G, 1, q) {
ld >> l >> r >> K, l ^= LA, r ^= LA, K ^= LA;
if (K <= B) {
bool flag = 0;
int S = bel1[l], T = bel1[r];
if (S == T) {
int Ans = 0;
REP(i, l, r) ++tcnt[a[i]];
REP(i, l, r) {
if (tcnt[a[i]] == K) Ans = max(Ans, a[i]);
tcnt[a[i]] = 0;
}
jyt << (LA = Ans) << '\n';
continue;
}
PER(i, T - 1, S + 1) {
if (bel3[Rt[S + 1][i][K]]) {
REP(j, 1, W2) tnum[j] += blcnt[bel3[Rt[S + 1][i][K]]][j];
break;
}
for (auto &P : tr[Rt[S + 1][i][K]]) tnum[P.first] += P.second;
}
REP(i, l, R1[S]) {
if (cnt[T - 1][a[i]] - cnt[S][a[i]] + tcnt[a[i]] == K) --tnum[bel2[a[i]]];
++tcnt[a[i]];
if (cnt[T - 1][a[i]] - cnt[S][a[i]] + tcnt[a[i]] == K) ++tnum[bel2[a[i]]];
}
REP(i, L1[T], r) {
if (cnt[T - 1][a[i]] - cnt[S][a[i]] + tcnt[a[i]] == K) --tnum[bel2[a[i]]];
++tcnt[a[i]];
if (cnt[T - 1][a[i]] - cnt[S][a[i]] + tcnt[a[i]] == K) ++tnum[bel2[a[i]]];
}
PER(i, W2, 1) {
if (tnum[i]) {
PER(j, R2[i], L2[i])
if (cnt[T - 1][j] - cnt[S][j] + tcnt[j] == K) {
jyt << (LA = j) << '\n'; flag = 1;
break;
}
break;
}
}
REP(i, l, R1[S]) tcnt[a[i]] = 0;
REP(i, L1[T], r) tcnt[a [i]] = 0;
REP(i, 1, W2) tnum[i] = 0;
if (!flag) jyt << "0\n", LA = 0;
} else {
bool flag = 0;
PER(i, P.size(), 1)
if (bgs[r][i] - bgs[l - 1][i] == K) {
jyt << (LA = P[i - 1]) << '\n', flag = 1;
break;
}
if (!flag) jyt << "0\n", LA = 0;
}
}
return 0;
}
}
signed main() {
rpftexam::main();
return 0;
}