#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 |
9732 KB |
Output is correct |
2 |
Correct |
184 ms |
22492 KB |
Output is correct |
3 |
Correct |
182 ms |
22604 KB |
Output is correct |
4 |
Correct |
188 ms |
22580 KB |
Output is correct |
5 |
Correct |
180 ms |
22600 KB |
Output is correct |
6 |
Correct |
182 ms |
22624 KB |
Output is correct |
7 |
Correct |
184 ms |
22592 KB |
Output is correct |
8 |
Correct |
181 ms |
22612 KB |
Output is correct |
9 |
Correct |
178 ms |
22508 KB |
Output is correct |
10 |
Correct |
177 ms |
22532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
128 ms |
22176 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
128 ms |
22176 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9680 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
6 ms |
9704 KB |
Output is correct |
8 |
Incorrect |
6 ms |
9812 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9680 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
6 ms |
9704 KB |
Output is correct |
8 |
Incorrect |
6 ms |
9812 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
128 ms |
22176 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9732 KB |
Output is correct |
2 |
Correct |
184 ms |
22492 KB |
Output is correct |
3 |
Correct |
182 ms |
22604 KB |
Output is correct |
4 |
Correct |
188 ms |
22580 KB |
Output is correct |
5 |
Correct |
180 ms |
22600 KB |
Output is correct |
6 |
Correct |
182 ms |
22624 KB |
Output is correct |
7 |
Correct |
184 ms |
22592 KB |
Output is correct |
8 |
Correct |
181 ms |
22612 KB |
Output is correct |
9 |
Correct |
178 ms |
22508 KB |
Output is correct |
10 |
Correct |
177 ms |
22532 KB |
Output is correct |
11 |
Incorrect |
128 ms |
22176 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |