当前位置:网站首页>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;
}
边栏推荐
- Subcontracting configuration of uniapp applet subpackages
- 石子合并板子【区间DP】(普通石子合并 & 环形石子合并)
- Mysql5.7 installation super easy tutorial
- Qt-制作一个简单的计算器-实现四则运算-将结果以对话框的形式弹出来
- What are eNB, EPC and PGW?
- Partner cloud form strong upgrade! Pro version, more extraordinary!
- 自定义事件,全局事件总线,消息订阅与发布,$nextTick
- MySQL45讲——学习极客时间MySQL实战45讲笔记—— 04 | 深入浅出索引(上)
- Android kotlin fragment technology point
- Verification failed, please check your call back website. You can follow the instructions
猜你喜欢

Design of non main lamp: how to make intelligent lighting more "intelligent"?

无主灯设计:如何让智能照明更加「智能」?

Simple introduction to ENSP

What are eNB, EPC and PGW?

自定义事件,全局事件总线,消息订阅与发布,$nextTick

Qt-制作一个简单的计算器-实现四则运算

SystemServer进程
![[indomitable medal activity] life goes on and writing goes on](/img/c1/54e3f1b37db25af3f1998b39da301b.png)
[indomitable medal activity] life goes on and writing goes on
![2022 zero code / low code development white paper [produced by partner cloud] with download](/img/46/92c51090e0c476df3bcffd2d11fb6d.png)
2022 zero code / low code development white paper [produced by partner cloud] with download

Just 1000 fans, record it
随机推荐
默认插槽,具名插槽,作用域插槽
BeanUtils--浅拷贝--实例/原理
[indomitable medal activity] life goes on and writing goes on
刚好1000粉丝,记录一下
MySQL45讲——学习极客时间MySQL实战45讲笔记—— 04 | 深入浅出索引(上)
MySQL45讲——学习极客时间MySQL实战45讲笔记—— 05 | 深入浅出索引(下)
Slashgear shares 2021 life changing technology products, which are somewhat unexpected
P1908 逆序对
全屋Wi-Fi:一个谁也解决不好的痛点?
The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner
错误:EACCES:权限被拒绝,访问“/usr/lib/node_modules”
How to explain binary search to my sister? This is really difficult, fan!
科技的成就(二十七)
D如何检查null
Add sequence number column to query results in MySQL
Sum of the first n terms of Fibonacci (fast power of matrix)
Characteristics of selenium
Origin绘制热重TG和微分热重DTG曲线
Development skills of rxjs observable custom operator
Chaos engineering platform chaosblade box new heavy release