Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- using lli = long long;
- const int N = 1e5;
- int fw[N + 10], ar[N + 10];
- vector <int> num;
- int n, sz;
- void Update(int pst){
- for(int i=pst;i<=sz;i+=i&-i)
- fw[i] ++;
- }
- int Sum(int pst, int sum = 0){
- for(int i=pst;i>=1;i-=i&-i)
- sum += fw[i];
- return sum;
- }
- int main(){
- scanf("%d", &n);
- for(int i=1;i<=n;i++){
- scanf("%d", &ar[i]);
- num.push_back(ar[i]);
- }
- sort(num.begin(), num.end());
- num.resize( unique(num.begin(), num.end()) - num.begin() );
- sz = num.size();
- lli ans = 0;
- for(int i=1;i<=n;i++){
- int idx;
- {
- int l = 0, r = sz - 1;
- while(l <= r){
- int mid = (l + r) / 2;
- if(num[mid] == ar[i]){
- idx = mid + 1;
- break;
- }
- else if(num[mid] < ar[i]) l = mid + 1;
- else r = mid - 1;
- }
- }
- ans += Sum(sz) - Sum(idx);
- Update(idx);
- }
- printf("%lld", ans);
- return 0;
- }
Add Comment
Please, Sign In to add comment