# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
569710 | hoanghq2004 | Diversity (CEOI21_diversity) | C++14 | 7032 ms | 17688 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>
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 3e5 + 10;
int n, q;
int a[N];
int main() {
ios :: sync_with_stdio(0); cin.tie(0);
cin >> n >> q;
for (int i = 1; i <= n; ++i) cin >> a[i];
while (q--) {
int L, R;
cin >> L >> R;
map <int, int> cnt;
for (int i = L; i <= R; ++i) ++cnt[a[i]];
vector <int> s;
for (auto [_, __]: cnt) s.push_back(__);
sort(s.begin(), s.end());
vector <int> a;
for (int i = 0; i < s.size(); ++i) if (i % 2 == 0) a.push_back(s[i]);
for (int i = s.size() - 1; i >= 0; --i) if (i % 2 == 1) a.push_back(s[i]);
int tot = (R - L + 1);
long long ans = 0;
int cur = 0;
for (int i = 0; i < a.size(); ++i) {
ans += 1LL * a[i] * (a[i] + 1) / 2;
ans += 1LL * (cur + a[i]) * (tot - cur);
ans -= 1LL * a[i] * a[i];
cur += a[i];
}
cout << ans << '\n';
}
}
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... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |