原来GCC编译器还有这么恐怖的功能!!!
2026-06-26 09:07:13
发布于:香港
6阅读
0回复
0点赞
#include <iostream>
// 引入 PBDS 库
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
// 定义一个允许重复元素的红黑树,并支持统计功能
// 注意:为了处理重复数字,我们将存入 pair<值, 索引> 来保证元素的唯一性
typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
int main() {
int N;
if (!(cin >> N)) return 0;
ordered_set people;
long long ans = 0;
for (int i = 0; i < N; i++) {
int x;
cin >> x;
// order_of_key({x, -1}) 会返回树中严格小于 {x, -1} 的元素个数
// 这完美等同于你之前的 lower_bound 逻辑
ans += people.order_of_key({x, -1});
// 插入元素,带上索引 i 防止重复元素被覆盖
people.insert({x, i});
}
cout << ans << "\n";
return 0;
}
原来GCC编译器还有这么恐怖的功能!!!
这里空空如也







有帮助,赞一个