#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 |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
81 ms |
20168 KB |
Output is correct |
3 |
Correct |
84 ms |
20704 KB |
Output is correct |
4 |
Correct |
79 ms |
21080 KB |
Output is correct |
5 |
Correct |
85 ms |
20248 KB |
Output is correct |
6 |
Correct |
81 ms |
20836 KB |
Output is correct |
7 |
Correct |
109 ms |
21180 KB |
Output is correct |
8 |
Correct |
82 ms |
20676 KB |
Output is correct |
9 |
Correct |
74 ms |
21704 KB |
Output is correct |
10 |
Correct |
73 ms |
20168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
15812 KB |
Output is correct |
2 |
Correct |
32 ms |
15724 KB |
Output is correct |
3 |
Correct |
44 ms |
15448 KB |
Output is correct |
4 |
Correct |
30 ms |
16076 KB |
Output is correct |
5 |
Correct |
30 ms |
15056 KB |
Output is correct |
6 |
Correct |
35 ms |
16328 KB |
Output is correct |
7 |
Correct |
32 ms |
15732 KB |
Output is correct |
8 |
Correct |
26 ms |
15056 KB |
Output is correct |
9 |
Correct |
23 ms |
15564 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
15812 KB |
Output is correct |
2 |
Correct |
32 ms |
15724 KB |
Output is correct |
3 |
Correct |
44 ms |
15448 KB |
Output is correct |
4 |
Correct |
30 ms |
16076 KB |
Output is correct |
5 |
Correct |
30 ms |
15056 KB |
Output is correct |
6 |
Correct |
35 ms |
16328 KB |
Output is correct |
7 |
Correct |
32 ms |
15732 KB |
Output is correct |
8 |
Correct |
26 ms |
15056 KB |
Output is correct |
9 |
Correct |
23 ms |
15564 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
44 ms |
16584 KB |
Output is correct |
12 |
Correct |
48 ms |
15568 KB |
Output is correct |
13 |
Correct |
46 ms |
16356 KB |
Output is correct |
14 |
Correct |
45 ms |
15568 KB |
Output is correct |
15 |
Correct |
45 ms |
16072 KB |
Output is correct |
16 |
Correct |
45 ms |
16636 KB |
Output is correct |
17 |
Correct |
44 ms |
16584 KB |
Output is correct |
18 |
Correct |
27 ms |
15568 KB |
Output is correct |
19 |
Correct |
27 ms |
15816 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
1 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 |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
1 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 |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
5 ms |
1196 KB |
Output is correct |
12 |
Correct |
4 ms |
1368 KB |
Output is correct |
13 |
Correct |
4 ms |
1116 KB |
Output is correct |
14 |
Correct |
4 ms |
1112 KB |
Output is correct |
15 |
Correct |
4 ms |
1116 KB |
Output is correct |
16 |
Correct |
4 ms |
1252 KB |
Output is correct |
17 |
Correct |
4 ms |
1116 KB |
Output is correct |
18 |
Correct |
3 ms |
1272 KB |
Output is correct |
19 |
Correct |
2 ms |
1116 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
15812 KB |
Output is correct |
2 |
Correct |
32 ms |
15724 KB |
Output is correct |
3 |
Correct |
44 ms |
15448 KB |
Output is correct |
4 |
Correct |
30 ms |
16076 KB |
Output is correct |
5 |
Correct |
30 ms |
15056 KB |
Output is correct |
6 |
Correct |
35 ms |
16328 KB |
Output is correct |
7 |
Correct |
32 ms |
15732 KB |
Output is correct |
8 |
Correct |
26 ms |
15056 KB |
Output is correct |
9 |
Correct |
23 ms |
15564 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
44 ms |
16584 KB |
Output is correct |
12 |
Correct |
48 ms |
15568 KB |
Output is correct |
13 |
Correct |
46 ms |
16356 KB |
Output is correct |
14 |
Correct |
45 ms |
15568 KB |
Output is correct |
15 |
Correct |
45 ms |
16072 KB |
Output is correct |
16 |
Correct |
45 ms |
16636 KB |
Output is correct |
17 |
Correct |
44 ms |
16584 KB |
Output is correct |
18 |
Correct |
27 ms |
15568 KB |
Output is correct |
19 |
Correct |
27 ms |
15816 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
106 ms |
16556 KB |
Output is correct |
22 |
Correct |
106 ms |
17344 KB |
Output is correct |
23 |
Correct |
105 ms |
17860 KB |
Output is correct |
24 |
Correct |
103 ms |
17336 KB |
Output is correct |
25 |
Correct |
116 ms |
16236 KB |
Output is correct |
26 |
Correct |
105 ms |
16320 KB |
Output is correct |
27 |
Correct |
107 ms |
16232 KB |
Output is correct |
28 |
Correct |
52 ms |
17932 KB |
Output is correct |
29 |
Correct |
54 ms |
17984 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
81 ms |
20168 KB |
Output is correct |
3 |
Correct |
84 ms |
20704 KB |
Output is correct |
4 |
Correct |
79 ms |
21080 KB |
Output is correct |
5 |
Correct |
85 ms |
20248 KB |
Output is correct |
6 |
Correct |
81 ms |
20836 KB |
Output is correct |
7 |
Correct |
109 ms |
21180 KB |
Output is correct |
8 |
Correct |
82 ms |
20676 KB |
Output is correct |
9 |
Correct |
74 ms |
21704 KB |
Output is correct |
10 |
Correct |
73 ms |
20168 KB |
Output is correct |
11 |
Correct |
27 ms |
15812 KB |
Output is correct |
12 |
Correct |
32 ms |
15724 KB |
Output is correct |
13 |
Correct |
44 ms |
15448 KB |
Output is correct |
14 |
Correct |
30 ms |
16076 KB |
Output is correct |
15 |
Correct |
30 ms |
15056 KB |
Output is correct |
16 |
Correct |
35 ms |
16328 KB |
Output is correct |
17 |
Correct |
32 ms |
15732 KB |
Output is correct |
18 |
Correct |
26 ms |
15056 KB |
Output is correct |
19 |
Correct |
23 ms |
15564 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
44 ms |
16584 KB |
Output is correct |
22 |
Correct |
48 ms |
15568 KB |
Output is correct |
23 |
Correct |
46 ms |
16356 KB |
Output is correct |
24 |
Correct |
45 ms |
15568 KB |
Output is correct |
25 |
Correct |
45 ms |
16072 KB |
Output is correct |
26 |
Correct |
45 ms |
16636 KB |
Output is correct |
27 |
Correct |
44 ms |
16584 KB |
Output is correct |
28 |
Correct |
27 ms |
15568 KB |
Output is correct |
29 |
Correct |
27 ms |
15816 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 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 |
1 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 |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
5 ms |
1196 KB |
Output is correct |
42 |
Correct |
4 ms |
1368 KB |
Output is correct |
43 |
Correct |
4 ms |
1116 KB |
Output is correct |
44 |
Correct |
4 ms |
1112 KB |
Output is correct |
45 |
Correct |
4 ms |
1116 KB |
Output is correct |
46 |
Correct |
4 ms |
1252 KB |
Output is correct |
47 |
Correct |
4 ms |
1116 KB |
Output is correct |
48 |
Correct |
3 ms |
1272 KB |
Output is correct |
49 |
Correct |
2 ms |
1116 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
106 ms |
16556 KB |
Output is correct |
52 |
Correct |
106 ms |
17344 KB |
Output is correct |
53 |
Correct |
105 ms |
17860 KB |
Output is correct |
54 |
Correct |
103 ms |
17336 KB |
Output is correct |
55 |
Correct |
116 ms |
16236 KB |
Output is correct |
56 |
Correct |
105 ms |
16320 KB |
Output is correct |
57 |
Correct |
107 ms |
16232 KB |
Output is correct |
58 |
Correct |
52 ms |
17932 KB |
Output is correct |
59 |
Correct |
54 ms |
17984 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
155 ms |
20704 KB |
Output is correct |
62 |
Correct |
147 ms |
20928 KB |
Output is correct |
63 |
Correct |
161 ms |
21576 KB |
Output is correct |
64 |
Correct |
133 ms |
21496 KB |
Output is correct |
65 |
Correct |
136 ms |
21724 KB |
Output is correct |
66 |
Correct |
139 ms |
21460 KB |
Output is correct |
67 |
Correct |
145 ms |
21080 KB |
Output is correct |
68 |
Correct |
123 ms |
20992 KB |
Output is correct |
69 |
Correct |
109 ms |
17600 KB |
Output is correct |
70 |
Correct |
0 ms |
348 KB |
Output is correct |