#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;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
300 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
304 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Incorrect |
15 ms |
1716 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Incorrect |
15 ms |
1716 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Incorrect |
15 ms |
1716 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
300 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
304 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Incorrect |
15 ms |
1716 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
300 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
304 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Incorrect |
15 ms |
1716 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |