当前位置:网站首页>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;
}
边栏推荐
- Origin绘制热重TG和微分热重DTG曲线
- P3008 [usaco11jan]roads and planes g (SPFA + SLF optimization)
- SystemServer进程
- [technology development-22]: rapid overview of the application and development of network and communication technology-2-communication Technology
- 2022 zero code / low code development white paper [produced by partner cloud] with download
- What are eNB, EPC and PGW?
- (POJ - 1984) navigation nightare (weighted and search set)
- Chinese name extraction (toy code - accurate head is too small, right to play)
- 基于ssm+jsp框架实现的学生选课信息管理系统【源码+数据库】
- MySQL -- convert rownum in Oracle to MySQL
猜你喜欢

OpenFOAM:lduMatrix&lduAddressing

What are eNB, EPC and PGW?

Pointer from entry to advanced (1)

Can automatically update the universal weekly report template, you can use it with your hand!

能自动更新的万能周报模板,有手就会用!

Use bloc to build a page instance of shutter

Téléchargement par navigateur

Getting started with QT - making a simple calculator

Runhe hi3516 development board openharmony small system and standard system burning

2022 Heilongjiang provincial examination on the writing skills of Application Essays
随机推荐
Pointer from entry to advanced (1)
D how to check null
P3008 [usaco11jan]roads and planes g (SPFA + SLF optimization)
Three talking about exception -- error handling
为什么switch 的default后面要跟break?
693. 行程排序(map + 拓扑)
BeanUtils--浅拷贝--实例/原理
2022 zero code / low code development white paper [produced by partner cloud] with download
(POJ - 1308)Is It A Tree? (tree)
When tidb meets Flink: tidb efficiently enters the lake "new play" | tilaker team interview
[technology development-22]: rapid overview of the application and development of network and communication technology-2-communication Technology
Everyone believes that the one-stop credit platform makes the credit scenario "useful"
Countermeasures for the failure of MMPV billing period caused by negative inventory of materials in SAP mm
What are eNB, EPC and PGW?
Performance optimization of memory function
量子三体问题: Landau Fall
SystemServer进程
D如何检查null
Téléchargement par navigateur
Unity small map production [2]