当前位置:网站首页>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;
}
边栏推荐
- Téléchargement par navigateur
- The global special paper revenue in 2021 was about $27 million, and it is expected to reach $35 million in 2028. From 2022 to 2028, the CAGR was 3.8%
- Origin绘制热重TG和微分热重DTG曲线
- [document tree, setting] font becomes smaller
- [indomitable medal activity] life goes on and writing goes on
- Browser driven Download
- P1042 [NOIP2003 普及组] 乒乓球
- Verification failed, please check your call back website. You can follow the instructions
- Stone merging Board [interval DP] (ordinary stone Merging & Ring Stone merging)
- D how to check null
猜你喜欢

混沌工程平台 ChaosBlade-Box 新版重磅发布

错误:EACCES:权限被拒绝,访问“/usr/lib/node_modules”

c# 水晶报表打印
【文档树、设置】字体变小
![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]

The 29 year old programmer in Shanghai was sentenced to 10 months for "deleting the database and running away" on the day of his resignation!

Mysql5.7 installation super easy tutorial
![Unity small map production [2]](/img/d6/9d6556d37525b9986b74133f2a7aaa.jpg)
Unity small map production [2]

(POJ - 1984) navigation nightare (weighted and search set)

Origin plots thermogravimetric TG and differential thermogravimetric DTG curves
随机推荐
MySQL45讲——学习极客时间MySQL实战45讲笔记—— 05 | 深入浅出索引(下)
Integral link, inertia link and proportion link in Simulink
[indomitable medal activity] life goes on and writing goes on
QT new project_ MyNotepad++
Development skills of rxjs observable custom operator
[USACO05JAN]Watchcow S(欧拉回路)
Chaos engineering platform chaosblade box new heavy release
Use bloc to build a page instance of shutter
Add sequence number column to query results in MySQL
Dingtalk send message
The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner
【模板】最长公共子序列 (【DP or 贪心】板子)
[template] longest common subsequence ([DP or greedy] board)
BeanUtils--浅拷贝--实例/原理
D为何链接不了dll
Code implementation MNLM
P1347 排序(拓扑 + spfa判断环 or 拓扑[内判断环])
Just 1000 fans, record it
[unity] using GB2312, the solution to abnormal program after packaging
P1908 逆序对