当前位置:网站首页>P1908 reverse sequence pair
P1908 reverse sequence pair
2022-07-02 13:56:00 【Fu Fushou grass】
Tree array + discretization
The technique of discretization is to weight the order of input ( The value of the input ) Sort
Such as [5,4,2,6,3,1] The sequence obtained after sorting is [6,3,5,2,1,4]
The number of reverse pairs of the two is equal .
We can understand that : Choose any number in this ordered sequence , For this number , All the numbers in front of it are smaller than it , If the subscript of the previous number in the original array is greater than it , Then it forms a reverse order pair .
#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;
}
边栏推荐
- Winter vacation daily question - lucky numbers in the matrix
- 2022 zero code / low code development white paper [produced by partner cloud] with download
- BeanUtils -- shallow copy -- example / principle
- How to use SAP's metadata framework (MDF) to build custom business rules?
- P3008 [usaco11jan]roads and planes g (SPFA + SLF optimization)
- [usaco05jan]watchcow s (Euler loop)
- In 2021, the global revenue of structural bolts was about $796.4 million, and it is expected to reach $1097.6 million in 2028
- Use bloc to build a page instance of shutter
- P1347 排序(拓扑 + spfa判断环 or 拓扑[内判断环])
- QT how to set fixed size
猜你喜欢
Common options of tcpdump command: Three
Origin绘制热重TG和微分热重DTG曲线
Code implementation MNLM
selenium,元素操作以及浏览器操作方法
How to explain binary search to my sister? This is really difficult, fan!
瀏覽器驅動的下載
Unity skframework framework (XII), score scoring module
Just 1000 fans, record it
Use bloc to build a page instance of shutter
(POJ - 1984) navigation nightare (weighted and search set)
随机推荐
Chinese name extraction (toy code - accurate head is too small, right to play)
[cloud native database] what to do when encountering slow SQL (Part 1)?
Error function ERF
D language, possible 'string plug-ins'
Whole house Wi Fi: a pain point that no one can solve?
Origin plots thermogravimetric TG and differential thermogravimetric DTG curves
SystemServer进程
Characteristics of selenium
浏览器驱动的下载
Node.js通过ODBC访问PostgreSQL数据库
Explanation: here is your UFO, Goldbach conjecture
[technology development-22]: rapid overview of the application and development of network and communication technology-2-communication Technology
Story points vs. human days
Multi rotor aircraft control using PID and LQR controllers
(POJ - 1308)Is It A Tree? (tree)
Simple introduction to ENSP
In 2021, the global styrene butadiene styrene (SBS) revenue was about $3722.7 million, and it is expected to reach $5679.6 million in 2028
Student course selection information management system based on ssm+jsp framework [source code + database]
Origin绘制热重TG和微分热重DTG曲线
Drawing Nyquist diagram with MATLAB