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 i64 = long long;
constexpr i64 inf64 = 1ll << 60;
struct Query {
int l;
int r;
};
int calc(std::vector<int> X) {
const int n = (int)X.size();
i64 ans = 0;
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 n) {
return n * (n + 1) / 2;
};
for (int i = 0; i < n; ++i) {
ans += p2(s) - p2(lSum[i]) - p2(rSum[i + 1]);
}
return ans;
}
int naive(std::vector<int> A) {
const int N = (int)A.size();
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);
A.clear();
for (int i = 0; i < (int)B.size(); ++i) {
for (int j = 0; j < (int)B[i]; ++j) {
A.push_back(i);
}
}
i64 ans = 0;
for (int i = 0; i < N; ++i) {
for (int j = 1; j <= N; ++j) {
std::set<int> s;
for (int k = i; k < j; ++k) {
s.insert(A[k]);
}
ans += (int)s.size();
}
}
return ans;
}
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 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... |