# AT1219 歴史の研究

#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#define R(a,b,c) for(register int a = (b); a <= (c); ++a)
#define nR(a,b,c) for(register int a = (b); a >= (c); --a)
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))
#define MP make_pair
#ifdef QWQ
#define D_e_Line printf("\n------\n")
#define D_e(x) cerr << (#x) << " " << x << endl
#define C_e(x) cout << (#x) << " " << x << endl
#define FileOpen() freopen("in.txt", "r", stdin)
#define FileSave() freopen("out.txt", "w", stdout)
#define Pause() system("pause")
#include <cassert>
#define PASS fprintf(stderr, "Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#else
#define D_e_Line
#define D_e(x)
#define C_e(x)
#define FileOpen()
#define FileSave()
#define Pause()
#define PASS
#endif
using namespace std;
struct FastIO {
template<typename ATP> inline FastIO& operator >> (ATP &x) {
x = 0; int sign = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') sign = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
if(sign == -1) x = -x;
return *this;
}
} io;
template<typename ATP> inline ATP Max(ATP x, ATP y) {
return x > y ? x : y;
}
template<typename ATP> inline ATP Min(ATP x, ATP y) {
return x < y ? x : y;
}
template<typename ATP> inline ATP Abs(ATP x) {
return x < 0 ? -x : x;
}
const int N = 1e5 + 7;
#define int long long
struct Ques {
int l, r, id;
} q[N];
int blo[N], bloSize, ans[N], a[N], b[N], tot[N], tmp[N], ANS, n;
inline void Add(int i) {
++tot[a[i]];
ANS = Max(ANS, tot[a[i]] * b[a[i]]);
}
inline void Del(int i) {
--tot[a[i]];
}
inline int Calc(int l, int r) {
int maxx = 0;
R(i,l,r) tmp[a[i]] = 0;
R(i,l,r) ++tmp[a[i]], maxx = Max(maxx, tmp[a[i]] * b[a[i]]);
return maxx;
}
int now = 1;
inline void Solve(int &id) {
int maxn = Min(n, bloSize * id), r = maxn;
memset(tot, 0, (n + 3) * sizeof(int));
ANS = 0;
for(; blo[q[now].l] == id; ++now){
if(blo[q[now].l] == blo[q[now].r]){
ans[q[now].id] = Calc(q[now].l, q[now].r);
continue;
}
while(r < q[now].r) Add(++r);
int lst = ANS;
R(i,q[now].l,maxn) Add(i);
ans[q[now].id] = ANS;
R(i,q[now].l,maxn) Del(i);
ANS = lst;
}
}
#undef int
int main() {
#define int long long
int m;
io >> n >> m;
R(i,1,n) io >> a[i], b[i] = a[i];
sort(b + 1, b + n + 1);
int tot = unique(b + 1, b + n + 1) - b - 1;
R(i,1,n) a[i] = lower_bound(b + 1, b + tot + 1, a[i]) - b;

bloSize = sqrt(n);
R(i,1,n) blo[i] = (i - 1) / bloSize + 1;

R(i,1,m){
io >> q[i].l >> q[i].r;
q[i].id = i;
}
sort(q + 1, q + m + 1, [&](const Ques &x, const Ques &y){
if(blo[x.l] != blo[y.l]) return blo[x.l] < blo[y.l];
return x.r < y.r;
});

R(i,1,blo[n]) Solve(i);

R(i,1,m) printf("%lld\n", ans[i]);
return 0;
}


posted @ 2019-11-14 17:14  邱涵的秘密基地  阅读(103)  评论(0编辑  收藏  举报