#include<bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(false);cin.tie(NULL)
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define pb push_back
#define eb emplace_back
#define pre(a) cout<<fixed; cout.precision(a)
#define mp make_pair
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll INF = 1e18;
const int inf = 1e9;
const int sz = 400;
ll n, q;
ll x[100010];
ll cnt[100010];
ll ans[100010];
vector<ll> v;
vector<pair<pll, ll>> qr;
multiset<ll> S;
void add(int k) {
S.erase(cnt[k] * v[k]);
cnt[k]++;
S.insert(cnt[k] * v[k]);
}
void erase(int k) {
S.erase(cnt[k] * v[k]);
cnt[k]--;
S.insert(cnt[k] * v[k]);
}
int main() {
fast;
cin >> n >> q;
for(int i=0; i<n; i++) {
cin >> x[i];
v.eb(x[i]);
S.insert(0);
}
sort(all(v));
v.erase(unique(all(v)), v.end());
for(int i=0; i<n; i++) {
x[i] = lower_bound(all(v), x[i]) - v.begin();
}
for(int i=1; i<=q; i++) {
ll a, b;
cin >> a >> b;
a--, b--;
if(a > b) swap(a, b);
qr.eb(mp(a, b), i);
}
sort(all(qr), [](pair<pll, ll> i, pair<pll, ll> j) {
if(i.fi.fi / sz == j.fi.fi / sz) return i.fi.se < j.fi.se;
return i.fi.fi / sz < j.fi.fi / sz;
});
int s = qr[0].fi.fi;
int e = qr[0].fi.se;
for(int i=s; i<=e; i++) {
add(x[i]);
}
ans[qr[0].se] = *prev(S.end());
for(int i=1; i<q; i++) {
while(s < qr[i].fi.fi) erase(x[s++]);
while(s > qr[i].fi.fi) add(x[--s]);
while(e < qr[i].fi.se) add(x[++e]);
while(e > qr[i].fi.se) erase(x[e--]);
ans[qr[i].se] = *prev(S.end());
}
for(int i=1; i<=q; i++) {
cout << ans[i] << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
7 ms |
384 KB |
Output is correct |
18 |
Correct |
19 ms |
512 KB |
Output is correct |
19 |
Correct |
41 ms |
632 KB |
Output is correct |
20 |
Correct |
98 ms |
768 KB |
Output is correct |
21 |
Correct |
135 ms |
1024 KB |
Output is correct |
22 |
Incorrect |
166 ms |
1080 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
3 ms |
512 KB |
Output is correct |
7 |
Correct |
6 ms |
768 KB |
Output is correct |
8 |
Correct |
12 ms |
1152 KB |
Output is correct |
9 |
Correct |
26 ms |
2044 KB |
Output is correct |
10 |
Correct |
27 ms |
3576 KB |
Output is correct |
11 |
Correct |
107 ms |
10596 KB |
Output is correct |
12 |
Correct |
94 ms |
6760 KB |
Output is correct |
13 |
Correct |
111 ms |
8328 KB |
Output is correct |
14 |
Correct |
134 ms |
9952 KB |
Output is correct |
15 |
Correct |
143 ms |
11952 KB |
Output is correct |
16 |
Correct |
156 ms |
10332 KB |
Output is correct |
17 |
Correct |
86 ms |
8556 KB |
Output is correct |
18 |
Correct |
117 ms |
10600 KB |
Output is correct |
19 |
Correct |
111 ms |
11492 KB |
Output is correct |
20 |
Correct |
81 ms |
8936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
7 ms |
384 KB |
Output is correct |
18 |
Correct |
19 ms |
512 KB |
Output is correct |
19 |
Correct |
41 ms |
632 KB |
Output is correct |
20 |
Correct |
98 ms |
768 KB |
Output is correct |
21 |
Correct |
135 ms |
1024 KB |
Output is correct |
22 |
Incorrect |
166 ms |
1080 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |