Submission #698623

#TimeUsernameProblemLanguageResultExecution timeMemory
698623CyanmondDiversity (CEOI21_diversity)C++17
64 / 100
7063 ms23356 KiB
#include <bits/stdc++.h> using i64 = long long; constexpr i64 inf64 = 1ll << 60; struct Query { int l; int r; }; i64 calc(std::vector<int> X) { const int n = (int)X.size(); std::vector<i64> lSum(n + 1), rSum(n + 1); for (int i = 0; i < n; ++i) { lSum[i + 1] = lSum[i] + X[i]; } for (int i = n - 1; i >= 0; --i) { rSum[i] = rSum[i + 1] + X[i]; } const auto s = lSum[n]; auto p2 = [](i64 x) { return x * (x + 1) / 2; }; i64 ans = 0; for (int i = 0; i < n; ++i) { ans += p2(s) - p2(lSum[i]) - p2(rSum[i + 1]); } return ans; } i64 naive(std::vector<int> A) { std::map<int, int> countS; for (const auto e : A) { ++countS[e]; } A.clear(); for (auto &[key, val] : countS) { A.push_back(val); } std::sort(A.begin(), A.end()); std::vector<int> B(A.size()); for (int i = 0; i < (int)A.size(); ++i) { if (i % 2 == 0) { B[i / 2] = A[i]; } else { B[(int)A.size() - (i / 2) - 1] = A[i]; } } return calc(B); } int main() { int N, Q; std::cin >> N >> Q; std::vector<int> A(N); for (auto &e : A) { std::cin >> e; } std::vector<Query> queries(Q); for (auto &[l, r] : queries) { std::cin >> l >> r; --l; } for (int i = 0; i < Q; ++i) { std::vector<int> vec; std::copy(A.begin() + queries[i].l, A.begin() + queries[i].r, std::back_inserter(vec)); std::cout << naive(vec) << std::endl; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...