#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll,ll>;
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
ll n, q; cin >> n >> q;
vector<ll> arr(n);
for (ll i = 0; i < n; ++i) {
cin >> arr[i];
}
for (ll i = 0; i < q; ++i) {
ll l, r; cin >> l >> r;
l--;
ll bestAns = 1e18;
for (int m = 0; m < 20; ++m) {
vector<ll> v;
vector<ll> app(300002);
v.reserve(r - l);
ll s = r - l;
for (ll j = l; j < r; ++j) {
v.push_back(arr[j]);
}
for (auto x : v) {
app[x]++;
}
vector<pii> v2;
for (auto x : v) {
v2.emplace_back(app[x],x);
}
std::sort(v2.begin(), v2.end());
vector<pii> orderA, orderB;
ll last = -1;
ll leftS = 0, rightS = 0;
bool dir = true;
for (auto [a, x] : v2) {
if (last != x) {
dir = !dir;
if (rand() % 2) {
orderA.emplace_back(x, a);
leftS += a;
}
else {
orderB.emplace_back(x,a);
rightS += a;
}
}
last = x;
}
vector<pii> order;
for (auto x : orderA) {
order.push_back(x);
}
std::reverse(orderB.begin(), orderB.end());
for (auto x : orderB) {
order.push_back(x);
}
ll a = 0;
ll ans = 0;
for (auto [x, amount] : order) {
ll b = a + amount;
ll includeSub = a * (s - b);
ll includePar = a * amount + (s - b) * amount;
ll self = amount * (amount + 1) / 2;
ans += self + includePar + includeSub;
a += amount;
}
bestAns = min(ans, bestAns);
}
cout << bestAns << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2652 KB |
Output is correct |
2 |
Correct |
3 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2648 KB |
Output is correct |
5 |
Correct |
3 ms |
2652 KB |
Output is correct |
6 |
Correct |
3 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2868 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
3 ms |
2652 KB |
Output is correct |
10 |
Correct |
2 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2652 KB |
Output is correct |
2 |
Correct |
3 ms |
2652 KB |
Output is correct |
3 |
Incorrect |
9 ms |
3344 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2652 KB |
Output is correct |
2 |
Correct |
3 ms |
2652 KB |
Output is correct |
3 |
Incorrect |
9 ms |
3344 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2652 KB |
Output is correct |
2 |
Correct |
3 ms |
2652 KB |
Output is correct |
3 |
Incorrect |
9 ms |
3344 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2652 KB |
Output is correct |
2 |
Correct |
3 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2648 KB |
Output is correct |
5 |
Correct |
3 ms |
2652 KB |
Output is correct |
6 |
Correct |
3 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2868 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
3 ms |
2652 KB |
Output is correct |
10 |
Correct |
2 ms |
2652 KB |
Output is correct |
11 |
Correct |
3 ms |
2652 KB |
Output is correct |
12 |
Correct |
3 ms |
2652 KB |
Output is correct |
13 |
Incorrect |
9 ms |
3344 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2652 KB |
Output is correct |
2 |
Correct |
3 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2648 KB |
Output is correct |
5 |
Correct |
3 ms |
2652 KB |
Output is correct |
6 |
Correct |
3 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2868 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
3 ms |
2652 KB |
Output is correct |
10 |
Correct |
2 ms |
2652 KB |
Output is correct |
11 |
Correct |
3 ms |
2652 KB |
Output is correct |
12 |
Correct |
3 ms |
2652 KB |
Output is correct |
13 |
Incorrect |
9 ms |
3344 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |