当前位置:网站首页>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)
- De4000h storage installation configuration
- P3008 [USACO11JAN]Roads and Planes G (SPFA + SLF优化)
- Pattern matching and regular expressions in PostgreSQL - Das
- Use of UIC in QT
- MySQL -- convert rownum in Oracle to MySQL
- OpenFOAM:lduMatrix&lduAddressing
- 题解:《你的飞碟在这儿》、《哥德巴赫猜想》
- Chaos engineering platform chaosblade box new heavy release
- Three talking about exception -- error handling
猜你喜欢

Qt入门-制作一个简易的计算器

Memory management 01 - link script

Code implementation MNLM

When tidb meets Flink: tidb efficiently enters the lake "new play" | tilaker team interview

Solve "sub number integer", "jump happily", "turn on the light"

De4000h storage installation configuration

Bridge of undirected graph

What are eNB, EPC and PGW?

qt中uic的使用
![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]
随机推荐
OpenFOAM:lduMatrix&lduAddressing
JS reverse massive creative signature
【文档树、设置】字体变小
(POJ - 1308)Is It A Tree? (tree)
2022零代码/低代码开发白皮书【伙伴云出品】附下载
When tidb meets Flink: tidb efficiently enters the lake "new play" | tilaker team interview
QT new project_ MyNotepad++
Astro learning notes
Android kotlin broadcast technology point
[technology development-22]: rapid overview of the application and development of network and communication technology-2-communication Technology
[技术发展-22]:网络与通信技术的应用与发展快速概览-2- 通信技术
Stone merging Board [interval DP] (ordinary stone Merging & Ring Stone merging)
Gee learning notes 2
D how to check null
ensp简单入门
Code implementation MNLM
Operation tutorial: how does easydss convert MP4 on demand files into RTSP video streams?
P3807 [template] Lucas theorem /lucas theorem
Origin绘制热重TG和微分热重DTG曲线
Characteristics of selenium