This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#include "monster.h"
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
//#define mp make_pair
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, r, l) for(int i = r; i >= l; i--)
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
const int N = 1e5 + 5;
const int oo = 1e18 + 7, mod = 1e9 + 7;
int n, q;
int a[N];
int blk_sz = 305;
unordered_map<int, int> cnt;
int cur_ans;
int answer[N];
vector<iii> queries[N];
bool cmp(iii a, iii b){
return a.fi.se < b.fi.se;
}
int no_blk[N], st_blk[N];
void process(){
cin >> n >> q;
for(int i = 1; i <= n; i++){
cin >> a[i];
no_blk[i] = no_blk[i - 1];
if((i % blk_sz) == 1){
no_blk[i]++;
st_blk[no_blk[i]] = i;
}
}
for(int i = 1; i <= q; i++){
int l, r;
cin >> l >> r;
if(no_blk[l] == no_blk[r]){
cur_ans = 0;
for(int j = l; j <= r; j++){
cnt[a[j]]++;
cur_ans = max(cur_ans, cnt[a[j]] * a[j]);
}
answer[i] = cur_ans;
cnt.clear();
}
else queries[no_blk[l] + 1].pb({{l, r}, i});
}
for(int i = 1; i <= no_blk[n]; i++) sort(queries[i].begin(), queries[i].end(), cmp);
for(int i = 1; i <= no_blk[n]; i++){
int cur_itr = st_blk[i] - 1;
cur_ans = 0;
cnt.clear();
for(auto it : queries[i]){
while(cur_itr < it.fi.se){
cur_itr++;
cnt[a[cur_itr]]++;
cur_ans = max(cur_ans, cnt[a[cur_itr]] * a[cur_itr]);
}
int rem = cur_ans;
for(int j = st_blk[i] - 1; j >= it.fi.fi; j--){
cnt[a[j]]++;
cur_ans = max(cur_ans, cnt[a[j]] * a[j]);
}
answer[it.se] = cur_ans;
cur_ans = rem;
for(int j = st_blk[i] - 1; j >= it.fi.fi; j--) cnt[a[j]]--;
}
}
for(int i = 1; i <= q; i++) cout << answer[i] << "\n";
}
signed main(){
ios_base::sync_with_stdio(0);
process();
}
# | 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... |