#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = b; i >= a; i --)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPD(i, n) for (int i = n - 1; i >= 0; --i)
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void setupIO(){
#define name "Whisper"
//Phu Trong from Nguyen Tat Thanh High School for gifted student
srand(time(NULL));
cin.tie(nullptr)->sync_with_stdio(false); cout.tie(nullptr);
//freopen(name".inp", "r", stdin);
//freopen(name".out", "w", stdout);
cout << fixed << setprecision(10);
}
template <class X, class Y>
bool minimize(X &x, const Y &y){
X eps = 1e-9;
if (x > y + eps) {x = y; return 1;}
return 0;
}
template <class X, class Y>
bool maximize(X &x, const Y &y){
X eps = 1e-9;
if (x + eps < y) {x = y; return 1;}
return 0;
}
const int MAX = 3e5 + 5;
int nArr;
int a[MAX];
struct SegmentTree{
int n;
vector<int> f;
SegmentTree(int _n = 0){
this -> n = _n;
f.resize((n << 1));
}
void modify(int p, int val){
for (f[p += n] += val; p > 0; p >>= 1){
f[p >> 1] = f[p] + f[p ^ 1];
}
}
int query(int l, int r){
int ans = 0;
for(l += n, r += n + 1; l < r; l >>= 1, r >>= 1){
if(l & 1) ans += f[l++];
if(r & 1) ans += f[--r];
}
return ans;
}
}l, r;
void Whisper(){
cin >> nArr;
for (int i = 1; i <= nArr; ++i) cin >> a[i];
vector<int> compress;
for (int i = 1; i <= nArr; ++i){
compress.push_back(a[i]);
}
sort(compress.begin(), compress.end());
compress.resize(unique(compress.begin(), compress.end()) - compress.begin());
for (int i = 1; i <= nArr; ++i){
a[i] = lower_bound(compress.begin(), compress.end(), a[i]) - compress.begin() + 1;
}
l = SegmentTree(nArr + 1), r = SegmentTree(nArr + 1);
for (int i = 1; i <= nArr; ++i){
r.modify(a[i], 1);
}
int ans = 0;
for (int i = 1; i <= nArr; ++i){
r.modify(a[i], -1);
int left = l.query(1, a[i] - 1);
int right = r.query(1, a[i] - 1);
ans += left * right;
l.modify(a[i], 1);
}
cout << ans;
}
signed main(){
setupIO();
int Test = 1;
// cin >> Test;
for ( int i = 1 ; i <= Test ; i++ ){
Whisper();
if (i < Test) cout << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
113 ms |
19352 KB |
Output is correct |
3 |
Correct |
109 ms |
19040 KB |
Output is correct |
4 |
Correct |
129 ms |
17572 KB |
Output is correct |
5 |
Correct |
159 ms |
17756 KB |
Output is correct |
6 |
Correct |
109 ms |
17656 KB |
Output is correct |
7 |
Correct |
117 ms |
19268 KB |
Output is correct |
8 |
Correct |
133 ms |
19168 KB |
Output is correct |
9 |
Correct |
99 ms |
18888 KB |
Output is correct |
10 |
Correct |
100 ms |
18168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
16588 KB |
Output is correct |
2 |
Correct |
40 ms |
15180 KB |
Output is correct |
3 |
Correct |
55 ms |
15980 KB |
Output is correct |
4 |
Correct |
36 ms |
16580 KB |
Output is correct |
5 |
Correct |
54 ms |
15192 KB |
Output is correct |
6 |
Correct |
36 ms |
15196 KB |
Output is correct |
7 |
Correct |
36 ms |
15568 KB |
Output is correct |
8 |
Correct |
37 ms |
15640 KB |
Output is correct |
9 |
Correct |
31 ms |
15480 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
16588 KB |
Output is correct |
2 |
Correct |
40 ms |
15180 KB |
Output is correct |
3 |
Correct |
55 ms |
15980 KB |
Output is correct |
4 |
Correct |
36 ms |
16580 KB |
Output is correct |
5 |
Correct |
54 ms |
15192 KB |
Output is correct |
6 |
Correct |
36 ms |
15196 KB |
Output is correct |
7 |
Correct |
36 ms |
15568 KB |
Output is correct |
8 |
Correct |
37 ms |
15640 KB |
Output is correct |
9 |
Correct |
31 ms |
15480 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
63 ms |
15448 KB |
Output is correct |
12 |
Correct |
63 ms |
16144 KB |
Output is correct |
13 |
Correct |
64 ms |
16536 KB |
Output is correct |
14 |
Correct |
64 ms |
17012 KB |
Output is correct |
15 |
Correct |
66 ms |
15300 KB |
Output is correct |
16 |
Correct |
58 ms |
16396 KB |
Output is correct |
17 |
Correct |
92 ms |
16068 KB |
Output is correct |
18 |
Correct |
46 ms |
16716 KB |
Output is correct |
19 |
Correct |
36 ms |
17096 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
476 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
464 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
476 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
464 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
8 ms |
1116 KB |
Output is correct |
12 |
Correct |
6 ms |
1284 KB |
Output is correct |
13 |
Correct |
6 ms |
1100 KB |
Output is correct |
14 |
Correct |
5 ms |
1116 KB |
Output is correct |
15 |
Correct |
5 ms |
1116 KB |
Output is correct |
16 |
Correct |
5 ms |
1116 KB |
Output is correct |
17 |
Correct |
5 ms |
1116 KB |
Output is correct |
18 |
Correct |
5 ms |
1112 KB |
Output is correct |
19 |
Correct |
3 ms |
1116 KB |
Output is correct |
20 |
Correct |
1 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
16588 KB |
Output is correct |
2 |
Correct |
40 ms |
15180 KB |
Output is correct |
3 |
Correct |
55 ms |
15980 KB |
Output is correct |
4 |
Correct |
36 ms |
16580 KB |
Output is correct |
5 |
Correct |
54 ms |
15192 KB |
Output is correct |
6 |
Correct |
36 ms |
15196 KB |
Output is correct |
7 |
Correct |
36 ms |
15568 KB |
Output is correct |
8 |
Correct |
37 ms |
15640 KB |
Output is correct |
9 |
Correct |
31 ms |
15480 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
63 ms |
15448 KB |
Output is correct |
12 |
Correct |
63 ms |
16144 KB |
Output is correct |
13 |
Correct |
64 ms |
16536 KB |
Output is correct |
14 |
Correct |
64 ms |
17012 KB |
Output is correct |
15 |
Correct |
66 ms |
15300 KB |
Output is correct |
16 |
Correct |
58 ms |
16396 KB |
Output is correct |
17 |
Correct |
92 ms |
16068 KB |
Output is correct |
18 |
Correct |
46 ms |
16716 KB |
Output is correct |
19 |
Correct |
36 ms |
17096 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
144 ms |
15524 KB |
Output is correct |
22 |
Correct |
139 ms |
16456 KB |
Output is correct |
23 |
Correct |
162 ms |
17080 KB |
Output is correct |
24 |
Correct |
141 ms |
16308 KB |
Output is correct |
25 |
Correct |
136 ms |
16048 KB |
Output is correct |
26 |
Correct |
138 ms |
17456 KB |
Output is correct |
27 |
Correct |
144 ms |
16192 KB |
Output is correct |
28 |
Correct |
71 ms |
15816 KB |
Output is correct |
29 |
Correct |
73 ms |
16948 KB |
Output is correct |
30 |
Correct |
1 ms |
496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
113 ms |
19352 KB |
Output is correct |
3 |
Correct |
109 ms |
19040 KB |
Output is correct |
4 |
Correct |
129 ms |
17572 KB |
Output is correct |
5 |
Correct |
159 ms |
17756 KB |
Output is correct |
6 |
Correct |
109 ms |
17656 KB |
Output is correct |
7 |
Correct |
117 ms |
19268 KB |
Output is correct |
8 |
Correct |
133 ms |
19168 KB |
Output is correct |
9 |
Correct |
99 ms |
18888 KB |
Output is correct |
10 |
Correct |
100 ms |
18168 KB |
Output is correct |
11 |
Correct |
39 ms |
16588 KB |
Output is correct |
12 |
Correct |
40 ms |
15180 KB |
Output is correct |
13 |
Correct |
55 ms |
15980 KB |
Output is correct |
14 |
Correct |
36 ms |
16580 KB |
Output is correct |
15 |
Correct |
54 ms |
15192 KB |
Output is correct |
16 |
Correct |
36 ms |
15196 KB |
Output is correct |
17 |
Correct |
36 ms |
15568 KB |
Output is correct |
18 |
Correct |
37 ms |
15640 KB |
Output is correct |
19 |
Correct |
31 ms |
15480 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
63 ms |
15448 KB |
Output is correct |
22 |
Correct |
63 ms |
16144 KB |
Output is correct |
23 |
Correct |
64 ms |
16536 KB |
Output is correct |
24 |
Correct |
64 ms |
17012 KB |
Output is correct |
25 |
Correct |
66 ms |
15300 KB |
Output is correct |
26 |
Correct |
58 ms |
16396 KB |
Output is correct |
27 |
Correct |
92 ms |
16068 KB |
Output is correct |
28 |
Correct |
46 ms |
16716 KB |
Output is correct |
29 |
Correct |
36 ms |
17096 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
600 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
476 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
464 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
8 ms |
1116 KB |
Output is correct |
42 |
Correct |
6 ms |
1284 KB |
Output is correct |
43 |
Correct |
6 ms |
1100 KB |
Output is correct |
44 |
Correct |
5 ms |
1116 KB |
Output is correct |
45 |
Correct |
5 ms |
1116 KB |
Output is correct |
46 |
Correct |
5 ms |
1116 KB |
Output is correct |
47 |
Correct |
5 ms |
1116 KB |
Output is correct |
48 |
Correct |
5 ms |
1112 KB |
Output is correct |
49 |
Correct |
3 ms |
1116 KB |
Output is correct |
50 |
Correct |
1 ms |
504 KB |
Output is correct |
51 |
Correct |
144 ms |
15524 KB |
Output is correct |
52 |
Correct |
139 ms |
16456 KB |
Output is correct |
53 |
Correct |
162 ms |
17080 KB |
Output is correct |
54 |
Correct |
141 ms |
16308 KB |
Output is correct |
55 |
Correct |
136 ms |
16048 KB |
Output is correct |
56 |
Correct |
138 ms |
17456 KB |
Output is correct |
57 |
Correct |
144 ms |
16192 KB |
Output is correct |
58 |
Correct |
71 ms |
15816 KB |
Output is correct |
59 |
Correct |
73 ms |
16948 KB |
Output is correct |
60 |
Correct |
1 ms |
496 KB |
Output is correct |
61 |
Correct |
188 ms |
19308 KB |
Output is correct |
62 |
Correct |
226 ms |
18112 KB |
Output is correct |
63 |
Correct |
197 ms |
17812 KB |
Output is correct |
64 |
Correct |
199 ms |
19016 KB |
Output is correct |
65 |
Correct |
174 ms |
17908 KB |
Output is correct |
66 |
Correct |
189 ms |
18876 KB |
Output is correct |
67 |
Correct |
171 ms |
17748 KB |
Output is correct |
68 |
Correct |
159 ms |
17992 KB |
Output is correct |
69 |
Correct |
138 ms |
17068 KB |
Output is correct |
70 |
Correct |
0 ms |
348 KB |
Output is correct |