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
| #include <bits/stdc++.h> #define MAX_N 200000 #define lb lower_bound using namespace std; typedef long long lnt; int n, mp[2][MAX_N+5]; lnt ans; struct node {int x, y;} p[MAX_N+5], q[MAX_N+5], sta[2][MAX_N+5]; bool cmp(const node &a, const node &b) {return a.x < b.x;} void solve(int l, int r) { if (l == r) return; int mid = l+r>>1; int m0 = l, m1 = mid+1, tp0 = 0, tp1 = 0; for (int i = l; i <= r; i++) if (p[i].y <= mid) q[m0++] = p[i]; else q[m1++] = p[i]; memcpy(p+l, q+l, sizeof(node)*(r-l+1)); for (int p0 = l, p1 = mid+1; p1 <= r; ) { while (p0 <= mid && p[p0].x < p[p1].x) { while (tp0 && sta[0][tp0].y < p[p0].y) tp0--; sta[0][++tp0] = p[p0], p0++; } while (tp1 && sta[1][tp1].y > p[p1].y) tp1--; sta[1][++tp1] = p[p1], p1++; ans += tp0-(lb(sta[0]+1, sta[0]+tp0+1, sta[1][tp1-1], cmp)-sta[0])+1; } solve(l, mid), solve(mid+1, r); } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d%d", &p[i].x, &p[i].y), mp[0][i-1] = p[i].x, mp[1][i-1] = p[i].y; sort(mp[0], mp[0]+n), sort(mp[1], mp[1]+n); int m0 = unique(mp[0], mp[0]+n)-mp[0], m1 = unique(mp[1], mp[1]+n)-mp[1]; for (int i = 1; i <= n; i++) p[i].x = lb(mp[0], mp[0]+m0, p[i].x)-mp[0]+1, p[i].y = lb(mp[1], mp[1]+m1, p[i].y)-mp[1]+1; sort(p+1, p+n+1, cmp), solve(1, n); return printf("%lld", ans), 0; }
|