当前位置:网站首页>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;
}
边栏推荐
- Origin绘制热重TG和微分热重DTG曲线
- D language, possible 'string plug-ins'
- Use bloc to build a page instance of shutter
- Pocket Raider comments
- 故事点 vs. 人天
- I did it with two lines of code. As a result, my sister had a more ingenious way
- Story points vs. human days
- ArrayList and LinkedList
- Stone merging Board [interval DP] (ordinary stone Merging & Ring Stone merging)
- rxjs Observable 自定义 Operator 的开发技巧
猜你喜欢

Unity skframework framework (XII), score scoring module

不会看器件手册的工程师不是个好厨子

代码实现MNLM

Node. JS accessing PostgreSQL database through ODBC

为什么switch 的default后面要跟break?

Codeforces Round #803 (Div. 2)(A~D)

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

题解:《你的飞碟在这儿》、《哥德巴赫猜想》

QT new project_ MyNotepad++

Performance optimization of memory function
随机推荐
Use of UIC in QT
Explanation of 34 common terms on the Internet
Unity small map production [2]
[Blue Bridge Cup] children's worship circle
P1042 [NOIP2003 普及组] 乒乓球
Origin绘制热重TG和微分热重DTG曲线
基于ssm+jsp框架实现的学生选课信息管理系统【源码+数据库】
MySQL45讲——学习极客时间MySQL实战45讲笔记—— 04 | 深入浅出索引(上)
selenium 元素定位方法
On flow delivery between microservices
Qt-制作一个简单的计算器-实现四则运算
Characteristics of selenium
Gee learning notes 2
Detailed collection of common MySQL commands
[template] longest common subsequence ([DP or greedy] board)
Can automatically update the universal weekly report template, you can use it with your hand!
无主灯设计:如何让智能照明更加「智能」?
[indomitable medal activity] life goes on and writing goes on
selenium 在pycharm中安装selenium
Unity skframework framework (XII), score scoring module