#include <bits/stdc++.h>
class FenwickTree {
public:
std::vector<int> f;
int n;
FenwickTree(int n) {
this->n = n;
f.resize(n + 1);
}
void add(int idx, int del) {
++idx;
for (int i = idx; i <= n; i += i & (-i)) {
f[i] += del;
}
}
int query(int idx) {
++idx;
int ans = 0;
for (int i = idx; i >= 1; i -= i & (-i)) {
ans += f[i];
}
return ans;
}
int query(int l, int r) {
if (l > r) {
return 0;
}
int ans = query(r);
if (l - 1 >= 0) {
ans -= query(l - 1);
}
return ans;
}
};
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
std::vector<std::pair<long long, int>> h(n);
FenwickTree tree(n);
for (int i = 0; i < n; ++i) {
std::cin >> h[i].first;
h[i].second = i;
tree.add(i, 1);
}
std::sort(h.begin(), h.end());
std::stack<int> stk;
long long ans = 0;
for (int i = 0; i < n; ++i) {
if (i != 0 and h[i].first != h[i - 1].first) {
while (!stk.empty()) {
tree.add(stk.top(), -1);
stk.pop();
}
}
long long left = tree.query(0, h[i].second);
long long right = tree.query(h[i].second, n - 1);
// above is >=, so make it <
ans += (h[i].second - left + 1) * (n - h[i].second - right);
stk.push(h[i].second);
}
std::cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
57 ms |
11860 KB |
Output is correct |
3 |
Correct |
55 ms |
11688 KB |
Output is correct |
4 |
Correct |
52 ms |
11824 KB |
Output is correct |
5 |
Correct |
52 ms |
11860 KB |
Output is correct |
6 |
Correct |
51 ms |
11856 KB |
Output is correct |
7 |
Correct |
51 ms |
11688 KB |
Output is correct |
8 |
Correct |
50 ms |
11860 KB |
Output is correct |
9 |
Correct |
54 ms |
11860 KB |
Output is correct |
10 |
Correct |
56 ms |
11860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
7352 KB |
Output is correct |
2 |
Correct |
48 ms |
7340 KB |
Output is correct |
3 |
Correct |
47 ms |
7504 KB |
Output is correct |
4 |
Correct |
44 ms |
7508 KB |
Output is correct |
5 |
Correct |
45 ms |
7420 KB |
Output is correct |
6 |
Correct |
44 ms |
7504 KB |
Output is correct |
7 |
Correct |
43 ms |
7504 KB |
Output is correct |
8 |
Correct |
30 ms |
7508 KB |
Output is correct |
9 |
Correct |
29 ms |
7432 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
7352 KB |
Output is correct |
2 |
Correct |
48 ms |
7340 KB |
Output is correct |
3 |
Correct |
47 ms |
7504 KB |
Output is correct |
4 |
Correct |
44 ms |
7508 KB |
Output is correct |
5 |
Correct |
45 ms |
7420 KB |
Output is correct |
6 |
Correct |
44 ms |
7504 KB |
Output is correct |
7 |
Correct |
43 ms |
7504 KB |
Output is correct |
8 |
Correct |
30 ms |
7508 KB |
Output is correct |
9 |
Correct |
29 ms |
7432 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
61 ms |
7180 KB |
Output is correct |
12 |
Correct |
56 ms |
7004 KB |
Output is correct |
13 |
Correct |
57 ms |
7000 KB |
Output is correct |
14 |
Correct |
56 ms |
7000 KB |
Output is correct |
15 |
Correct |
67 ms |
7184 KB |
Output is correct |
16 |
Correct |
60 ms |
6960 KB |
Output is correct |
17 |
Correct |
56 ms |
7168 KB |
Output is correct |
18 |
Correct |
31 ms |
7004 KB |
Output is correct |
19 |
Correct |
31 ms |
7092 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
4 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
700 KB |
Output is correct |
13 |
Correct |
2 ms |
696 KB |
Output is correct |
14 |
Correct |
3 ms |
700 KB |
Output is correct |
15 |
Correct |
3 ms |
692 KB |
Output is correct |
16 |
Correct |
2 ms |
724 KB |
Output is correct |
17 |
Correct |
3 ms |
600 KB |
Output is correct |
18 |
Correct |
3 ms |
604 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
7352 KB |
Output is correct |
2 |
Correct |
48 ms |
7340 KB |
Output is correct |
3 |
Correct |
47 ms |
7504 KB |
Output is correct |
4 |
Correct |
44 ms |
7508 KB |
Output is correct |
5 |
Correct |
45 ms |
7420 KB |
Output is correct |
6 |
Correct |
44 ms |
7504 KB |
Output is correct |
7 |
Correct |
43 ms |
7504 KB |
Output is correct |
8 |
Correct |
30 ms |
7508 KB |
Output is correct |
9 |
Correct |
29 ms |
7432 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
61 ms |
7180 KB |
Output is correct |
12 |
Correct |
56 ms |
7004 KB |
Output is correct |
13 |
Correct |
57 ms |
7000 KB |
Output is correct |
14 |
Correct |
56 ms |
7000 KB |
Output is correct |
15 |
Correct |
67 ms |
7184 KB |
Output is correct |
16 |
Correct |
60 ms |
6960 KB |
Output is correct |
17 |
Correct |
56 ms |
7168 KB |
Output is correct |
18 |
Correct |
31 ms |
7004 KB |
Output is correct |
19 |
Correct |
31 ms |
7092 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
60 ms |
8044 KB |
Output is correct |
22 |
Correct |
61 ms |
8016 KB |
Output is correct |
23 |
Correct |
62 ms |
8028 KB |
Output is correct |
24 |
Correct |
61 ms |
7852 KB |
Output is correct |
25 |
Correct |
65 ms |
8016 KB |
Output is correct |
26 |
Correct |
62 ms |
8024 KB |
Output is correct |
27 |
Correct |
62 ms |
8016 KB |
Output is correct |
28 |
Correct |
42 ms |
8016 KB |
Output is correct |
29 |
Correct |
35 ms |
8028 KB |
Output is correct |
30 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
57 ms |
11860 KB |
Output is correct |
3 |
Correct |
55 ms |
11688 KB |
Output is correct |
4 |
Correct |
52 ms |
11824 KB |
Output is correct |
5 |
Correct |
52 ms |
11860 KB |
Output is correct |
6 |
Correct |
51 ms |
11856 KB |
Output is correct |
7 |
Correct |
51 ms |
11688 KB |
Output is correct |
8 |
Correct |
50 ms |
11860 KB |
Output is correct |
9 |
Correct |
54 ms |
11860 KB |
Output is correct |
10 |
Correct |
56 ms |
11860 KB |
Output is correct |
11 |
Correct |
42 ms |
7352 KB |
Output is correct |
12 |
Correct |
48 ms |
7340 KB |
Output is correct |
13 |
Correct |
47 ms |
7504 KB |
Output is correct |
14 |
Correct |
44 ms |
7508 KB |
Output is correct |
15 |
Correct |
45 ms |
7420 KB |
Output is correct |
16 |
Correct |
44 ms |
7504 KB |
Output is correct |
17 |
Correct |
43 ms |
7504 KB |
Output is correct |
18 |
Correct |
30 ms |
7508 KB |
Output is correct |
19 |
Correct |
29 ms |
7432 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
61 ms |
7180 KB |
Output is correct |
22 |
Correct |
56 ms |
7004 KB |
Output is correct |
23 |
Correct |
57 ms |
7000 KB |
Output is correct |
24 |
Correct |
56 ms |
7000 KB |
Output is correct |
25 |
Correct |
67 ms |
7184 KB |
Output is correct |
26 |
Correct |
60 ms |
6960 KB |
Output is correct |
27 |
Correct |
56 ms |
7168 KB |
Output is correct |
28 |
Correct |
31 ms |
7004 KB |
Output is correct |
29 |
Correct |
31 ms |
7092 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
4 ms |
604 KB |
Output is correct |
42 |
Correct |
2 ms |
700 KB |
Output is correct |
43 |
Correct |
2 ms |
696 KB |
Output is correct |
44 |
Correct |
3 ms |
700 KB |
Output is correct |
45 |
Correct |
3 ms |
692 KB |
Output is correct |
46 |
Correct |
2 ms |
724 KB |
Output is correct |
47 |
Correct |
3 ms |
600 KB |
Output is correct |
48 |
Correct |
3 ms |
604 KB |
Output is correct |
49 |
Correct |
2 ms |
604 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
60 ms |
8044 KB |
Output is correct |
52 |
Correct |
61 ms |
8016 KB |
Output is correct |
53 |
Correct |
62 ms |
8028 KB |
Output is correct |
54 |
Correct |
61 ms |
7852 KB |
Output is correct |
55 |
Correct |
65 ms |
8016 KB |
Output is correct |
56 |
Correct |
62 ms |
8024 KB |
Output is correct |
57 |
Correct |
62 ms |
8016 KB |
Output is correct |
58 |
Correct |
42 ms |
8016 KB |
Output is correct |
59 |
Correct |
35 ms |
8028 KB |
Output is correct |
60 |
Correct |
0 ms |
344 KB |
Output is correct |
61 |
Correct |
78 ms |
11808 KB |
Output is correct |
62 |
Correct |
74 ms |
11856 KB |
Output is correct |
63 |
Correct |
73 ms |
11684 KB |
Output is correct |
64 |
Correct |
75 ms |
11696 KB |
Output is correct |
65 |
Correct |
75 ms |
11860 KB |
Output is correct |
66 |
Correct |
74 ms |
11864 KB |
Output is correct |
67 |
Correct |
75 ms |
11700 KB |
Output is correct |
68 |
Correct |
80 ms |
11900 KB |
Output is correct |
69 |
Correct |
62 ms |
8028 KB |
Output is correct |
70 |
Correct |
0 ms |
344 KB |
Output is correct |