# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
164833 | ly20 | 역사적 조사 (JOI14_historical) | C++17 | 20 ms | 1108 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;
const int MAXN = 112345;
const int sz = sqrt(MAXN) + 10;
int v[MAXN];
long long marc[MAXN];
long long mrk[sz][MAXN];
int sq[MAXN];
map <int, int> mp;
set <int> s;
set <int> :: iterator it;
int ini[sz], fim[sz];
long long rs[sz][sz];
long long imp[MAXN];
int main() {
int n, q;
scanf("%d %d", &n, &q);
int par = (n - 1) / sz + 1;
for(int i = 0; i < n; i++) {
scanf("%d", &v[i]);
s.insert(v[i]);
}
int temp = 0;
for(it = s.begin(); it != s.end(); it++) {
mp[*it] = temp;
imp[temp] = *it;
temp++;
}
for(int i = 0; i < n; i++) {
v[i] = mp[v[i]];
sq[i] = i / sz;
}
for(int i = 0; i < par; i++) {
ini[i] = i * sz;
fim[i] = min(n - 1, i * (sz + 1) - 1);
}
long long resp;
for(int i = 0; i < par; i++) {
resp = 0;
for(int j = ini[i]; j < n - 1; j++) {
mrk[i][v[j]]++;
if(mrk[i][v[j]] * imp[v[j]] > resp) {
resp = mrk[i][v[j]] * imp[v[j]];
}
if(j == fim[sq[j]]) {
rs[i][sq[j]] = resp;
}
}
}
for(int i = 0; i < q; i++) {
int a, b;
scanf("%d %d", &a, &b); a--; b--;
if(sq[a] == sq[b]) {
long long resp = 0;
for(int j = a; j <= b; j++) {
marc[v[j]]++;
resp = max(resp, marc[v[j]] * imp[v[j]]);
}
printf("%lld\n", resp);
for(int j = a; j <= b; j++) marc[v[j]]--;
}
else {
long long resp = rs[sq[a] + 1][sq[b] - 1];
for(int j = a; j <= fim[sq[a]]; j++) {
marc[v[j]]++;
if((marc[v[j]] + mrk[sq[a] + 1][v[j]] - mrk[sq[b]][v[j]]) * imp[v[j]] > resp) {
resp = (marc[v[j]] + mrk[sq[a] + 1][v[j]] - mrk[sq[b]][v[j]]) * imp[v[j]];
}
}
for(int j = b; j >= ini[sq[b]];j--) {
marc[v[j]]++;
if((marc[v[j]] + mrk[sq[a] + 1][v[j]] - mrk[sq[b]][v[j]]) * imp[v[j]] > resp) {
resp = (marc[v[j]] + mrk[sq[a] + 1][v[j]] - mrk[sq[b]][v[j]]) * imp[v[j]];
}
}
printf("%lld\n", resp);
for(int j = a; j <= fim[sq[a]]; j++) {
marc[v[j]]--;
}
for(int j = b; j >= ini[sq[b]];j--) {
marc[v[j]]--;
}
}
}
}
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... |