天天看点

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

Description

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

Solution

回滚莫队模板题。

将询问以左端点所在块为第一关键字,右端点为第二关键字排序。

直接莫队、用

std::set

维护是 O(nn‾√logn) O ( n n log ⁡ n ) 的。

对于所有左端点所在块相同的询问,右端点都是递增的,我们从该块的右端点依次向每个询问的右端点扩展,处理每次询问时,将左端向左移动得到答案后还原即可。

通过回滚莫队,我们可以将很多 O(nn‾√logn) O ( n n log ⁡ n ) 的莫队优化成 O(nn‾√) O ( n n ) 。

Code

/************************************************
 * Au: Hany01
 * Date: Aug 25th, 2018
 * Prob: BZOJ4241 历史研究
 * Email: [email protected] & [email protected]
 * 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,  : ; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b,  : ; }

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

const int maxn =  + ;

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 + , hxt_ = hxt, cnt[maxn];
    register LL  Max_, Max;
    Set(cnt, ), Max = ;
    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, , n) a[i] = ls[i] = read();
    sort(ls + , ls +  + n), tot = unique(ls + , ls +  + n) - ls - ;
    For(i, , n) a[i] = lower_bound(ls + , ls +  + tot, a[i]) - ls;
    For(i, , q) Q[i].id = i, Q[i].l = read(), Q[i].r = read();
    bk = sqrt(n);
    For(i, , n) bel[i] = (i - ) / bk + ;
    num = bel[n], sort(Q + , Q +  + q), cur = ;

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

    return ;
}