#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll trig (ll x) { return x * (x + 1) / 2; }
ll get2 (ll a, ll b, ll c) {
a = min(a, c); b = min(b, c);
c = min(c, a + b);
return (c - b) * (b + 1) + (c + 1) * (a - c + b + 1) - trig(a) + trig(c - b - 1);
}
const int MAXN = 2e5 + 25;
int arr[MAXN], n;
vector <int> occ[MAXN];
vector <int> d;
struct bit {
int tree[2 * MAXN + 25] = {};
void add (int pos, int val) {
for (; pos < 2 * MAXN + 25; pos += pos & (-pos)) tree[pos] += val;
}
ll get (int pos) {
ll sum = 0; for (; pos; pos -= pos & (-pos)) sum += tree[pos];
return sum;
}
};
bit cur;
int a[MAXN];
ll solve1 (int x) {
memset(cur.tree, 0, sizeof(cur.tree));
for (int i = 1; i <= n; i++) {
a[i] = -1;
}
for (auto i : occ[x]) a[i] = 1;
cur.add(MAXN, 1);
ll ret = 0;
for (int i = 1; i <= n; i++) {
a[i] += a[i - 1];
ret += cur.get(a[i] - 1 + MAXN);
cur.add(a[i] + MAXN, 1);
}
return ret;
}
ll solve2 (int x) {
ll ret = (int)occ[x].size() - 2;
for (int i = 1; i < (int)occ[x].size() - 2; i--) {
for (int j = i + 1; j < (int)occ[x].size() - 1; j++) {
ret += get2(occ[x][i] - occ[x][i - 1], occ[x][j + 1] - occ[x][j], 2 * (j - i + 1) - 2 - occ[x][j] + occ[x][i]);
}
}
return ret;
}
int main () {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
d.push_back(arr[i]);
}
sort(d.begin(), d.end()); d.resize(unique(d.begin(), d.end()) - d.begin());
for (int i = 1; i <= n; i++) arr[i] = lower_bound(d.begin(), d.end(), arr[i]) - d.begin();
for (int i = 1; i <= n; i++) {
occ[arr[i]].push_back(i);
}
ll sum = 0;
int x = sqrt(n); while (x * x < n) x++; while (x * x > n) x--;
for (int i = 0; i < n; i++) {
if (occ[i].empty()) continue;
if ((int)occ[i].size() > 10 * x) sum += solve1(i);
else {
occ[i].push_back(n + 1);
occ[i].insert(occ[i].begin(), 0);
sum += solve2(i);
}
}
cout << sum << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
5212 KB |
Output is correct |
2 |
Correct |
1 ms |
5212 KB |
Output is correct |
3 |
Execution timed out |
3076 ms |
5212 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
5212 KB |
Output is correct |
2 |
Correct |
1 ms |
5212 KB |
Output is correct |
3 |
Execution timed out |
3076 ms |
5212 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
9684 KB |
Output is correct |
2 |
Correct |
21 ms |
10200 KB |
Output is correct |
3 |
Correct |
13 ms |
9180 KB |
Output is correct |
4 |
Correct |
23 ms |
10200 KB |
Output is correct |
5 |
Correct |
23 ms |
10452 KB |
Output is correct |
6 |
Correct |
25 ms |
10200 KB |
Output is correct |
7 |
Correct |
24 ms |
10200 KB |
Output is correct |
8 |
Correct |
29 ms |
10456 KB |
Output is correct |
9 |
Correct |
25 ms |
10200 KB |
Output is correct |
10 |
Correct |
26 ms |
10200 KB |
Output is correct |
11 |
Correct |
17 ms |
9820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
5212 KB |
Output is correct |
2 |
Correct |
1 ms |
5212 KB |
Output is correct |
3 |
Execution timed out |
3076 ms |
5212 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |