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 历史研究
* 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;
}
标签:回滚,read,BZOJ4241,int,ls,端点,hxt,莫队,define From: https://blog.51cto.com/u_16117582/6292700