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 <bits/stdc++.h>
using namespace std;
typedef tuple<int,int,int> iii;
typedef long long ll;
const int N = 100005;
const int BK = 300;
int n,q;
int ct[N];
int a[N];
vector<int> v;
unordered_map<int,int> ID;
ll ans[N];
ll cur = 0;
vector<iii> qu[N/BK+1];
int add[N];
bool cmp(iii a, iii b){
return (get<1>(a) < get<1>(b));
}
void ADD(int id){
//printf("adding %d\n",id);
ct[a[id]]++;
cur = max(cur,(ll)ct[a[id]]*v[a[id]]);
}
void remove(int id){
//printf("removing %d\n",id);
ct[a[id]]--;
}
int main(){
scanf("%d%d",&n,&q);
for (int i = 1 ; i <= n; i++) {
scanf("%d",&a[i]);
v.push_back(a[i]);
}
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
for (int i = 0; i < (int) v.size(); i++){
ID[v[i]] = i;
}
for (int i = 1; i <= n; i++){
a[i] = ID[a[i]];
}
for (int i = 0; i < q; i++){
int l,r;
scanf("%d%d",&l,&r);
qu[l/BK].emplace_back(l,r,i);
}
int R;
int L_;
for (int Q = 0; Q <= n/BK; Q++){
cur = 0;
L_ = R = (Q*BK)+BK;
bool first = true;
memset(ct,0,sizeof(ct));
//printf("%d %d\n",Q,(int)qu[Q].size());
sort(qu[Q].begin(),qu[Q].end(),cmp);
for (auto q : qu[Q]){
int l,r,id;
tie(l,r,id) = q;
vector<int> used;
if (r < L_){
//printf("WITHIN %d %d\n",l,r);
for (int i = l; i <= r; i++){
used.push_back(a[i]);
ct[a[i]]++;
ans[id] = max(ans[id],(ll)ct[a[i]]*v[a[i]]);
}
for (auto x : used) ct[x] = 0;
used.clear();
continue;
}
else{
if (first){ ADD(R); first = false; }
while (R < r){
R++;
ADD(R);
}
}
for (int i = L_-1; i >= l; i--){
used.push_back(a[i]);
add[a[i]]++;
}
ans[id] = cur;
for (auto x : used){
if (add[x] == 0) continue;
ans[id] = max(ans[id],(ll)(ct[x]+add[x])*v[x]);
add[x] = 0;
}
//printf("%d %d %lld\n",l,r,ans[id]);
}
}
for (int i = 0; i < q; i++){
printf("%lld\n",ans[i]);
}
}
Compilation message (stderr)
historic.cpp: In function 'int main()':
historic.cpp:29:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&n,&q);
~~~~~^~~~~~~~~~~~~~
historic.cpp:31:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&a[i]);
~~~~~^~~~~~~~~~~~
historic.cpp:44:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&l,&r);
~~~~~^~~~~~~~~~~~~~
# | 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... |