This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
link: https://oj.uz/problem/view/NOI20_mountains
*/
#include<bits/stdc++.h>
#define endl '\n'
#define pb push_back
#define fr first
#define sc second
#define ll long long int
#define bit(idx) idx&(-idx)
#define bin(x, a) bitset<a>(x)
#define all(A) A.begin(), A.end()
#define debug(x) cout << #x << " = " << x << endl;
using namespace std;
const int MAXN = 3e5 + 5;
ll n, A[MAXN], BIT[MAXN], tmp[MAXN], sz;
void upd(int idx){
for(int _idx = idx + 1; _idx < MAXN; _idx += bit(_idx)){
++ BIT[_idx];
}
}
ll qry(int idx){
ll ret = 0;
for(int _idx = idx + 1; _idx > 0; _idx -= bit(_idx)){
ret += BIT[_idx];
}
return ret;
}
int main(){
ios_base::sync_with_stdio(NULL); cin.tie(); cout.tie();
cin >> n;
for(int i = 0; i < n; i ++){
cin >> A[i];
tmp[sz ++] = A[i];
}
sort(tmp, tmp + sz);
sz = unique(tmp, tmp + sz) - tmp;
for(int i = 0; i < n; i ++)
A[i] = lower_bound(tmp, tmp + sz, A[i]) - tmp;
ll L[MAXN] = {0}, R[MAXN] = {0};
for(int i = 0; i < n; i ++){
L[i] = qry(A[i] - 1);
upd(A[i]);
}
memset(BIT, 0, sizeof(BIT));
for(int i = n - 1; i >= 0; i --){
R[i] = qry(A[i] - 1);
upd(A[i]);
}
/// cout << "A: "; for(int i = 0; i < n; i ++) cout << A[i] << " "; cout << endl;
/// cout << "L: "; for(int i = 0; i < n; i ++) cout << L[i] << " "; cout << endl;
/// cout << "R: "; for(int i = 0; i < n; i ++) cout << R[i] << " "; cout << endl;
ll ans = 0;
for(int i = 0; i < n; i ++)
ans += L[i] * R[i];
cout << ans << endl;
}
/**
5
0 1 1 0 1
2
6
500 20 900 0 900 70
7
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |