当前位置:网站首页>P1908 逆序对
P1908 逆序对
2022-07-02 10:20:00 【fu福寿草】
树状数组+离散化
离散化的技巧就是给输入的顺序按权值(输入的值)排序
如 [5,4,2,6,3,1] 排完序后得到的顺序序列就是 [6,3,5,2,1,4]
两者的逆序对数量相等。
我们可以这么理解:在这个有序序列中任意选一个数,对于这个数来说,它前面的所有数都小于它,如果前面数在原数组中的下标大于它,则构成一个逆序对。
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
struct fenwick {
const int n;
std::vector<int> c;
fenwick(int n) : c(n + 10), n(n + 10) {
}
void add(int pos, int val) {
for (int i = pos; i < n; i += i & -i) {
c[i] += val;
}
}
int sum(int pos) {
int ans = 0;
for (int i = pos; i > 0; i -= i & -i) {
ans += c[i];
}
return ans;
}
int range(int l, int r) {
return std::abs(sum(r) - sum(l)); }
};
constexpr int N = 5e5 + 100;
pair<int, int> a[N];
void solve() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i].first;
a[i].second = i + 1;
}
sort(a, a + n);
int ans = 0;
fenwick f(n);
for (int i = 0; i < n; i++) {
ans += f.range(a[i].second, n);
f.add(a[i].second, 1);
}
cout << ans << "\n";
}
int32_t main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int tt(1);
//std::cin >> tt;
while (tt--) {
solve();
}
return 0;
}
边栏推荐
- Solution: Compression Technology (original version and sequel version)
- 验证失败,请检查您的回电网址。您可以按照指导进行操作
- Detailed collection of common MySQL commands
- [Unity]使用GB2312,打包后程序不正常解决方案
- [technology development-22]: rapid overview of the application and development of network and communication technology-2-communication Technology
- selenium 在pycharm中安装selenium
- [USACO05JAN]Watchcow S(欧拉回路)
- Verification failed, please check your call back website. You can follow the instructions
- 故事点 vs. 人天
- Getting started with QT - making a simple calculator
猜你喜欢

大家信夫一站式信用平台让信用场景“用起来

Chaos engineering platform chaosblade box new heavy release

Drawing Nyquist diagram with MATLAB

题解:《你的飞碟在这儿》、《哥德巴赫猜想》

瀏覽器驅動的下載

Three methods of finding LCA of the nearest common ancestor
![[indomitable medal activity] life goes on and writing goes on](/img/c1/54e3f1b37db25af3f1998b39da301b.png)
[indomitable medal activity] life goes on and writing goes on

Common options of tcpdump command: Three
![[cloud native database] what to do when encountering slow SQL (Part 1)?](/img/ac/a59189aba1901e769beec1ec7e6d32.png)
[cloud native database] what to do when encountering slow SQL (Part 1)?
![[Blue Bridge Cup] children's worship circle](/img/ad/5af4fe76ad5d1426bc460904d7f049.jpg)
[Blue Bridge Cup] children's worship circle
随机推荐
Chinese name extraction (toy code - accurate head is too small, right to play)
Everyone believes that the one-stop credit platform makes the credit scenario "useful"
What are the classifications of SSL certificates? How to choose the appropriate SSL certificate?
qt中uic的使用
Stone merging Board [interval DP] (ordinary stone Merging & Ring Stone merging)
Qt-制作一个简单的计算器-实现四则运算-将结果以对话框的形式弹出来
你的 Sleep 服务会梦到服务网格外的 bookinfo 吗
OpenAPI generator: simplify the restful API development process
Bridge of undirected graph
Will your sleep service dream of the extra bookinfo on the service network
错误:EACCES:权限被拒绝,访问“/usr/lib/node_modules”
Node. JS accessing PostgreSQL database through ODBC
Qt-制作一个简单的计算器-实现四则运算
Subcontracting configuration of uniapp applet subpackages
BeanUtils -- shallow copy -- example / principle
QT - make a simple calculator - realize four operations
Student course selection information management system based on ssm+jsp framework [source code + database]
selenium,元素操作以及浏览器操作方法
Origin绘制热重TG和微分热重DTG曲线
Skillfully use SSH to get through the Internet restrictions