当前位置:网站首页>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;
}
边栏推荐
- mysql ---- Oracle中的rownum转换成MySQL
- QT - make a simple calculator - realize four operations
- Verification failed, please check your call back website. You can follow the instructions
- Word frequency statistics & sorting
- 全屋Wi-Fi:一个谁也解决不好的痛点?
- MySQL -- convert rownum in Oracle to MySQL
- Student course selection information management system based on ssm+jsp framework [source code + database]
- 无主灯设计:如何让智能照明更加「智能」?
- Slashgear shares 2021 life changing technology products, which are somewhat unexpected
- How much do you know about free SSL certificates? The difference between free SSL certificate and charged SSL certificate
猜你喜欢

Halcon extract orange (Orange)

I did it with two lines of code. As a result, my sister had a more ingenious way

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

瀏覽器驅動的下載

OpenFOAM:lduMatrix&lduAddressing

qt中uic的使用

Runhe hi3516 development board openharmony small system and standard system burning

Qt入门-制作一个简易的计算器

Origin plots thermogravimetric TG and differential thermogravimetric DTG curves

MySQL45讲——学习极客时间MySQL实战45讲笔记—— 05 | 深入浅出索引(下)
随机推荐
2022 zero code / low code development white paper [produced by partner cloud] with download
selenium 在pycharm中安装selenium
QT - make a simple calculator - realize four operations
(POJ - 1984) navigation nightare (weighted and search set)
Solution: Compression Technology (original version and sequel version)
Qt如何设置固定大小
Performance optimization of memory function
瀏覽器驅動的下載
Chaos engineering platform chaosblade box new heavy release
Qt新项目_MyNotepad++
Don't spend money, spend an hour to build your own blog website
Story points vs. human days
Code implementation MNLM
Clean up system cache and free memory under Linux
全屋Wi-Fi:一个谁也解决不好的痛点?
题解:《你的飞碟在这儿》、《哥德巴赫猜想》
Android kotlin broadcast technology point
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!
Qt入门-制作一个简易的计算器
使用BLoC 构建 Flutter的页面实例