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 namespace std;
const int maxn = 200010;
int n, a[maxn], sum[maxn], c[maxn * 2];
long long ans;
map<int, vector<int>> mp;
void add(int p) {
for (p += maxn; p < maxn * 2; p += p & -p) c[p]++;
}
int query(int p) {
int s = 0;
for (p += maxn; p; p -= p & -p) s += c[p];
return s;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]), mp[a[i]].push_back(i);
}
auto calc = [&](int x, int y, int lim) {
int t = min(x, max(0, y - lim));
return (2LL * lim + t + 1) * t / 2 + 1LL * (x - t) * y;
};
for (auto &p : mp) {
auto &pos = p.second;
if (pos.size() <= 500) {
for (int i = 0; i < pos.size(); i++) {
for (int j = i; j < pos.size(); j++) {
int l = (i ? pos[i - 1] : 0) + 1;
int r = (j + 1 < pos.size() ? pos[j + 1] : n + 1) - 1;
int lim = 2 * (j - i);
ans += calc(pos[i], r, lim) + calc(l - 1, pos[j] - 1, lim);
ans -= calc(l - 1, r, lim) + calc(pos[i], pos[j] - 1, lim);
}
}
} else {
memset(sum, -1, sizeof(sum));
for (int x : pos) {
sum[x] = 1;
}
for (int i = 1; i <= n; i++) {
sum[i] += sum[i - 1];
}
memset(c, 0, sizeof(c));
for (int i = 1; i <= n; i++) {
add(sum[i - 1]), ans += query(sum[i] - 1);
}
}
}
printf("%lld\n", ans);
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:31:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for (int i = 0; i < pos.size(); i++) {
| ~~^~~~~~~~~~~~
Main.cpp:32:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for (int j = i; j < pos.size(); j++) {
| ~~^~~~~~~~~~~~
Main.cpp:34:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | int r = (j + 1 < pos.size() ? pos[j + 1] : n + 1) - 1;
| ~~~~~~^~~~~~~~~~~~
Main.cpp:20:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
Main.cpp:22:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
22 | scanf("%d", &a[i]), mp[a[i]].push_back(i);
| ~~~~~^~~~~~~~~~~~~
# | 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... |