#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define pi pair<int,int>
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
template<class T> using upq = priority_queue<T, vector<T>, greater<T>>;
template<class T> int lwrbound(const vector<T>& a, const T& b, const int s = 0){return int(lower_bound(s + all(a), b) - a.begin());}
template<class T> int uprbound(const vector<T>& a, const T& b, const int s = 0){return int(upper_bound(s + all(a), b) - a.begin());}
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define ROF(i, a, b) for(int i = (a); i >= (b); i--)
#define sumof(x) accumulate(all(x), 0ll)
#define dbg(x) "[" << #x " = " << (x) << "]"
#define el "\n"
using ll = long long;
using ld = long double;
template<class T> bool ckmx(T& a, const T b){return (a < b ? a = b, true : false);}
template<class T> bool ckmn(T& a, const T b){return (a > b ? a = b, true : false);}
const int N = 2e5 + 5, B = 200;
void Main()
{
int n,q; cin >> n >> q;
vi a(n + 1); FOR(i, 1, n) cin >> a[i];
vi comp; FOR(i, 1, n) comp.eb(a[i]);
sort(all(comp)), compact(comp);
FOR(i, 1, n) a[i] = lwrbound(comp, a[i]);
vi cnt(sz(comp));
int nblock = (n + B - 1) / B;
vector<vi> sblock(nblock + 1, vi());
vector<ll> ans(q + 1, 0); vi L(q + 1), R(q + 1);
FOR(i, 1, q){
cin >> L[i] >> R[i];
if(L[i] - R[i] <= B){
FOR(j, L[i], R[i]){
++cnt[a[j]];
ckmx(ans[i], 1ll * cnt[a[j]] * comp[a[j]]);
}
FOR(j, L[i], R[i]) --cnt[a[j]];
}
else{
sblock[(L[i] + B - 1) / B].eb(i);
}
}
FOR(i, 1, nblock){
if(!sz(sblock[i])) continue;
sort(all(sblock[i]), [&](int x, int y){return R[x] < R[y];});
int rblock = min(n, i * B), ptr = rblock + 1;
ll cans = 0;
for(int id : sblock[i]){
while(ptr <= R[id]){
++cnt[a[ptr]];
ckmx(cans, 1ll * cnt[a[ptr]] * comp[a[ptr]]);
++ptr;
}
ans[id] = cans;
ROF(j, rblock, L[id]){
++cnt[a[j]];
ckmx(ans[id], 1ll * cnt[a[j]] * comp[a[j]]);
}
ROF(j, rblock, L[id]) --cnt[a[j]];
}
--ptr;
while(ptr > rblock) --cnt[a[ptr]], ptr--;
}
FOR(i, 1, q) cout << ans[i] << el;
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define name "InvMOD"
if(fopen(name".INP", "r")){
freopen(name".INP", "r", stdin);
freopen(name".OUT", "w", stdout);
}
int t = 1; while(t--) Main();
return 0;
}
Compilation message (stderr)
historic.cpp: In function 'int32_t main()':
historic.cpp:105:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
105 | freopen(name".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
historic.cpp:106:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
106 | freopen(name".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |