当前位置:网站首页>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;
}
边栏推荐
- Student course selection information management system based on ssm+jsp framework [source code + database]
- Will your sleep service dream of the extra bookinfo on the service network
- Android kotlin material design technology points
- Everyone believes that the one-stop credit platform makes the credit scenario "useful"
- [USACO05JAN]Watchcow S(欧拉回路)
- Pointer from entry to advanced (1)
- [cloud native database] what to do when encountering slow SQL (Part 1)?
- Sum of the first n terms of Fibonacci (fast power of matrix)
- 故事点 vs. 人天
- P1347 sorting (topology + SPFA judgment ring or topology [inner judgment ring])
猜你喜欢
selenium 在pycharm中安装selenium
Solve "sub number integer", "jump happily", "turn on the light"
selenium 元素定位方法
使用BLoC 构建 Flutter的页面实例
Use bloc to build a page instance of shutter
BeanUtils--浅拷贝--实例/原理
Runhe hi3516 development board openharmony small system and standard system burning
Halcon extract orange (Orange)
QT - make a simple calculator - realize four operations
Qt-制作一个简单的计算器-实现四则运算
随机推荐
Dingtalk send message
Verification failed, please check your call back website. You can follow the instructions
Multi rotor aircraft control using PID and LQR controllers
Let juicefs help you with "remote backup"
大家信夫一站式信用平台让信用场景“用起来
mysql ---- Oracle中的rownum转换成MySQL
Getting started with QT - making a simple calculator
selenium 元素定位方法
Pattern matching and regular expressions in PostgreSQL - Das
刚好1000粉丝,记录一下
P3807 [template] Lucas theorem /lucas theorem
Pointer from entry to advanced (1)
Qt-制作一个简单的计算器-实现四则运算-将结果以对话框的形式弹出来
无主灯设计:如何让智能照明更加「智能」?
Simple introduction to ENSP
[usaco05jan]watchcow s (Euler loop)
Explanation: here is your UFO, Goldbach conjecture
Can automatically update the universal weekly report template, you can use it with your hand!
qt中uic的使用
Slashgear shares 2021 life changing technology products, which are somewhat unexpected