# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
151887 | EntityIT | 역사적 조사 (JOI14_historical) | C++14 | 422 ms | 27168 KiB |
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;
#define int ll
using ll = long long;
const int N = (int)1e5 + 5, BS = sqrt(N), NB = N / BS + 5;
int n, q, x[N], cnt[N], bIdOf[N], bBe[NB], bEn[NB], nB, prefCnt[NB][N];
ll mx[NB][NB];
vector<int> comp;
int32_t main() {
// freopen("test.INP", "r", stdin);
scanf("%lld %lld", &n, &q);
comp.assign(n + 1, 0);
for (int i = 1; i <= n; ++i) scanf("%lld", x + i), comp[i] = x[i];
sort(comp.begin(), comp.end() ); comp.erase(unique(comp.begin(), comp.end() ), comp.end() );
for (int i = 1; i <= n; ++i) x[i] = lower_bound(comp.begin(), comp.end(), x[i]) - comp.begin();
for (int i = 1; i <= n; i += BS) {
bBe[++nB] = i;
bEn[nB] = min(n, bBe[nB] + BS - 1);
for (int j = bBe[nB]; j <= bEn[nB]; ++j) bIdOf[j] = nB;
}
for (int i = 1; i <= nB; ++i) {
ll ans = 0;
memset(cnt, 0, sizeof cnt);
for (int j = i; j <= nB; ++j) {
for (int k = bBe[j]; k <= bEn[j]; ++k) {
++cnt[ x[k] ];
ans = max(ans, (ll)comp[ x[k] ] * cnt[ x[k] ]);
}
mx[i][j] = ans;
}
for (int j = 1; j < (int)comp.size(); ++j) prefCnt[i][j] = prefCnt[i - 1][j];
for (int j = bBe[i]; j <= bEn[i]; ++j) ++prefCnt[i][ x[j] ];
}
memset(cnt, 0, sizeof cnt);
while (q --) {
int l, r; scanf("%lld %lld", &l, &r);
ll ans = 0;
if (bIdOf[l] == bIdOf[r]) {
for (int i = l; i <= r; ++i) {
ans = max(ans, (ll)comp[ x[i] ] * (++cnt[ x[i] ]) );
}
for (int i = l; i <= r; ++i) --cnt[ x[i] ];
}
else {
ans = mx[ bIdOf[l] + 1 ][ bIdOf[r] - 1 ];
for (int i = l; i <= bEn[ bIdOf[l] ]; ++i) ans = max(ans, (ll)comp[ x[i] ] * ( (++cnt[ x[i] ]) + prefCnt[ bIdOf[r] - 1][ x[i] ] - prefCnt[ bIdOf[l] ][ x[i] ]) );
for (int i = r; i >= bBe[ bIdOf[r] ]; --i) ans = max(ans, (ll)comp[ x[i] ] * ( (++cnt[ x[i] ]) + prefCnt[ bIdOf[r] - 1][ x[i] ] - prefCnt[ bIdOf[l] ][ x[i] ]) );
for (int i = l; i <= bEn[ bIdOf[l] ]; ++i) --cnt[ x[i] ];
for (int i = r; i >= bBe[ bIdOf[r] ]; --i) --cnt[ x[i] ];
}
printf("%I64d\n", ans);
}
return 0;
}
Compilation message (stderr)
# | 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... |