当前位置:网站首页>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;
}
边栏推荐
- OpenFOAM:lduMatrix&lduAddressing
- Runhe hi3516 development board openharmony small system and standard system burning
- We sincerely invite young creators to share with investors and entrepreneurs how to make choices in life in the metauniverse
- 2022 Heilongjiang provincial examination on the writing skills of Application Essays
- Will your sleep service dream of the extra bookinfo on the service network
- MySQL -- convert rownum in Oracle to MySQL
- Memory management 01 - link script
- Three talking about exception -- error handling
- Pattern matching and regular expressions in PostgreSQL - Das
- 使用BLoC 构建 Flutter的页面实例
猜你喜欢
[indomitable medal activity] life goes on and writing goes on
Partner cloud form strong upgrade! Pro version, more extraordinary!
浏览器驱动的下载
OpenAPI generator: simplify the restful API development process
Chinese name extraction (toy code - accurate head is too small, right to play)
[document tree, setting] font becomes smaller
rxjs Observable 自定义 Operator 的开发技巧
Selenium installing selenium in pycharm
Tupang multi-target tracking! BOT sort: robust correlated multi pedestrian tracking
Halcon extract orange (Orange)
随机推荐
P3008 [USACO11JAN]Roads and Planes G (SPFA + SLF优化)
Why can't d link DLL
Node.js通过ODBC访问PostgreSQL数据库
selenium的特点
Essential for operation and maintenance - Elk log analysis system
Operation tutorial: how does easydss convert MP4 on demand files into RTSP video streams?
[cloud native database] what to do when encountering slow SQL (Part 1)?
selenium 在pycharm中安装selenium
2022 zero code / low code development white paper [produced by partner cloud] with download
Pocket Raider comments
[usaco05jan]watchcow s (Euler loop)
The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner
Origin绘制热重TG和微分热重DTG曲线
JS reverse row query data decryption
Engineers who can't read device manuals are not good cooks
Runhe hi3516 development board openharmony small system and standard system burning
Qt-制作一个简单的计算器-实现四则运算-将结果以对话框的形式弹出来
Use bloc to build a page instance of shutter
[Blue Bridge Cup] children's worship circle
为什么switch 的default后面要跟break?