#include <iostream>
#include <map>
#include <vector>
using namespace std;
map<long long int, int> m;
long long int a[200005], c[200005];
vector<int> g[200005];
struct FENWICK {
vector<long long int> v;
FENWICK (int n) {
v.resize(n + 1);
for (int i = 0; i <= n; i++) {
v[i] = 0;
}
}
long long int build(long long int x) {
long long int res = 0;
x++;
while (x) {
res += v[x];
x -= (x & (-x));
}
return res;
}
void update(long long int x, long long int y) {
x++;
while (x < v.size()) {
v[x] += y;
x += (x & (-x));
}
}
long long int sum(long long int l, long long int r) {
if (l == 0) return build(r);
return build(r) - build(l - 1);
}
};
int main() {
long long int n, d = 0, res = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (!m.count(a[i])) {
m[a[i]] = m.size();
}
a[i] = m[a[i]];
g[a[i]].push_back(i);
}
for (long long int x = 0; x <= n; x++) {
if (g[x].empty()) continue;
if (g[x].size() >= 450) {
c[0] = 0;
for (int i = 1; i <= n; i++) {
c[i] = c[i - 1];
if (a[i - 1] == x) c[i]++;
}
FENWICK f(n + c[n] * 2 + 1);
for (int j = 0; j < n; j++) {
f.update(c[j] * 2 + n - j + 2, 1);
res += f.sum(0, c[j + 1] * 2 + n - j);
}
}
else {
vector<int> v = g[x];
for (int i = 0; i < v.size(); i++) {
for (int j = i; j < v.size(); j++) {
long long int l, r;
if (i == 0) l = v[i];
else l = v[i] - v[i - 1] - 1;
if (j == v.size() - 1) r = n - v[j];
else r = v[j + 1] - v[j];
long long int k = (j - i) * 2 + v[i] - v[j] + 1;
res += max(min(l, k) - max(k - r, 0ll) + 1, 0ll) * k;
long long int ll = max(k - r, 0ll), rr = min(l, k);
if (ll <= rr) {
res -= (rr + 1) * rr / 2 - ll * (ll - 1) / 2;
}
res += r * max(min(k - r - 1, l) + 1, 0ll);
}
}
}
}
cout << res;
}
Compilation message
Main.cpp: In member function 'void FENWICK::update(long long int, long long int)':
Main.cpp:27:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | while (x < v.size()) {
| ~~^~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:64:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for (int i = 0; i < v.size(); i++) {
| ~~^~~~~~~~~~
Main.cpp:65:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for (int j = i; j < v.size(); j++) {
| ~~^~~~~~~~~~
Main.cpp:69:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | if (j == v.size() - 1) r = n - v[j];
| ~~^~~~~~~~~~~~~~~
Main.cpp:38:22: warning: unused variable 'd' [-Wunused-variable]
38 | long long int n, d = 0, res = 0;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5008 KB |
Output is correct |
4 |
Correct |
4 ms |
4948 KB |
Output is correct |
5 |
Correct |
4 ms |
4948 KB |
Output is correct |
6 |
Correct |
5 ms |
5004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5008 KB |
Output is correct |
4 |
Correct |
4 ms |
4948 KB |
Output is correct |
5 |
Correct |
4 ms |
4948 KB |
Output is correct |
6 |
Correct |
5 ms |
5004 KB |
Output is correct |
7 |
Correct |
5 ms |
5020 KB |
Output is correct |
8 |
Correct |
5 ms |
4948 KB |
Output is correct |
9 |
Correct |
5 ms |
4948 KB |
Output is correct |
10 |
Correct |
6 ms |
5076 KB |
Output is correct |
11 |
Correct |
6 ms |
5076 KB |
Output is correct |
12 |
Correct |
6 ms |
5024 KB |
Output is correct |
13 |
Correct |
5 ms |
5076 KB |
Output is correct |
14 |
Correct |
5 ms |
4948 KB |
Output is correct |
15 |
Correct |
6 ms |
5012 KB |
Output is correct |
16 |
Correct |
6 ms |
5076 KB |
Output is correct |
17 |
Correct |
6 ms |
5016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
9940 KB |
Output is correct |
2 |
Correct |
43 ms |
11444 KB |
Output is correct |
3 |
Correct |
29 ms |
8852 KB |
Output is correct |
4 |
Correct |
44 ms |
11648 KB |
Output is correct |
5 |
Correct |
52 ms |
11788 KB |
Output is correct |
6 |
Correct |
48 ms |
12344 KB |
Output is correct |
7 |
Correct |
47 ms |
12324 KB |
Output is correct |
8 |
Correct |
47 ms |
12348 KB |
Output is correct |
9 |
Correct |
46 ms |
12268 KB |
Output is correct |
10 |
Correct |
48 ms |
12276 KB |
Output is correct |
11 |
Correct |
41 ms |
13740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5008 KB |
Output is correct |
4 |
Correct |
4 ms |
4948 KB |
Output is correct |
5 |
Correct |
4 ms |
4948 KB |
Output is correct |
6 |
Correct |
5 ms |
5004 KB |
Output is correct |
7 |
Correct |
5 ms |
5020 KB |
Output is correct |
8 |
Correct |
5 ms |
4948 KB |
Output is correct |
9 |
Correct |
5 ms |
4948 KB |
Output is correct |
10 |
Correct |
6 ms |
5076 KB |
Output is correct |
11 |
Correct |
6 ms |
5076 KB |
Output is correct |
12 |
Correct |
6 ms |
5024 KB |
Output is correct |
13 |
Correct |
5 ms |
5076 KB |
Output is correct |
14 |
Correct |
5 ms |
4948 KB |
Output is correct |
15 |
Correct |
6 ms |
5012 KB |
Output is correct |
16 |
Correct |
6 ms |
5076 KB |
Output is correct |
17 |
Correct |
6 ms |
5016 KB |
Output is correct |
18 |
Correct |
36 ms |
9940 KB |
Output is correct |
19 |
Correct |
43 ms |
11444 KB |
Output is correct |
20 |
Correct |
29 ms |
8852 KB |
Output is correct |
21 |
Correct |
44 ms |
11648 KB |
Output is correct |
22 |
Correct |
52 ms |
11788 KB |
Output is correct |
23 |
Correct |
48 ms |
12344 KB |
Output is correct |
24 |
Correct |
47 ms |
12324 KB |
Output is correct |
25 |
Correct |
47 ms |
12348 KB |
Output is correct |
26 |
Correct |
46 ms |
12268 KB |
Output is correct |
27 |
Correct |
48 ms |
12276 KB |
Output is correct |
28 |
Correct |
41 ms |
13740 KB |
Output is correct |
29 |
Correct |
86 ms |
13944 KB |
Output is correct |
30 |
Correct |
43 ms |
8620 KB |
Output is correct |
31 |
Correct |
96 ms |
11680 KB |
Output is correct |
32 |
Correct |
188 ms |
17996 KB |
Output is correct |
33 |
Correct |
91 ms |
12028 KB |
Output is correct |
34 |
Correct |
109 ms |
12256 KB |
Output is correct |
35 |
Correct |
76 ms |
10060 KB |
Output is correct |
36 |
Correct |
33 ms |
8224 KB |
Output is correct |
37 |
Correct |
37 ms |
8804 KB |
Output is correct |
38 |
Correct |
65 ms |
11600 KB |
Output is correct |
39 |
Correct |
69 ms |
11336 KB |
Output is correct |
40 |
Correct |
74 ms |
11456 KB |
Output is correct |
41 |
Correct |
67 ms |
11288 KB |
Output is correct |
42 |
Correct |
68 ms |
11536 KB |
Output is correct |
43 |
Correct |
141 ms |
11444 KB |
Output is correct |
44 |
Correct |
153 ms |
11340 KB |
Output is correct |
45 |
Correct |
140 ms |
11292 KB |
Output is correct |
46 |
Correct |
137 ms |
11284 KB |
Output is correct |
47 |
Correct |
152 ms |
11400 KB |
Output is correct |
48 |
Correct |
1253 ms |
10892 KB |
Output is correct |
49 |
Correct |
1223 ms |
10888 KB |
Output is correct |
50 |
Correct |
496 ms |
11416 KB |
Output is correct |
51 |
Correct |
535 ms |
11328 KB |
Output is correct |
52 |
Correct |
2268 ms |
10768 KB |
Output is correct |
53 |
Correct |
2132 ms |
10680 KB |
Output is correct |
54 |
Correct |
1205 ms |
10768 KB |
Output is correct |