【BZOJ4241】【回滚莫队模板题】历史研究

博客围绕给定序列,解决每次询问区间内所有权值与其出现次数乘积最大值的问题。采用回滚莫队方法,将询问按左端点所在块和右端点排序,可把原本O(n√n logn)的莫队复杂度优化成O(n√n)。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

给定一个序列,每次询问区间[l,r][l,r]内,所有权值与其出现次数的乘积的最大值。


Solution

回滚莫队模板题。
将询问以左端点所在块为第一关键字,右端点为第二关键字排序。
直接莫队、用std::set维护是O(nnlogn)O(nnlog⁡n)的。
对于所有左端点所在块相同的询问,右端点都是递增的,我们从该块的右端点依次向每个询问的右端点扩展,处理每次询问时,将左端向左移动得到答案后还原即可。
通过回滚莫队,我们可以将很多O(nnlogn)O(nnlog⁡n)的莫队优化成O(nn)O(nn)


Code

/************************************************
 * Au: Hany01
 * Date: Aug 25th, 2018
 * Prob: BZOJ4241 历史研究
 * Email: hany01dxx@gmail.com & hany01@foxmail.com
 * Inst: Yali High School
************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read() {
    static int _, __; static char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const int maxn = 1e5 + 5;

int n, q, a[maxn], ls[maxn], bk, num, bel[maxn], cur, tot;
LL  ans[maxn];

struct Query { int id, l, r; } Q[maxn];
inline bool operator < (Query A, Query B) { return bel[A.l] == bel[B.l] ? A.r < B.r : A.l < B.l; }

inline void bfcalc(int t) {
    static int cnt[maxn];
    For(i, Q[t].l, Q[t].r)
        ++ cnt[a[i]], chkmax(ans[Q[t].id], (LL)cnt[a[i]] * ls[a[i]]);
    For(i, Q[t].l, Q[t].r) -- cnt[a[i]];
}

inline void Solve(int lb) {
    register int now = min(n, lb * bk), hxt = now + 1, hxt_ = hxt, cnt[maxn];
    register LL  Max_, Max;
    Set(cnt, 0), Max = 0;
    for (; cur <= n && bel[Q[cur].l] == lb; ++ cur) {
        if (bel[Q[cur].l] == bel[Q[cur].r]) { bfcalc(cur); continue; }
        while (now < Q[cur].r) ++ cnt[a[++ now]], chkmax(Max, (LL)cnt[a[now]] * ls[a[now]]);
        for (Max_ = Max; hxt > Q[cur].l; ++ cnt[a[-- hxt]], chkmax(Max_, (LL)cnt[a[hxt]] * ls[a[hxt]]));
        ans[Q[cur].id] = Max_;
        while (hxt < hxt_) -- cnt[a[hxt ++]];
    }
}

int main()
{
#ifdef hany01
    freopen("bzoj4241.in", "r", stdin);
    freopen("bzoj4241.out", "w", stdout);
#endif

    n = read(), q = read();
    For(i, 1, n) a[i] = ls[i] = read();
    sort(ls + 1, ls + 1 + n), tot = unique(ls + 1, ls + 1 + n) - ls - 1;
    For(i, 1, n) a[i] = lower_bound(ls + 1, ls + 1 + tot, a[i]) - ls;
    For(i, 1, q) Q[i].id = i, Q[i].l = read(), Q[i].r = read();
    bk = sqrt(n);
    For(i, 1, n) bel[i] = (i - 1) / bk + 1;
    num = bel[n], sort(Q + 1, Q + 1 + q), cur = 1;

    For(i, 1, num) Solve(i);
    For(i, 1, q) printf("%lld\n", ans[i]);

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值