WA pts0
查看原帖
WA pts0
637788
kimi0705楼主2023/7/17 17:17
// Time: 2023-07-17 16:49:07
// Problem: P1774 最接近神的人
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P1774
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// Author: Zhong Jiaxuan
// Luogu: 637788
// Email: zhongjiaxuankimi@qq.com
// Tips:
//   - INT_MAX = 2147483647
//   - INT_MIN = -2147483648
// Tag:
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define int long long
#define db double
using namespace std;
const int N = 5e5 + 100;
struct Num {
  int num, id;
} arr[N];
int n, ans;
int tree[N];
inline int lowbit(int x) { return x & (-x); }
void add(int x, int y) {
  while (x <= n) {
    tree[x] += y;
    x += lowbit(x);
  }
}
int sum(int x) {
  int ans = 0;
  while (x > 0) {
    ans += tree[x];
    x -= lowbit(x);
  }
  return ans;
}
bool cmp(Num a, Num b) { return a.num < b.num; }
void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> arr[i].num, arr[i].id = i;
  sort(arr + 1, arr + n + 1, cmp);
  for (int i = 1; i <= n; i++)
    ans += sum(arr[i].id - 1), add(arr[i].id, 1);
  cout << ans;
}
signed main() {
  solve();
  return 0;
}
2023/7/17 17:17
加载中...