1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| #include <cstdio> #include <iostream> #include <algorithm>
using namespace std;
typedef long long ll; const int maxn = 1e5 + 10; const int p = 1e9 + 7;
int n; ll ans(0); struct node{int v, x, p;}c[maxn];
inline bool cmpx(node x, node y) { return x.x < y.x; }
inline bool cmpv(node x, node y) { return x.v > y.v; }
#define lowbit(x) (x & -x) int tree[maxn], cnt[maxn]; inline void add(int x, int v, int p) { while (x <= n) { tree[x] += v; cnt[x] += p; x += lowbit(x); } }
inline pair<int, int> query(int x) { int ans(0), cur(0); while (x) { ans += tree[x]; cur += cnt[x]; x -= lowbit(x); } return make_pair(ans, cur); }
#define sum first #define num second
pair <int, int> operator - (pair <int, int> a, pair<int, int> b) { return make_pair (a.sum - b.sum, a.num - b.num); }
inline pair<int, int> query(int l, int r) { return query(r) - query(l); }
int main() { scanf ("%d", &n); for (int i = 1; i <= n; ++i) scanf ("%d %d", &c[i].v, &c[i].x); sort (c + 1, c + n + 1, cmpx); for (int i = 1; i <= n; ++i) add(i, c[i].x, 1), c[i].p = i; sort (c + 1, c + n + 1, cmpv); for (int i = 1; i <= n; ++i) { pair <int, int> r = query(c[i].p, n), l = query(c[i].p - 1); ans += 1ll * c[i].v * (r.sum - r.num * c[i].x + l.num * c[i].x - l.sum); add(c[i].p, -c[i].x, -1); } printf ("%lld\n", ans); }
|