Codeforces Round 918 (Div. 4) F - Greetings 题解
摘要:二维偏序,数据结构
传送门:https://www.luogu.com.cn/problem/CF1915F
题意
数轴上有
如果某个时刻两个人在同一个位置,他们就会打招呼(注意,到了终点也可以和别人打招呼)。求最终打招呼发生了几次。
分析思路
如果一个人
简而言之,我们就是要求这样的点对
由于需要动态插入,动态统计比一个数小的数的个数,可以考虑树状数组。由于位置的值域太大,考虑使用离散化。注意离散化之后可能有
代码
#include <bits/stdc++.h>
#define lowbit(x) (x & (-x))
using namespace std;
const int N = 200010;
int t, n, a[N], b[N], c[2 * N]; // c 开 2 * n !!!
template <class _Tp>
struct Mess {
vector<_Tp> v;
inline void insert(_Tp x) { v.push_back(x); }
inline void init(void) {
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
}
inline int query(_Tp x) {
return lower_bound(v.begin(), v.end(), x) - v.begin() + 1;
}
}; // 离散化模板
struct Node {
int a, b;
inline bool operator<(const Node &x) const {
return a < x.a;
}
} d[N];
inline void add(int x) {
for (; x < 2 * N; x += lowbit(x)) c[x]++;
}
inline int query(int x) {
int ans = 0;
for (; x; x ^= lowbit(x)) ans += c[x];
return ans;
}
inline void solve(void) {
cin >> n;
Mess<int> M;
for (int i = 1; i <= n; i++) {
cin >> a[i] >> b[i], M.insert(a[i]), M.insert(b[i]);
}
M.init(); // 离散化初始化,树状数组清空
for (size_t i = 1; i <= M.v.size(); i++) c[i] = 0;
for (int i = 1; i <= n; i++) {
d[i] = {M.query(a[i]), M.query(b[i])};
}
long long ans = 0;
sort(d + 1, d + 1 + n);
for (int i = n; i >= 1; i--) {
ans += query(d[i].b), add(d[i].b);
}
cout << ans << endl;
}
int main(int argc, char const *argv[]) {
cin >> t;
while (t--) solve();
return 0;
}