当前位置:网站首页>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;
}
边栏推荐
- [OpenGL] notes 29. Advanced lighting (specular highlights)
- [Unity]使用GB2312,打包后程序不正常解决方案
- leetcode621. task scheduler
- 2022 zero code / low code development white paper [produced by partner cloud] with download
- Download files and preview pictures
- [技术发展-22]:网络与通信技术的应用与发展快速概览-2- 通信技术
- 错误:EACCES:权限被拒绝,访问“/usr/lib/node_modules”
- D language, possible 'string plug-ins'
- [cloud native database] what to do when encountering slow SQL (Part 1)?
- P3807 [template] Lucas theorem /lucas theorem
猜你喜欢

selenium 在pycharm中安装selenium

Research shows that "congenial" is more likely to become friends

rxjs Observable 自定义 Operator 的开发技巧
![Student course selection information management system based on ssm+jsp framework [source code + database]](/img/71/900d83dba41974589b15d23e632119.png)
Student course selection information management system based on ssm+jsp framework [source code + database]

Qt-制作一个简单的计算器-实现四则运算

selenium,元素操作以及浏览器操作方法

Téléchargement par navigateur

Pointer from entry to advanced (1)

Selenium installing selenium in pycharm

OpenFOAM:lduMatrix&lduAddressing
随机推荐
The xftp connection Haikang camera reported an error: the SFTP subsystem application has been rejected. Please ensure that the SFTP subsystem settings of the SSH connection are valid
Pattern matching and regular expressions in PostgreSQL - Das
为什么switch 的default后面要跟break?
大家信夫一站式信用平台让信用场景“用起来
selenium的特点
uniapp小程序 subPackages分包配置
刚好1000粉丝,记录一下
[技术发展-22]:网络与通信技术的应用与发展快速概览-2- 通信技术
[cloud native database] what to do when encountering slow SQL (Part 1)?
ensp简单入门
Don't spend money, spend an hour to build your own blog website
Add sequence number column to query results in MySQL
Will your sleep service dream of the extra bookinfo on the service network
Memory management 01 - link script
Clean up system cache and free memory under Linux
Dingtalk 发送消息
The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner
Solve "sub number integer", "jump happily", "turn on the light"
免费SSL证书知多少?免费SSL证书和收费SSL证书的区别
P3008 [USACO11JAN]Roads and Planes G (SPFA + SLF优化)