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 <stdio.h>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
map<int, int> chk;
int N,Q,C,X[100010],O[100010],Z; long long R[100010],B[1<<18],A[100010];
struct seg{
seg(int s_, int e_, int i_){s = s_; e = e_; i = i_;}
int s,e,i;
bool operator <(const seg t)const{return s == t.s ? e < t.e : s < t.s;}
};
vector<seg> intv[501];
void upt(int x, int p)
{
x = X[x]; O[x] += p;
B[x+Z] = R[x] * O[x];
x = (x + Z) >> 1;
while (x){
B[x] = max(B[x*2],B[x*2+1]);
x >>= 1;
}
}
int main()
{
scanf ("%d %d",&N,&Q);
for (int i=0;i<N;i++){
scanf ("%d",&X[i]);
chk[X[i]] = 0;
}
for (map<int, int>::iterator I = chk.begin(); I != chk.end(); I++) R[C] = I->first, I->second = C++;
for (int i=0;i<N;i++) X[i] = chk[X[i]];
for (Z=1;Z<C;Z<<=1);
for (int i=0,s,e;i<Q;i++){
scanf ("%d %d",&s,&e); s--;
intv[s/400+e/400].push_back(seg(s,e,i));
}
for (int i=0;i<500;i++) if (!intv[i].empty()){
for (int k=0;k<2*Z;k++) B[k] = 0;
for (int k=0;k<C;k++) O[k] = 0;
int s = 0, e = 0;
sort(intv[i].begin(),intv[i].end());
for (int j=0;j<intv[i].size();j++){
while (e < intv[i][j].e) upt(e++,1);
while (e > intv[i][j].e) upt(--e,-1);
while (s < intv[i][j].s) upt(s++,-1);
A[intv[i][j].i] = B[1];
}
}
for (int i=0;i<Q;i++) printf ("%lld\n",A[i]);
return 0;
}
# | 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... |