#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
#define pairll pair<ll,ll>
#define lpairll pair<ll,pairll>
#define repp(i,a,b) for (ll i = a; i <= b; i++)
#define repz(i,a,b) for (ll i = a; i < b; i++)
#define repm(i,a,b) for (ll i = a; i >= b; i--)
#define fr first
#define sc second
#define mp make_pair
#define pb push_back
const ll N = 3e5+5, MOD = 1e9+7;
ll tc = 1, n, m, ar[N], lr[N], rr[N], tree[N*4];
string s, s1, s2, ye = "YA", no = "TIDAK";
vector<pairll> v;
void input(){
cin >> n;
repp(i,1,n){
cin >> ar[i];
v.pb(mp(ar[i],i));
}
sort(v.begin(),v.end());
ll pre = -1, cnt = 0;
for (auto i : v){
if (i.fr != pre){
pre = i.fr;
cnt++;
}
ar[i.sc] = cnt;
}
}
void upd(ll l, ll r, ll idx, ll pos, ll val){
if (l == r){
tree[idx] += val;
return;
}
ll md = (l+r)/2;
if (pos <= md) upd(l,md,idx*2,pos,val);
else upd(md+1,r,idx*2+1,pos,val);
tree[idx] = tree[idx*2]+tree[idx*2+1];
}
ll que(ll l, ll r, ll idx, ll x, ll y){
if (x > y || l > r || l > y || r < x) return 0;
if (x <= l && r <= y) return tree[idx];
ll md = (l+r)/2;
return que(l,md,idx*2,x,y)+que(md+1,r,idx*2+1,x,y);
}
void solve(){
memset(tree,0,sizeof(tree));
repp(i,1,n){
lr[i] = que(1,n,1,1,ar[i]-1);
upd(1,n,1,ar[i],1);
}
memset(tree,0,sizeof(tree));
repm(i,n,1){
rr[i] = que(1,n,1,1,ar[i]-1);
upd(1,n,1,ar[i],1);
}
ll ans = 0;
repp(i,1,n){
ans += lr[i]*rr[i];
//cout << i << " " << lr[i] << " " << rr[i] << endl;
}
cout << ans << endl;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
//cin >> tc;
while(tc--){
input();
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
183 ms |
21728 KB |
Output is correct |
3 |
Correct |
180 ms |
21488 KB |
Output is correct |
4 |
Correct |
177 ms |
21584 KB |
Output is correct |
5 |
Correct |
181 ms |
21496 KB |
Output is correct |
6 |
Correct |
184 ms |
21544 KB |
Output is correct |
7 |
Correct |
177 ms |
21624 KB |
Output is correct |
8 |
Correct |
179 ms |
21496 KB |
Output is correct |
9 |
Correct |
181 ms |
21484 KB |
Output is correct |
10 |
Correct |
174 ms |
21472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
129 ms |
21468 KB |
Output is correct |
2 |
Correct |
135 ms |
22056 KB |
Output is correct |
3 |
Correct |
139 ms |
22208 KB |
Output is correct |
4 |
Correct |
132 ms |
22136 KB |
Output is correct |
5 |
Correct |
156 ms |
22164 KB |
Output is correct |
6 |
Correct |
132 ms |
22164 KB |
Output is correct |
7 |
Correct |
137 ms |
22188 KB |
Output is correct |
8 |
Correct |
115 ms |
22200 KB |
Output is correct |
9 |
Correct |
115 ms |
22096 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
129 ms |
21468 KB |
Output is correct |
2 |
Correct |
135 ms |
22056 KB |
Output is correct |
3 |
Correct |
139 ms |
22208 KB |
Output is correct |
4 |
Correct |
132 ms |
22136 KB |
Output is correct |
5 |
Correct |
156 ms |
22164 KB |
Output is correct |
6 |
Correct |
132 ms |
22164 KB |
Output is correct |
7 |
Correct |
137 ms |
22188 KB |
Output is correct |
8 |
Correct |
115 ms |
22200 KB |
Output is correct |
9 |
Correct |
115 ms |
22096 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
181 ms |
22312 KB |
Output is correct |
12 |
Correct |
186 ms |
22440 KB |
Output is correct |
13 |
Correct |
180 ms |
22424 KB |
Output is correct |
14 |
Correct |
182 ms |
22444 KB |
Output is correct |
15 |
Correct |
188 ms |
22460 KB |
Output is correct |
16 |
Correct |
183 ms |
22332 KB |
Output is correct |
17 |
Correct |
180 ms |
22328 KB |
Output is correct |
18 |
Correct |
123 ms |
22384 KB |
Output is correct |
19 |
Correct |
136 ms |
22644 KB |
Output is correct |
20 |
Correct |
6 ms |
9672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9696 KB |
Output is correct |
2 |
Correct |
5 ms |
9704 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9764 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9696 KB |
Output is correct |
2 |
Correct |
5 ms |
9704 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9764 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9672 KB |
Output is correct |
11 |
Correct |
14 ms |
10456 KB |
Output is correct |
12 |
Correct |
12 ms |
10368 KB |
Output is correct |
13 |
Correct |
17 ms |
10444 KB |
Output is correct |
14 |
Correct |
13 ms |
10380 KB |
Output is correct |
15 |
Correct |
14 ms |
10444 KB |
Output is correct |
16 |
Correct |
12 ms |
10456 KB |
Output is correct |
17 |
Correct |
12 ms |
10384 KB |
Output is correct |
18 |
Correct |
14 ms |
10456 KB |
Output is correct |
19 |
Correct |
10 ms |
10200 KB |
Output is correct |
20 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
129 ms |
21468 KB |
Output is correct |
2 |
Correct |
135 ms |
22056 KB |
Output is correct |
3 |
Correct |
139 ms |
22208 KB |
Output is correct |
4 |
Correct |
132 ms |
22136 KB |
Output is correct |
5 |
Correct |
156 ms |
22164 KB |
Output is correct |
6 |
Correct |
132 ms |
22164 KB |
Output is correct |
7 |
Correct |
137 ms |
22188 KB |
Output is correct |
8 |
Correct |
115 ms |
22200 KB |
Output is correct |
9 |
Correct |
115 ms |
22096 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
181 ms |
22312 KB |
Output is correct |
12 |
Correct |
186 ms |
22440 KB |
Output is correct |
13 |
Correct |
180 ms |
22424 KB |
Output is correct |
14 |
Correct |
182 ms |
22444 KB |
Output is correct |
15 |
Correct |
188 ms |
22460 KB |
Output is correct |
16 |
Correct |
183 ms |
22332 KB |
Output is correct |
17 |
Correct |
180 ms |
22328 KB |
Output is correct |
18 |
Correct |
123 ms |
22384 KB |
Output is correct |
19 |
Correct |
136 ms |
22644 KB |
Output is correct |
20 |
Correct |
6 ms |
9672 KB |
Output is correct |
21 |
Correct |
240 ms |
23316 KB |
Output is correct |
22 |
Correct |
247 ms |
23312 KB |
Output is correct |
23 |
Correct |
260 ms |
23364 KB |
Output is correct |
24 |
Correct |
242 ms |
23212 KB |
Output is correct |
25 |
Correct |
248 ms |
23272 KB |
Output is correct |
26 |
Correct |
245 ms |
23256 KB |
Output is correct |
27 |
Correct |
261 ms |
23244 KB |
Output is correct |
28 |
Correct |
150 ms |
23340 KB |
Output is correct |
29 |
Correct |
142 ms |
23312 KB |
Output is correct |
30 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
183 ms |
21728 KB |
Output is correct |
3 |
Correct |
180 ms |
21488 KB |
Output is correct |
4 |
Correct |
177 ms |
21584 KB |
Output is correct |
5 |
Correct |
181 ms |
21496 KB |
Output is correct |
6 |
Correct |
184 ms |
21544 KB |
Output is correct |
7 |
Correct |
177 ms |
21624 KB |
Output is correct |
8 |
Correct |
179 ms |
21496 KB |
Output is correct |
9 |
Correct |
181 ms |
21484 KB |
Output is correct |
10 |
Correct |
174 ms |
21472 KB |
Output is correct |
11 |
Correct |
129 ms |
21468 KB |
Output is correct |
12 |
Correct |
135 ms |
22056 KB |
Output is correct |
13 |
Correct |
139 ms |
22208 KB |
Output is correct |
14 |
Correct |
132 ms |
22136 KB |
Output is correct |
15 |
Correct |
156 ms |
22164 KB |
Output is correct |
16 |
Correct |
132 ms |
22164 KB |
Output is correct |
17 |
Correct |
137 ms |
22188 KB |
Output is correct |
18 |
Correct |
115 ms |
22200 KB |
Output is correct |
19 |
Correct |
115 ms |
22096 KB |
Output is correct |
20 |
Correct |
5 ms |
9684 KB |
Output is correct |
21 |
Correct |
181 ms |
22312 KB |
Output is correct |
22 |
Correct |
186 ms |
22440 KB |
Output is correct |
23 |
Correct |
180 ms |
22424 KB |
Output is correct |
24 |
Correct |
182 ms |
22444 KB |
Output is correct |
25 |
Correct |
188 ms |
22460 KB |
Output is correct |
26 |
Correct |
183 ms |
22332 KB |
Output is correct |
27 |
Correct |
180 ms |
22328 KB |
Output is correct |
28 |
Correct |
123 ms |
22384 KB |
Output is correct |
29 |
Correct |
136 ms |
22644 KB |
Output is correct |
30 |
Correct |
6 ms |
9672 KB |
Output is correct |
31 |
Correct |
6 ms |
9696 KB |
Output is correct |
32 |
Correct |
5 ms |
9704 KB |
Output is correct |
33 |
Correct |
6 ms |
9684 KB |
Output is correct |
34 |
Correct |
4 ms |
9684 KB |
Output is correct |
35 |
Correct |
6 ms |
9684 KB |
Output is correct |
36 |
Correct |
5 ms |
9684 KB |
Output is correct |
37 |
Correct |
5 ms |
9684 KB |
Output is correct |
38 |
Correct |
5 ms |
9764 KB |
Output is correct |
39 |
Correct |
5 ms |
9684 KB |
Output is correct |
40 |
Correct |
5 ms |
9672 KB |
Output is correct |
41 |
Correct |
14 ms |
10456 KB |
Output is correct |
42 |
Correct |
12 ms |
10368 KB |
Output is correct |
43 |
Correct |
17 ms |
10444 KB |
Output is correct |
44 |
Correct |
13 ms |
10380 KB |
Output is correct |
45 |
Correct |
14 ms |
10444 KB |
Output is correct |
46 |
Correct |
12 ms |
10456 KB |
Output is correct |
47 |
Correct |
12 ms |
10384 KB |
Output is correct |
48 |
Correct |
14 ms |
10456 KB |
Output is correct |
49 |
Correct |
10 ms |
10200 KB |
Output is correct |
50 |
Correct |
5 ms |
9684 KB |
Output is correct |
51 |
Correct |
240 ms |
23316 KB |
Output is correct |
52 |
Correct |
247 ms |
23312 KB |
Output is correct |
53 |
Correct |
260 ms |
23364 KB |
Output is correct |
54 |
Correct |
242 ms |
23212 KB |
Output is correct |
55 |
Correct |
248 ms |
23272 KB |
Output is correct |
56 |
Correct |
245 ms |
23256 KB |
Output is correct |
57 |
Correct |
261 ms |
23244 KB |
Output is correct |
58 |
Correct |
150 ms |
23340 KB |
Output is correct |
59 |
Correct |
142 ms |
23312 KB |
Output is correct |
60 |
Correct |
5 ms |
9684 KB |
Output is correct |
61 |
Correct |
299 ms |
27008 KB |
Output is correct |
62 |
Correct |
295 ms |
27176 KB |
Output is correct |
63 |
Correct |
286 ms |
26992 KB |
Output is correct |
64 |
Correct |
296 ms |
27108 KB |
Output is correct |
65 |
Correct |
301 ms |
27016 KB |
Output is correct |
66 |
Correct |
318 ms |
27100 KB |
Output is correct |
67 |
Correct |
298 ms |
27048 KB |
Output is correct |
68 |
Correct |
281 ms |
27152 KB |
Output is correct |
69 |
Correct |
256 ms |
23424 KB |
Output is correct |
70 |
Correct |
5 ms |
9740 KB |
Output is correct |