Problem:
The Cross Covers Everything
A cross-shaped infinite area on the $x$-$y$ plane can be specified by two distinct points as depicted
on the figure below.
Figure J.1. The cross area specified by two points numbered 2 and 4
Given a set of points on the plane, you are asked to figure out how many pairs of the points
    form a cross-shaped area that covers all the points. To be more precise, when $n$ points with
    coordinates ($x_i$, $y_i$) ($i = 1, \cdot , n$) are given, the ordered pair $\langle p, q \rangle$ is said to cover a point ($x, y$)
    if $x_p \leq x \leq x_q$, $y_p \leq y \leq y_q$, or both hold. Your task is to find how many pairs $\langle p, q \rangle$ cover all
    the $n$ points. No two given points have the same $x$-coordinate nor the same $y$-coordinate.
Input
The input consists of a single test case of the following format.
$n$
$x_1$ $y_1$
$\vdots$
$x_n$ $y_n$
The first line contains an integer $n$ ($2 \leq n \leq 2 \times 10^5$), which is the number of points given. Two
    integers $x_i$ and $y_i$ in the $i$-th line of the following $n$ lines are the coordinates of the $i$-th point
    ($1 \leq x_i \leq 10^6, 1 \leq y_i \leq 10^6$). You may assume that $x_j \neq x_k$ and $y_j \neq y_k$ hold for all $j \neq k$.
Output
Print in a line the number of ordered pairs of points that satisfy the condition.
Sample Input 1
4
2 1
1 2
6 3
5 4
Sample Output 1
4
Sample Input 2
20
15 9
14 13
2 7
10 5
11 17
13 8
9 3
8 12
6 4
19 18
12 1
3 2
5 10
18 11
4 19
20 16
16 15
1 14
7 6
17 20
Sample Output 2
9
Figure J.1 depicts the cross specified by two points numbered 2 and 4, that are the second and
    the fourth points of the Sample Input 1. This is one of the crosses covering all the points.

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PLL = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < n; ++i)
constexpr ll INF = 9009009009009009009LL;

int main() {
    ll n; cin >> n;
    vector<PLL> pos(n);
    rep(i, n) {
        cin >> pos[i].first >> pos[i].second;
    }

    sort(pos.begin(), pos.end());
    ll ans = 0;

    vector<ll> rightMax(n + 1, -INF);
    vector<ll> rightMin(n + 1, INF);
    for (ll i = n - 1; i >= 0; --i) {
        rightMax[i] = max(rightMax[i + 1], pos[i].second);
        rightMin[i] = min(rightMin[i + 1], pos[i].second);
    }
    vector<ll> canRightV(n + 1, 0);
    {
        ll nowmx = -INF;
        for (ll i = n - 1; i >= 0; --i) {
            canRightV[i] = canRightV[i + 1];
            if (nowmx < pos[i].second) {
                canRightV[i]++;
                nowmx = pos[i].second;
            }
        }
    }

    ll nowmn = INF;
    ll nowmx = -INF;
    rep(i, n) {
        nowmx = max(nowmx, pos[i].second);
        if (pos[i].second > nowmn) continue;
        nowmn = pos[i].second;

        // 右からのnowmnを下回らない最大の左
        ll mnL = 0, mnR = n;
        if (rightMin[mnL] >= nowmn) {
            mnR = 0;
        }
        else {
            while (mnR - mnL > 1) {
                ll mid = (mnL + mnR) / 2;
                if (rightMin[mid] >= nowmn) mnR = mid;
                else mnL = mid;
            }
        }

        ll canL = max(mnR, i + 1);

        // 今の点から、nowmxを上回る最大の右
        ll mxL = i, mxR = n;
        if (rightMax[mxL] < nowmx) {
            // Do nothing
        }
        else {
            while (mxR - mxL > 1) {
                ll mid = (mxL + mxR) / 2;
                if (rightMax[mid] < nowmx) mxR = mid;
                else mxL = mid;
            }
        }

        ll canR = mxL;

        if (canR >= canL) {
            ans += canRightV[canL] - canRightV[canR + 1];
        } 
    }

    cout << ans << endl;
}
accuracy: 1
time_ms: 60
memory_kb: 11140
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PLL = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < n; ++i)
constexpr ll INF = 9009009009009009009LL;

int main() {
    ll n; cin >> n;
    vector<PLL> pos(n);
    rep(i, n) {
        cin >> pos[i].first >> pos[i].second;
    }

    sort(pos.begin(), pos.end());
    ll ans = 0;

    rep(_, 2) {
        vector<ll> rightMax(n + 1, -INF);
        vector<ll> rightMin(n + 1, INF);
        for (ll i = n - 1; i >= 0; --i) {
            rightMax[i] = max(rightMax[i + 1], pos[i].second);
            rightMin[i] = min(rightMin[i + 1], pos[i].second);
        }
        vector<ll> canRightV(n + 1, 0);
        {
            ll nowmx = -INF;
            for (ll i = n - 1; i >= 0; --i) {
                canRightV[i] = canRightV[i + 1];
                if (nowmx < pos[i].second) {
                    canRightV[i]++;
                    nowmx = pos[i].second;
                }
            }
        }

        ll nowmn = INF;
        ll nowmx = -INF;
        rep(i, n) {
            nowmx = max(nowmx, pos[i].second);
            if (pos[i].second > nowmn) continue;
            nowmn = pos[i].second;

            // 右からのnowmnを下回らない最大の左
            ll mnL = 0, mnR = n;
            if (rightMin[mnL] >= nowmn) {
                mnR = 0;
            }
            else {
                while (mnR - mnL > 1) {
                    ll mid = (mnL + mnR) / 2;
                    if (rightMin[mid] >= nowmn) mnR = mid;
                    else mnL = mid;
                }
            }

            ll canL = max(mnR, i + 1);

            // 今の点から、nowmxを上回る最大の右
            ll mxL = i, mxR = n;
            if (rightMax[mxL] < nowmx) {
                // Do nothing
            }
            else {
                while (mxR - mxL > 1) {
                    ll mid = (mxL + mxR) / 2;
                    if (rightMax[mid] < nowmx) mxR = mid;
                    else mxL = mid;
                }
            }

            ll canR = mxL;

            if (canR >= canL) {
                ans += canRightV[canL] - canRightV[canR + 1];
            } 
        }

        reverse(pos.begin(), pos.end());
    }

    cout << ans << endl;
}
accuracy: 0.09375
time_ms: 50
memory_kb: 11140
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;

bool chmax(auto &a, auto b) { return a < b ? a = b, true : false; }
bool chmin(auto &a, auto b) { return a > b ? a = b, true : false; }

struct xy {
    int x, y;
};
const int M = 1'000'000;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N; cin >> N;
    vector<xy> point(N);
    for(int i=0; i<N; ++i) cin >> point[i].x >> point[i].y;
    vector<int> L(M + 1, M), D(M + 1, M);
    sort(point.begin(), point.end(), [](xy a, xy b) { return a.x < b.x; });
    for(int i=N-1, x=M, y=M; i>=0; --i) {
        chmin(y, point[i].y);
        for(; i ? x > point[i-1].x : x >= 0; --x) D[x] = y;
    }
    sort(point.begin(), point.end(), [](xy a, xy b) { return a.y < b.y; });
    for(int i=N-1, y=M, x=M; i>=0; --i) {
        chmin(x, point[i].x);
        for(; i ? y > point[i-1].y : y >= 0; --y) L[y] = x;
    }

    vector<xy> P, Q;
    for(int i=0, l=1e9; i<N; ++i) if(chmin(l, point[i].x)) P.emplace_back(point[i]);
    for(int i=N-1, r=-1e9; i>=0; --i) if(chmax(r, point[i].x)) Q.emplace_back(point[i]);

    // for(auto i:P) cout << i.x << ' ' << i.y << '\n';
    // cout << '\n';
    // for(auto i:Q) cout << i.x << ' ' << i.y << '\n';
    // cout << '\n';

    ll ans = 0;
    for(int i=0; i<Q.size(); ++i) {
        // cout << Q[i].x << ' ' << Q[i].y << '\n';
        // cout << L[Q[i].y] << ' ' << D[Q[i].x] << '\n';
        int l = P.rend() - lower_bound(P.rbegin(), P.rend(), L[Q[i].y], [](xy &K, int v) {
            return v > K.x;
        });
        int r = lower_bound(P.begin(), P.end(), D[Q[i].x], [](xy &K, int v) {
            return v > K.y;
        }) - P.begin();
        ans += max(0, r - l);
        // cout << '\n';
    }
    cout << ans << '\n';
}
accuracy: 1
time_ms: 40
memory_kb: 17420
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using a2 = array<ll, 2>;
using a3 = array<ll, 3>;

bool chmin(auto& a, const auto& b) { return a > b ? a = b, 1 : 0; }
bool chmax(auto& a, const auto& b) { return a < b ? a = b, 1 : 0; }

const ll INF = 1e18;
ll mod = 998244353;

ifstream in;
ofstream out;

int main(int argc, char** argv) {
    ios::sync_with_stdio(false);
    cin.tie(0);

    if (argc > 2) {
        in.open(argv[1]);
        cin.rdbuf(in.rdbuf());
        out.open(argv[2]);
        cout.rdbuf(out.rdbuf());
    }

    ll n;
    cin >> n;
    vector<a2> yx(n);
    for (int i = 0; i < n; i++) {
        ll x, y;
        cin >> x >> y;
        yx[i] = {y, x};
    }
    sort(yx.begin(), yx.end());
    vector<a2> a, b;
    ll nowx = INF;
    for (auto& x : yx) {
        if (x[1] < nowx) {
            nowx = x[1];
            a.push_back(x);
        }
    }
    nowx = -INF;
    reverse(yx.begin(), yx.end());
    for (auto& x : yx) {
        if (x[1] > nowx) {
            nowx = x[1];
            b.push_back(x);
        }
    }
    reverse(b.begin(), b.end());

    vector<ll> mx_xy(1e6 + 5, 0), mx_yx(1e6 + 5, 0);
    for (int i = 0; i < n; i++) {
        mx_xy[yx[i][1]] = yx[i][0];
        mx_yx[yx[i][0]] = yx[i][1];
    }

    for (int i = 0; i < 1e6 + 3; i++) {
        chmax(mx_xy[i + 1], mx_xy[i]);
        chmax(mx_yx[i + 1], mx_yx[i]);
    }

    ll ans = 0;
    for (auto& [y, x] : a) {
        ll px = -1, qx = b.size();
        while (qx - px > 1) {
            ll mid = (px + qx) / 2;
            (mx_xy[x] < b[mid][0] ? qx : px) = mid;
        }
        ll py = -1, qy = b.size();
        while (qy - py > 1) {
            ll mid = (py + qy) / 2;
            (mx_yx[y] < b[mid][1] ? py : qy) = mid;
        }
        ans += max(0LL, qy - qx);
    }
    cout << ans << endl;

    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 28544
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pair<int,int>>
#define vll vector<pair<ll,ll>>
#define vvi vector<vector<int>>
#define vvl vector<vector<ll>>
#define vvii vector<vector<pair<int,int>>>
#define vvll vector<vector<pair<ll,ll>>>
#define vst vector<string>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define mkunique(x) sort(all(x));(x).erase(unique(all(x)),(x).end())
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=998244353,MAX=300005,INF=15<<26;

// BIT セグ木 遅延セグ木 のみ

// from: https://gist.github.com/yosupo06/ddd51afb727600fd95d9d8ad6c3c80c9
// (based on AtCoder STL)

#include <algorithm>
#include <array>
#ifdef _MSC_VER
#include <intrin.h>
#endif
namespace atcoder {
namespace internal {
int ceil_pow2(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n)) x++;
    return x;
}
int bsf(unsigned int n) {
#ifdef _MSC_VER
    unsigned long index;
    _BitScanForward(&index, n);
    return index;
#else
    return __builtin_ctz(n);
#endif
}
}  // namespace internal

}  // namespace atcoder

#include <cassert>
#include <numeric>
#include <type_traits>

namespace atcoder {

namespace internal {

#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
typename std::conditional<std::is_same<T, __int128_t>::value ||
std::is_same<T, __int128>::value,
std::true_type,
std::false_type>::type;

template <class T>
using is_unsigned_int128 =
typename std::conditional<std::is_same<T, __uint128_t>::value ||
std::is_same<T, unsigned __int128>::value,
std::true_type,
std::false_type>::type;

template <class T>
using make_unsigned_int128 =
typename std::conditional<std::is_same<T, __int128_t>::value,
__uint128_t,
unsigned __int128>;

template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
is_signed_int128<T>::value ||
is_unsigned_int128<T>::value,
std::true_type,
std::false_type>::type;

template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
                                                 std::is_signed<T>::value) ||
is_signed_int128<T>::value,
std::true_type,
std::false_type>::type;

template <class T>
using is_unsigned_int =
typename std::conditional<(is_integral<T>::value &&
                           std::is_unsigned<T>::value) ||
is_unsigned_int128<T>::value,
std::true_type,
std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<
is_signed_int128<T>::value,
make_unsigned_int128<T>,
typename std::conditional<std::is_signed<T>::value,
std::make_unsigned<T>,
std::common_type<T>>::type>::type;

#else

template <class T> using is_integral = typename std::is_integral<T>;

template <class T>
using is_signed_int =
typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
std::true_type,
std::false_type>::type;

template <class T>
using is_unsigned_int =
typename std::conditional<is_integral<T>::value &&
std::is_unsigned<T>::value,
std::true_type,
std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
std::make_unsigned<T>,
std::common_type<T>>::type;

#endif

template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;

template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;

template <class T> using to_unsigned_t = typename to_unsigned<T>::type;

}  // namespace internal

}  // namespace atcoder

#include <cassert>
#include <vector>

namespace atcoder {

template <class T> struct fenwick_tree {
    using U = internal::to_unsigned_t<T>;
    
public:
    fenwick_tree() : _n(0) {}
    fenwick_tree(int n) : _n(n), data(n) {}
    
    void add(int p, T x) {
        assert(0 <= p && p < _n);
        p++;
        while (p <= _n) {
            data[p - 1] += U(x);
            p += p & -p;
        }
    }
    
    T sum(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        return sum(r) - sum(l);
    }
    
private:
    int _n;
    std::vector<U> data;
    
    U sum(int r) {
        U s = 0;
        while (r > 0) {
            s += data[r - 1];
            r -= r & -r;
        }
        return s;
    }
};

}  // namespace atcoder


#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
namespace atcoder {

template <class S,
S (*op)(S, S),
S (*e)(),
class F,
S (*mapping)(F, S),
F (*composition)(F, F),
F (*id)()>
struct lazy_segtree {
public:
    lazy_segtree() : lazy_segtree(0) {}
    lazy_segtree(int n) : lazy_segtree(std::vector<S>(n, e())) {}
    lazy_segtree(const std::vector<S>& v) : _n(int(v.size())) {
        log = internal::ceil_pow2(_n);
        size = 1 << log;
        d = std::vector<S>(2 * size, e());
        lz = std::vector<F>(size, id());
        for (int i = 0; i < _n; i++) d[size + i] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }
    
    void set(int p, S x) {
        assert(0 <= p && p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }
    
    S get(int p) {
        assert(0 <= p && p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        return d[p];
    }
    
    S prod(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        if (l == r) return e();
        
        l += size;
        r += size;
        
        for (int i = log; i >= 1; i--) {
            if (((l >> i) << i) != l) push(l >> i);
            if (((r >> i) << i) != r) push(r >> i);
        }
        
        S sml = e(), smr = e();
        while (l < r) {
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        
        return op(sml, smr);
    }
    
    S all_prod() { return d[1]; }
    
    void apply(int p, F f) {
        assert(0 <= p && p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        d[p] = mapping(f, d[p]);
        for (int i = 1; i <= log; i++) update(p >> i);
    }
    void apply(int l, int r, F f) {
        assert(0 <= l && l <= r && r <= _n);
        if (l == r) return;
        
        l += size;
        r += size;
        
        for (int i = log; i >= 1; i--) {
            if (((l >> i) << i) != l) push(l >> i);
            if (((r >> i) << i) != r) push((r - 1) >> i);
        }
        
        {
            int l2 = l, r2 = r;
            while (l < r) {
                if (l & 1) all_apply(l++, f);
                if (r & 1) all_apply(--r, f);
                l >>= 1;
                r >>= 1;
            }
            l = l2;
            r = r2;
        }
        
        for (int i = 1; i <= log; i++) {
            if (((l >> i) << i) != l) update(l >> i);
            if (((r >> i) << i) != r) update((r - 1) >> i);
        }
    }
    
    template <bool (*g)(S)> int max_right(int l) {
        return max_right(l, [](S x) { return g(x); });
    }
    template <class G> int max_right(int l, G g) {
        assert(0 <= l && l <= _n);
        assert(g(e()));
        if (l == _n) return _n;
        l += size;
        for (int i = log; i >= 1; i--) push(l >> i);
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!g(op(sm, d[l]))) {
                while (l < size) {
                    push(l);
                    l = (2 * l);
                    if (g(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return _n;
    }
    
    template <bool (*g)(S)> int min_left(int r) {
        return min_left(r, [](S x) { return g(x); });
    }
    template <class G> int min_left(int r, G g) {
        assert(0 <= r && r <= _n);
        assert(g(e()));
        if (r == 0) return 0;
        r += size;
        for (int i = log; i >= 1; i--) push((r - 1) >> i);
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!g(op(d[r], sm))) {
                while (r < size) {
                    push(r);
                    r = (2 * r + 1);
                    if (g(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }
    
private:
    int _n, size, log;
    std::vector<S> d;
    std::vector<F> lz;
    
    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
    void all_apply(int k, F f) {
        d[k] = mapping(f, d[k]);
        if (k < size) lz[k] = composition(f, lz[k]);
    }
    void push(int k) {
        all_apply(2 * k, lz[k]);
        all_apply(2 * k + 1, lz[k]);
        lz[k] = id();
    }
};

}  // namespace atcoder

#include <algorithm>
#include <cassert>
#include <vector>

namespace atcoder {

template <class S, S (*op)(S, S), S (*e)()> struct segtree {
public:
    segtree() : segtree(0) {}
    segtree(int n) : segtree(std::vector<S>(n, e())) {}
    segtree(const std::vector<S>& v) : _n(int(v.size())) {
        log = internal::ceil_pow2(_n);
        size = 1 << log;
        d = std::vector<S>(2 * size, e());
        for (int i = 0; i < _n; i++) d[size + i] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }
    
    void set(int p, S x) {
        assert(0 <= p && p < _n);
        p += size;
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }
    
    S get(int p) {
        assert(0 <= p && p < _n);
        return d[p + size];
    }
    
    S prod(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        S sml = e(), smr = e();
        l += size;
        r += size;
        
        while (l < r) {
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return op(sml, smr);
    }
    
    S all_prod() { return d[1]; }
    
    template <bool (*f)(S)> int max_right(int l) {
        return max_right(l, [](S x) { return f(x); });
    }
    template <class F> int max_right(int l, F f) {
        assert(0 <= l && l <= _n);
        assert(f(e()));
        if (l == _n) return _n;
        l += size;
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!f(op(sm, d[l]))) {
                while (l < size) {
                    l = (2 * l);
                    if (f(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return _n;
    }
    
    template <bool (*f)(S)> int min_left(int r) {
        return min_left(r, [](S x) { return f(x); });
    }
    template <class F> int min_left(int r, F f) {
        assert(0 <= r && r <= _n);
        assert(f(e()));
        if (r == 0) return 0;
        r += size;
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!f(op(d[r], sm))) {
                while (r < size) {
                    r = (2 * r + 1);
                    if (f(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }
    
private:
    int _n, size, log;
    std::vector<S> d;
    
    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
};

}  // namespace atcoder


int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    ll N;cin>>N;
    vl usex,usey;
    vll S(N);
    for(int i=0;i<N;i++){
        ll a,b;cin>>a>>b;
        usex.pb(a);
        usey.pb(b);
        S[i]=mp(a,b);
    }
    mkunique(usex);
    mkunique(usey);
    for(int i=0;i<N;i++){
        S[i].fi=lower_bound(all(usex),S[i].fi)-usex.begin();
        S[i].se=lower_bound(all(usey),S[i].se)-usey.begin();
    }
    sort(all(S));
    vi A,B;
    for(int i=0;i<N;i++){
        if(si(A)==0||S[A.back()].se>S[i].se){
            A.pb(i);
        }
    }
    for(int i=N-1;i>=0;i--){
        if(si(B)==0||S[B.back()].se<S[i].se){
            B.pb(i);
        }
    }
    atcoder::fenwick_tree<ll> BI(N);
    
    vi L(N),R(N);
    for(int i=0;i<N;i++){
        L[i]=S[i].se;
        if(i) chmax(L[i],L[i-1]);
    }
    for(int i=N-1;i>=0;i--){
        R[i]=S[i].se;
        if(i+1<N) chmin(R[i],R[i+1]);
    }
    
    vi ad;
    
    int j=0;
    ll ans=0;
    for(int i=si(A)-1;i>=0;i--){
        while(j<si(B)&&A[i]<B[j]&&A.back()<B[j]){
            BI.add(S[B[j]].se,1);
            ad.pb(B[j]);
            j++;
        }
        while(si(ad)&&R[ad.back()]<S[A[i]].se){
            BI.add(S[ad.back()].se,-1);
            ad.pop_back();
        }
        ans+=BI.sum(L[A[i]]+1,N);
        
        //cout<<A[i]<<" "<<ans<<endl;
    }
    cout<<ans<<endl;
}
accuracy: 1
time_ms: 90
memory_kb: 14908
final_rank: 7

code_snippet: #include<bits/stdc++.h>
// #include<atcoder/all>
// #include<boost/multiprecision/cpp_int.hpp>

using namespace std;
// using namespace atcoder;
// using bint = boost::multiprecision::cpp_int;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<ll>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using ve = vector<vector<int>>;
using vb = vector<bool>;
using vvb = vector<vb>;
#define rep(i,s,n) for(ll i = (ll)s;i < (ll)n;i++)
#define rrep(i,l,r) for(ll i = r-1;i >= l;i--)
#define ALL(x) (x).begin(),(x).end()
#define sz(c) ((ll)(c).size())
#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())
#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())
// #define MOD 1000000007
#define MOD 998244353
template<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;
template<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?" ":"");return os;}
template<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}
template<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<" "<<p.second;return os;}
template<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}
template<typename T> inline bool chmax(T &a,T b){if(a < b){a = b;return true;}return false;}
template<typename T> inline bool chmin(T &a,T b){if(a > b){a = b;return true;}return false;}
ld dist(ld x1,ld y1,ld x2, ld y2){return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}


int main(){
  
  ios_base::sync_with_stdio(0), cin.tie(0);
  ll n;cin >> n;
  vector<pii> v(n);
  rep(i,0,n)cin >> v[i];
  sort(ALL(v));
  vi y(n);
  rep(i,0,n)y[i] = v[i].second;
  vi rui(n+1);
  {
    ll mx = -1;
    rrep(i,0,n){
      chmax(mx,y[i]);
      if(y[i] == mx)rui[i]++;
      rui[i] += rui[i+1];
    }
  }
  deque<pll> inc,dec;
  rrep(i,0,n){
    if(!sz(inc))inc.push_back({y[i],i});
    else{
      if(inc.back().first < y[i])inc.push_back({y[i],i});
    }
    if(!sz(dec))dec.push_back({y[i],i});
    else{
      if(dec.back().first > y[i])dec.push_back({y[i],i});
    }
  }
  ll mx = -1,mn = MOD;
  auto lb_dec = [&](int val){
    int l = -1,r = sz(dec);
    while(r-l > 1){
      int md = (l + r)/2;
      if(dec[md].first >= val)l = md;
      else r = md;
    }
    return l;
  };
  auto lb_inc = [&](int val){
    int l = -1,r = sz(inc);
    while(r-l > 1){
      int md = (l+r)/2;
      if(inc[md].first >= val)r = md;
      else l = md;
    }
    return r;
  };
  ll res = 0;
  rep(i,0,n){
    chmax(mx,y[i]);chmin(mn,y[i]);
    while(sz(inc) && inc.back().second <= i)inc.pop_back();
    while(sz(dec) && dec.back().second <= i)dec.pop_back();
    if(mn != y[i])continue;
    int k_inc = lb_inc(mx);
    int k_dec = lb_dec(y[i]);
    if(k_inc >= sz(inc) || k_dec < 0)continue;
    ll l = dec[k_dec].second,r = inc[k_inc].second;
    if(k_dec+1 == sz(dec))l = i + 1;
    else l = dec[k_dec+1].second + 1;
    if(l <= r)res += rui[l]-rui[r+1];
    // cout << i << "   " << l << " " << r << "  a\n";
    // cout << i << "  " << rui[l]-rui[r+1] << "  bbb" << endl;
    // cout << i << " _______________\n";
    // rep(i,0,sz(inc))if(l <= inc[i].second && inc[i].second <= r){
      // cout << inc[i].first << " " << inc[i].second << " cc" << endl;
    // }
  }
  cout << res << endl;
  // rep(i,0,n)cout << rui[i]-rui[i+1] << " ";

  

  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 10948
final_rank: 2

code_snippet: #include<bits/stdc++.h>
// #include<atcoder/all>
// #include<boost/multiprecision/cpp_int.hpp>

using namespace std;
// using namespace atcoder;
// using bint = boost::multiprecision::cpp_int;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<ll>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using ve = vector<vector<int>>;
using vb = vector<bool>;
using vvb = vector<vb>;
#define rep(i,s,n) for(ll i = (ll)s;i < (ll)n;i++)
#define rrep(i,l,r) for(ll i = r-1;i >= l;i--)
#define ALL(x) (x).begin(),(x).end()
#define sz(c) ((ll)(c).size())
#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())
#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())
// #define MOD 1000000007
#define MOD 998244353
template<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;
template<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?" ":"");return os;}
template<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}
template<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<" "<<p.second;return os;}
template<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}
template<typename T> inline bool chmax(T &a,T b){if(a < b){a = b;return true;}return false;}
template<typename T> inline bool chmin(T &a,T b){if(a > b){a = b;return true;}return false;}
ld dist(ld x1,ld y1,ld x2, ld y2){return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}

int main(){
  
  ios_base::sync_with_stdio(0), cin.tie(0);
  int n;cin >> n;
  vector<pii> v(n);
  rep(i,0,n)cin >> v[i];
  sort(ALL(v));
  vi y(n);
  rep(i,0,n)y[i] = v[i].second;
  vi rui(n+1);
  {
    ll mx = -1;
    rrep(i,0,n){
      chmax(mx,y[i]);
      if(y[i] == mx)rui[i]++;
      rui[i] += rui[i+1];
    }
  }
  deque<pll> inc,dec;
  rrep(i,0,n){
    if(!sz(inc))inc.push_back({y[i],i});
    else{
      if(inc.back().first < y[i])inc.push_back({y[i],i});
    }
    if(!sz(dec))dec.push_back({y[i],i});
    else{
      if(dec.back().first > y[i])dec.push_back({y[i],i});
    }
  }
  ll mx = -1,mn = MOD;
  auto lb_dec = [&](int val){
    int l = -1,r = sz(dec);
    while(r-l > 1){
      int md = (l + r)/2;
      if(dec[md].first >= val)l = md;
      else r = md;
    }
    return l;
  };
  auto lb_inc = [&](int val){
    int l = -1,r = sz(inc);
    while(r-l > 1){
      int md = (l+r)/2;
      if(inc[md].first >= val)r = md;
      else l = md;
    }
    return r;
  };
  int res = 0;
  rep(i,0,n){
    chmax(mx,y[i]);chmin(mn,y[i]);
    while(sz(inc) && inc.back().second <= i)inc.pop_back();
    while(sz(dec) && dec.back().second <= i)dec.pop_back();
    if(mn != y[i])continue;
    int k_inc = lb_inc(mx);
    int k_dec = lb_dec(y[i]);
    if(k_inc >= sz(inc) || k_dec < 0)continue;
    int l = dec[k_dec].second,r = inc[k_inc].second;
    if(k_dec+1 == sz(dec))l = i + 1;
    else l = dec[k_dec+1].second + 1;
    if(l <= r)res += rui[l]-rui[r+1];
    // cout << i << "   " << l << " " << r << "  a\n";
    // cout << i << "  " << rui[l]-rui[r+1] << "  bbb" << endl;
    // cout << i << " _______________\n";
    // rep(i,0,sz(inc))if(l <= inc[i].second && inc[i].second <= r){
      // cout << inc[i].first << " " << inc[i].second << " cc" << endl;
    // }
  }
  cout << res << endl;
  // rep(i,0,n)cout << rui[i]-rui[i+1] << " ";

  

  return 0;
}
accuracy: 0.65625
time_ms: 40
memory_kb: 10884
final_rank: 10

code_snippet: #include<bits/stdc++.h>
using namespace std;

int main(){
    int n;cin >> n;
    vector<pair<int,int>> xy(n);
    for (int i = 0; i < n; i++)cin >> xy[i].first >> xy[i].second;

    sort(xy.begin(),xy.end());

    vector<int> premin(n),premax(n),sufmin(n),sufmax(n);
    vector<int> sufsum(n+1,0);

    premin[0] = xy[0].second;
    premax[0] = xy[0].second;
    for (int i = 1; i < n; i++){
        premin[i] = min(premin[i-1], xy[i].second);
        premax[i] = max(premax[i-1], xy[i].second);
    }
    sufmin[n-1] = xy[n-1].second;
    sufmax[n-1] = xy[n-1].second;
    for (int i = n-2; i >= 0; i--){
        sufmin[i] = min(sufmin[i+1], xy[i].second);
        sufmax[i] = max(sufmax[i+1], xy[i].second);
    }

    sufsum[n-1] = 1;
    for (int i = n-2; i >= 0; i--)sufsum[i] = sufsum[i+1] + (sufmax[i] == xy[i].second);

    long long ans = 0;
    for (int i = 0; i < n-1; i++){
        if (premin[i] != xy[i].second)continue;
        int l,r;
        {
            int ng = i,ok = n,mid;
            while(abs(ok-ng)!=1){
                mid = (ok+ng)/2;
                if (premin[i] > sufmin[mid])ng = mid;
                else ok = mid;
            }
            l = ok;
        }

        {
            int ng = n,ok = i+1,mid;
            while(abs(ok-ng)!=1){
                mid = (ok+ng)/2;
                if (premax[i] < sufmax[mid])ok = mid;
                else ng = mid;
            }
            r = ok;
        }
        if (r < l)continue;
        ans += sufsum[l] - sufsum[r+1];
        //cout << endl << i << ' ' << l << ' ' << r << endl << sufsum[l] - sufsum[r+1] << endl;
    }
    cout << ans << endl;
}
accuracy: 0.28125
time_ms: 80
memory_kb: 8520
final_rank: 14

code_snippet: #include<bits/stdc++.h>
using namespace std;

int main(){
    int n;cin >> n;
    vector<pair<int,int>> xy(n);
    for (int i = 0; i < n; i++)cin >> xy[i].first >> xy[i].second;

    sort(xy.begin(),xy.end());

    vector<int> premin(n),premax(n),sufmin(n),sufmax(n);
    vector<int> sufsum(n+1,0);

    premin[0] = xy[0].second;
    premax[0] = xy[0].second;
    for (int i = 1; i < n; i++){
        premin[i] = min(premin[i-1], xy[i].second);
        premax[i] = max(premax[i-1], xy[i].second);
    }
    sufmin[n-1] = xy[n-1].second;
    sufmax[n-1] = xy[n-1].second;
    for (int i = n-2; i >= 0; i--){
        sufmin[i] = min(sufmin[i+1], xy[i].second);
        sufmax[i] = max(sufmax[i+1], xy[i].second);
    }

    sufsum[n-1] = 1;
    for (int i = n-2; i >= 0; i--)sufsum[i] = sufsum[i+1] + (sufmax[i] == xy[i].second);

    long long ans = 0;
    for (int i = 0; i < n-1; i++){
        if (premin[i] != xy[i].second)continue;
        int l,r;
        {
            int ng = i,ok = n,mid;
            while(abs(ok-ng)!=1){
                mid = (ok+ng)/2;
                if (premin[i] > sufmin[mid])ng = mid;
                else ok = mid;
            }
            l = ok;
        }

        {
            int ng = n,ok = i+1,mid;
            while(abs(ok-ng)!=1){
                mid = (ok+ng)/2;
                if (premax[i] < sufmax[mid])ok = mid;
                else ng = mid;
            }
            r = ok;
        }
        if (r <= l)continue;
        ans += sufsum[l] - sufsum[r+1];
        //cout << endl << i << ' ' << l << ' ' << r << endl << sufsum[l] - sufsum[r+1] << endl;
    }
    cout << ans << endl;
}
accuracy: 0.28125
time_ms: 80
memory_kb: 8628
final_rank: 15

code_snippet: #include<bits/stdc++.h>
using namespace std;

int main(){
    int n;cin >> n;
    vector<pair<int,int>> xy(n);
    for (int i = 0; i < n; i++)cin >> xy[i].first >> xy[i].second;

    sort(xy.begin(),xy.end());

    vector<int> premin(n),premax(n),sufmin(n),sufmax(n);
    vector<int> sufsum(n+1,0);

    premin[0] = xy[0].second;
    premax[0] = xy[0].second;
    for (int i = 1; i < n; i++){
        premin[i] = min(premin[i-1], xy[i].second);
        premax[i] = max(premax[i-1], xy[i].second);
    }
    sufmin[n-1] = xy[n-1].second;
    sufmax[n-1] = xy[n-1].second;
    for (int i = n-2; i >= 0; i--){
        sufmin[i] = min(sufmin[i+1], xy[i].second);
        sufmax[i] = max(sufmax[i+1], xy[i].second);
    }

    sufsum[n-1] = 1;
    for (int i = n-2; i >= 0; i--)sufsum[i] = sufsum[i+1] + (sufmax[i] == xy[i].second);


    long long ans = 0;
    for (int i = 0; i < n-1; i++){
        if (premin[i] != xy[i].second)continue;
        int l,r;
        {
            int ng = i,ok = n,mid;
            while(abs(ok-ng)!=1){
                mid = (ok+ng)/2;
                if (premin[i] > sufmin[mid])ng = mid;
                else ok = mid;
            }
            l = ok;
        }

        {
            int ng = n,ok = i+1,mid;
            while(abs(ok-ng)!=1){
                mid = (ok+ng)/2;
                if (premax[i] < sufmax[mid])ok = mid;
                else ng = mid;
            }
            r = ok;
        }
        ans += sufsum[l] - sufsum[r+1];
    }
    cout << ans << endl;
}
accuracy: 0.09375
time_ms: 70
memory_kb: 8628
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main() {
    int n;
    cin >> n;
    int INF = 1e9;
    vector<int> X(n), Y(n);
    for (int i = 0; i < n; i++) {
        cin >> X[i] >> Y[i];
    }
    vector<pair<int, int>> P;
    for (int i = 0; i < n; i++) {
        P.emplace_back(X[i], Y[i]);
    }
    sort(P.begin(), P.end());
    int Max = -INF, Min = INF;
    vector<int> vMin;
    vector<int> vMaximum, vMaximumi;
    long long ans = 0;
    for (int i = n - 1; i > -1; --i) {
        if (Max < P[i].second) {
            vMaximumi.emplace_back(-i);
            vMaximum.emplace_back(P[i].second);
        }
        Max = max(P[i].second, Max);
        Min = min(P[i].second, Min);
        vMin.emplace_back(-Min);
    }

    Max = -INF, Min = INF;

    for (int i = 0; i < n; i++) {
        if (Min > P[i].second) {
            int index = lower_bound(vMin.begin(), vMin.end(), -P[i].second) -
                        vMin.begin();
            index = n  - index;
            int index2 =
                upper_bound(vMaximumi.begin(), vMaximumi.end(), -index) -
                vMaximumi.begin();
            int index3 = upper_bound(vMaximum.begin(), vMaximum.end(), Max) -
                         vMaximum.begin();

            ans += max(0, index2 - index3);
        }
        Max = max(P[i].second, Max);
        Min = min(P[i].second, Min);
    }
    cout << ans << '\n';
}
accuracy: 1
time_ms: 80
memory_kb: 9744
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main() {
    int n;
    cin >> n;
    int INF = 1e9;
    vector<int> X(n), Y(n);
    for (int i = 0; i < n; i++) {
        cin >> X[i] >> Y[i];
    }
    vector<pair<int, int>> P;
    for (int i = 0; i < n; i++) {
        P.emplace_back(X[i], Y[i]);
    }
    sort(P.begin(), P.end());
    int Max = -INF, Min = INF;
    vector<int> vMin;
    vector<int> vMaximum, vMaximumi;
    long long ans = 0;
    for (int i = n - 1; i > -1; --i) {
        if (Max < P[i].second) {
            vMaximumi.emplace_back(-i);
            vMaximum.emplace_back(P[i].second);
        }
        Max = max(P[i].second, Max);
        Min = min(P[i].second, Min);
        vMin.emplace_back(-Min);
    }

    Max = -INF, Min = INF;

    for (int i = 0; i < n; i++) {
        if (Min > P[i].second) {
            int index = lower_bound(vMin.begin(), vMin.end(), -P[i].second) -
                        vMin.begin();
            index = n - 1 - index;
            int index2 =
                upper_bound(vMaximumi.begin(), vMaximumi.end(), -index) -
                vMaximumi.begin();
            int index3 = upper_bound(vMaximum.begin(), vMaximum.end(), Max) -
                         vMaximum.begin();

            ans += max(0, index2 - index3);
        }
        Max = max(P[i].second, Max);
        Min = min(P[i].second, Min);
    }
    cout << ans << '\n';
}
accuracy: 0.09375
time_ms: 60
memory_kb: 8012
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep2(i,s,t) for(ll i=s;i<t;i++)
#define all(A) A.begin(),A.end()
using ll=long long;


ll test(vector<pair<int,int>> P){
  int N=P.size();
  int an=0;
  rep(i,N)rep(j,N){
    if(i==j)continue;
    if(P[i].first<P[j].first&&P[i].second<P[j].second){

    }
    else continue;
    bool OK=1;
    int l=min(P[i].first,P[j].first);
    int r=max(P[i].first,P[j].first);
    int u=max(P[i].second,P[j].second);
    int d=min(P[i].second,P[j].second );
    rep(k,N){
      if(i==k||j==k)continue;
      if(l<P[k].first&&P[k].first<r)continue;
      if(d<P[k].second&&P[k].second<u)continue;
      OK=0;
    }
    if(OK){
      an++;
      cout<<"test "<<i<<" "<<j<<endl;
    }
  }
  return an;
}

void runch(){
    int N=rand()%3+1;
    cin>>N;
    // cout<<N<<endl;
    vector<pair<int,int>> P(N);
    set<int> SX,SY;
    SX.insert(-1);
    SY.insert(-1);
    for(int i=0;i<N;i++){
      // int y=-1,x=-1;
      // while(SY.count(y))y=rand()%N+1;
      // while(SX.count(x))x=rand()%N+1;
      // SY.insert(y);
      // SX.insert(x);
      // P[i]={x,y};
      // cout<<i<<" "<<P[i].first<<" "<<P[i].second<<endl;
      cin>>P[i].first>>P[i].second;
    }
    // int bn=test(P);
    // cout<<bn<<" ";
    sort(all(P));
    auto Q=P;
    reverse(all(Q));
    int mx=-1;
    vector<int> QX,QY;
    rep(i,N){
      if(mx<Q[i].second){
        QX.push_back(Q[i].first);
        QY.push_back(Q[i].second);
      }
      mx=max(mx,Q[i].second);
    }

    vector<int> LM(N),TM(N);
    int MM=1e8,mm=-1e8;
    for(int i=N-1;i>=0;i--){
      MM=min(MM,P[i].second);
      LM[i]=MM;
      mm=max(mm,P[i].second);
      TM[i]=mm;
    }


    int QN=QX.size();
    // swap(Q,SQ);
    mx=1e8;
    int pm=-1e8; 
    ll an=0;
    auto QQY=QY;
    // for(int i=0;i<QN;i++)cout<<QX[i]<<"*"<<QY[i]<<endl;
    reverse(all(QX));
    
    // for(int i=0;i<N;i++)cout<<P[i].first<<" "<<P[i].second<<endl;
    rep(i,N){
      
      pm=max(pm,P[i].second);
      if(mx>P[i].second){
        
        int L=0,R=N;
        while(R-L>1){
          int mid=(R+L)/2;
          if(LM[mid]<=P[i].second)L=mid;
          else R=mid;
        }
        int a=upper_bound(all(QX),P[L].first)-QX.begin();
        int b=QN-1-(lower_bound(all(QY),pm)-QY.begin());
        // cout<<i<<" "<<P[i].second<<" "<<L<<" "<<a<<" "<<b<<endl;
        an+=max(0,b-a+1);
      }
      mx=min(mx,P[i].second);
    }
    cout<<an<<endl;
    // assert(an==bn);
}

int main(){
    
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    rep(i,1)runch();
    
}
accuracy: 1
time_ms: 40
memory_kb: 10076
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep2(i,s,t) for(ll i=s;i<t;i++)
#define all(A) A.begin(),A.end()
using ll=long long;

int main(){
    
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int N;
    cin>>N;
    vector<pair<int,int>> P(N);
    for(int i=0;i<N;i++)cin>>P[i].first>>P[i].second;
    sort(all(P));
    auto Q=P;
    reverse(all(Q));
    int mx=-1;
    vector<int> QX,QY;
    rep(i,N){
      if(mx<Q[i].second){
        QX.push_back(Q[i].first);
        QY.push_back(Q[i].second);
      }
      mx=max(mx,Q[i].second);
    }

    vector<int> LM(N);
    int MM=1e8;
    for(int i=N-1;i>=0;i--){
      LM[i]=MM;
      
      MM=min(MM,P[i].second);
    }


    int QN=QX.size();
    // swap(Q,SQ);
    mx=1e8;
    int pm=-1e8; 
    ll an=0;
    auto QQY=QY;
    // for(int i=0;i<QN;i++)cout<<QX[i]<<"*"<<QY[i]<<endl;
    reverse(all(QX));
    
    // for(int i=0;i<N;i++)cout<<P[i].first<<" "<<P[i].second<<endl;
    rep(i,N){
      
      pm=max(pm,P[i].second);
      if(mx>P[i].second){
        
        int L=0,R=N;
        while(R-L>1){
          int mid=(R+L)/2;
          if(LM[mid]<=P[i].second)L=mid;
          else R=mid;
        }
        // cout<<i<<" "<<L<<endl;
        int a=upper_bound(all(QX),R)-QX.begin();
        int b=QN-1-(upper_bound(all(QY),pm)-QY.begin());
        // cout<<i<<" "<<a<<" "<<b<<endl;
        an+=max(0,b-a+1);
        // for(int j=a;j<=b;j++)cout<<QX[j]<<" "<<QY[QN-1-j]<<endl;
        // cout<<i<<" "<<a<<" "<<b<<endl;
      }
      mx=min(mx,P[i].second);
    }
    cout<<an<<endl;
    
}
accuracy: 0.3125
time_ms: 30
memory_kb: 9464
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep2(i,s,t) for(ll i=s;i<t;i++)
#define all(A) A.begin(),A.end()
using ll=long long;

int main(){
    
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int N;
    cin>>N;
    vector<pair<int,int>> P(N);
    for(int i=0;i<N;i++)cin>>P[i].first>>P[i].second;
    sort(all(P));
    auto Q=P;
    reverse(all(Q));
    int mx=-1;
    vector<int> QX,QY;
    rep(i,N){
      if(mx<Q[i].second){
        QX.push_back(Q[i].first);
        QY.push_back(Q[i].second);
      }
      mx=max(mx,Q[i].second);
    }

    vector<int> LM(N);
    int MM=1e8;
    for(int i=N-1;i>=0;i--){
      MM=min(MM,P[i].second);
      LM[i]=MM;
    }


    int QN=QX.size();
    // swap(Q,SQ);
    mx=1e8;
    int pm=-1e8; 
    ll an=0;
    reverse(all(QY));
    rep(i,N){
      
      pm=max(pm,P[i].second);
      if(mx>P[i].second){
        
        int L=i,R=N;
        while(R-L>1){
          int mid=(R+L)/2;
          if(LM[mid]<P[i].second)L=mid;
          else R=mid;
        }

        int a=upper_bound(all(QX),R)-QX.begin();
        int b=QN-1-(upper_bound(all(QY),pm)-QY.begin());
      
        an+=max(0,b-a+1);
        // for(int j=a;j<=b;j++)cout<<QX[j]<<" "<<QY[QN-1-j]<<endl;
        // cout<<i<<" "<<a<<" "<<b<<endl;
      }
      mx=min(mx,P[i].second);
    }
    cout<<an<<endl;
    
}
accuracy: 0.28125
time_ms: 30
memory_kb: 8784
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep2(i,s,t) for(ll i=s;i<t;i++)
#define all(A) A.begin(),A.end()
using ll=long long;

int main(){
    
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int N;
    cin>>N;
    vector<pair<int,int>> P(N);
    for(int i=0;i<N;i++)cin>>P[i].first>>P[i].second;
    sort(all(P));
    auto Q=P;
    reverse(all(Q));
    int mx=-1;
    vector<int> QX,QY;
    rep(i,N){
      if(mx<Q[i].second){
        QX.push_back(Q[i].first);
        QY.push_back(Q[i].second);
      }
      mx=max(mx,Q[i].second);
    }
    int QN=QX.size();
    // swap(Q,SQ);
    mx=1e8;
    int pm=-1e8; 
    ll an=0;
    reverse(all(QY));
    rep(i,N){
      
      pm=max(pm,P[i].second);
      if(mx>P[i].second){
        int a=upper_bound(all(QX),P[i].first)-QX.begin();
        int b=QN-1-(upper_bound(all(QY),pm)-QY.begin());
        an+=max(0,b-a+1);
      }
      mx=min(mx,P[i].second);
    }
    cout<<an<<endl;
    
}
accuracy: 0.28125
time_ms: 20
memory_kb: 7988
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep2(i,s,t) for(ll i=s;i<t;i++)
#define all(A) A.begin(),A.end()
using ll=long long;

int main(){
    
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int N;
    cin>>N;
    vector<pair<int,int>> P(N);
    for(int i=0;i<N;i++)cin>>P[i].first>>P[i].second;
    sort(all(P));
    auto Q=P;
    reverse(all(Q));
    int mx=-1;
    vector<int> QX,QY;
    rep(i,N){
      if(mx<Q[i].second){
        QX.push_back(Q[i].first);
        QY.push_back(Q[i].second);
      }
      mx=max(mx,Q[i].second);
    }
    int QN=QX.size();
    // swap(Q,SQ);
    mx=1e8;
    int pm=-1e8; 
    ll an=0;
    reverse(all(QY));
    rep(i,N){
      if(mx>P[i].second){
        int a=upper_bound(all(QX),P[i].first)-QX.begin();
        int b=QN-1-(upper_bound(all(QY),pm)-QY.begin());
        an+=max(0,b-a+1);
      }
      mx=min(mx,P[i].second);
      pm=max(pm,P[i].second);
    }
    cout<<an<<endl;
    
}
accuracy: 0.09375
time_ms: 20
memory_kb: 6136
final_rank: 16

code_snippet: #include<bits/stdc++.h>
using namespace std;
template <class S, S (*op)(S, S), S (*e)()> class segtree {
public:
  explicit segtree(int n) : segtree(vector<S>(n, e())) {}
  explicit segtree(vector<S> v) : n((int)v.size()) {
    size = 1;
    while (size < n) size *= 2;
    data.assign(2 * size, e());
    for (int i = 0; i < n; i++) data[size + i] = v[i];
    for (int i = size - 1; i >= 1; i--) update(i);
  }

  void set(int p, S x) {
    assert(0 <= p && p < n);
    p += size;
    data[p] = x;
    for (p /= 2; p >= 1; p /= 2) update(p);
  }

  S get(int p) {
    assert(0 <= p && p < n);
    return data[p + size];
  }

  S prod(int l, int r) {
    assert(0 <= l && l <= r && r <= n);
    S sml = e(), smr = e();
    l += size;
    r += size;

    while (l < r) {
      if (l & 1) sml = op(sml, data[l++]);
      if (r & 1) smr = op(data[--r], smr);
        l /= 2;
        r /= 2;
      }
    return op(sml, smr);
  }

private:
  int n, size;
  vector<S> data;

  void update(int k) {
      data[k] = op(data[2 * k], data[2 * k + 1]);
  }
};
int op(int a,int b){
  return a+b;
}
int e(){
  return 0;
}
int main(){
  cin.tie(0)->sync_with_stdio(0);
  int N;cin>>N;
  vector<int>X(N),Y(N);for(int i=0;i<N;++i)cin>>X[i]>>Y[i];
  map<pair<int,int>,pair<int,int>>dummy;
  map<int,vector<int>>xy,yx;
  for(int i=0;i<N;++i){
    xy[X[i]].emplace_back(Y[i]);
    yx[Y[i]].emplace_back(X[i]);
    dummy[{X[i],Y[i]}]={1<<30,1<<30};
  }
  {
    int mny=1<<30,mxy=-1<<30;
    for(auto it=xy.begin();it!=xy.end();++it){
      int x=it->first;
      for(int y:it->second){
        if(y<=mny)dummy[{x,y}].second=max(y,mxy);
      }
      for(int y:it->second){
        mny=min(mny,y);
        mxy=max(mxy,y);
      }
    }
  }
  {
    int mnx=1<<30,mxx=-1<<30;
    for(auto it=yx.begin();it!=yx.end();++it){
      int y=it->first;
      for(int x:it->second){
        if(x<=mnx)dummy[{x,y}].first=max(x,mxx);
      }
      for(int x:it->second){
        mnx=min(mnx,x);
        mxx=max(mxx,x);
      }
    }
  }
  //for(int i=0;i<N;++i)cout<<dummy[{X[i],Y[i]}].first<<' '<<dummy[{X[i],Y[i]}].second<<endl;
  map<int,vector<pair<int,int>>>mp;
  vector<int>ref;
  for(int i=0;i<N;++i){
    mp[X[i]].emplace_back(Y[i],0);
    auto[dx,dy]=dummy[{X[i],Y[i]}];
    mp[dx].emplace_back(dy,1);
    ref.emplace_back(Y[i]);
    ref.emplace_back(dy);
  }
  sort(ref.begin(),ref.end());
  ref.erase(unique(ref.begin(),ref.end()),ref.end());
  segtree<int,op,e>seg(ref.size());
  long long ans=0;
  int mxy=-1<<30;
  for(auto it=mp.rbegin();it!=mp.rend();++it){
    int nmxy=mxy;
    for(auto[y,is_dummy]:it->second){
      if(is_dummy)continue;
      if(mxy<=y){
        int pos=distance(ref.begin(),lower_bound(ref.begin(),ref.end(),y));
        seg.set(pos,seg.get(pos)+1);
      }
      nmxy=max(nmxy,y);
    }
    mxy=nmxy;
    for(auto[y,is_dummy]:it->second){
      if(is_dummy){
        int l=distance(ref.begin(),lower_bound(ref.begin(),ref.end(),y));
        ans+=seg.prod(l,ref.size());
      }
    }
  }
  cout<<ans<<endl;
}
accuracy: 1
time_ms: 550
memory_kb: 89068
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using namespace std;
class Segtree{
private:
  vector<int>dat;
  int siz;
  
  int query(int l,int r,int a,int b,int u){
    if(r<=a||b<=l)return 0;
    if(l<=a&&b<=r)return dat[u];
    int mid=(a+b)/2;
    return query(l,r,a,mid,2*u)+query(l,r,mid,b,2*u+1);
  }

public:
  Segtree(int n){
    siz=1;
    while(siz<n)siz*=2;
    dat.assign(2*siz,0);
  }

  void set(int p,int x){
    p=p+siz;
    dat[p]=x;
    while(2<=p){
      p/=2;
      dat[p]=dat[2*p]+dat[2*p+1];
    }
  }

  int prod(int l,int r){
    return query(l+1,r+1,1,siz+1,1);
  }
};
int main(){
  cin.tie(0)->sync_with_stdio(0);
  int N;cin>>N;
  vector<int>X(N),Y(N);for(int i=0;i<N;++i)cin>>X[i]>>Y[i];
  map<int,vector<int>>xy,yx;
  for(int i=0;i<N;++i){
    xy[X[i]].emplace_back(Y[i]);
    yx[Y[i]].emplace_back(X[i]);
  }
  map<pair<int,int>,pair<int,int>>dummy;
  for(int i=0;i<N;++i){
    dummy[{X[i],Y[i]}]={-1<<30,-1<<30};
  }

  // dummy y;
  {
    int mny=1<<29,mxy=-1<<29;
    for(auto it=xy.begin();it!=xy.end();++it){
      int x=it->first;
      for(int y:it->second){
        if(mny<y)continue;
        dummy[{x,y}].second=mxy;
      }
      for(int y:it->second){
        mny=min(mny,y);
        mxy=max(mxy,y);
      }
    }
  }
  // dummy x
  {
    int mnx=1<<29,mxx=-1<<29;
    for(auto it=yx.begin();it!=yx.end();++it){
      int y=it->first;
      for(int x:it->second){
        if(mnx<x)continue;
        dummy[{x,y}].first=mxx;
      }
      for(int x:it->second){
        mnx=min(mnx,x);
        mxx=max(mxx,x);
      }
    }
  }
  //for(int i=0;i<N;++i)cout<<dummy[{X[i],Y[i]}].first<<' '<<dummy[{X[i],Y[i]}].second<<endl;

  // {y offset, is dummy?}
  map<int,vector<pair<int,bool>>>mp;
  for(int i=0;i<N;++i){
    mp[X[i]].emplace_back(Y[i],0);
    if(dummy[{X[i],Y[i]}].first!=-1<<30&&dummy[{X[i],Y[i]}].second!=-1<<30){
      mp[dummy[{X[i],Y[i]}].first].emplace_back(dummy[{X[i],Y[i]}].second,1);
    }
  }
  vector<int>ref=Y;
  sort(ref.begin(),ref.end());
  ref.erase(unique(ref.begin(),ref.end()),ref.end());
  Segtree seg(ref.size());
  long long ans=0;
  long long cum=0;
  int mxy=-1<<30;
  for(auto it=mp.rbegin();it!=mp.rend();++it){
    vector<int>pts;
    for(auto[y,flag]:it->second){
      if(!flag&&mxy<=y){
        pts.emplace_back(y);
      }
    }
    sort(pts.begin(),pts.end());
    for(auto[y,flag]:it->second){
      if(flag){ // dummy
        ans+=cum;
        ans+=distance(lower_bound(pts.begin(),pts.end(),y),pts.end());
        int r=distance(ref.begin(),lower_bound(ref.begin(),ref.end(),y));
        ans-=seg.prod(0,r);
      }
    }
    for(int y:pts){
      int p=distance(ref.begin(),lower_bound(ref.begin(),ref.end(),y));
      seg.set(p,seg.prod(p,p+1)+1);
    }
    cum+=pts.size();
    for(auto[y,flag]:it->second){
      if(!flag){
        mxy=max(mxy,y);
      }
    }
  }
  cout<<ans<<endl;
}
accuracy: 0.09375
time_ms: 280
memory_kb: 85724
final_rank: 20

code_snippet: #include <iostream>
#include <cassert>
#include <utility>
#include <vector>
#include <algorithm>
int N;
int X[200020], Y[200020];
int main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);
    std::cin >> N;
    std::vector<std::pair<int, int>> xy(N);
    for (int i{} ; i < N ; i++) {
        std::cin >> X[i] >> Y[i];
        xy[i] = {X[i], Y[i]};
    }
    std::sort(xy.begin(), xy.end());
    const int MAX{1000010};
    std::vector<int> min_right(MAX + 1);
    for (int i{} ; i < N ; i++) min_right[xy[i].second] = i;
    for (int i{} ; i < MAX ; i++) min_right[i + 1] = std::max(min_right[i + 1], min_right[i]);
    std::vector<bool> max(N), min(N);
    {
        std::vector<int> stk;
        for (int i{N} ; i-- ; ) {
            while (stk.size() and xy[stk.back()].second < xy[i].second) stk.pop_back();
            max[i] = stk.empty();
            stk.push_back(i);
        }
    }
    {
        std::vector<int> stk;
        for (int i{} ; i < N ; i++) {
            while (stk.size() and xy[stk.back()].second > xy[i].second) stk.pop_back();
            min[i] = stk.empty();
            stk.push_back(i);
        }
    }
    std::vector<int> sum(N + 1);
    for (int i{} ; i < N ; i++) sum[i + 1] = sum[i] + max[i];
    std::vector<std::vector<std::pair<int, int>>> query(N); // firstより右にある、y座標がsecondより大きい点
    query.reserve(N);
    int prev_max{};
    for (int i{} ; i < N ; i++) {
        prev_max = std::max(prev_max, xy[i].second);
        if (min[i]) {
            int r{min_right[xy[i].second]};
            query[r].push_back({prev_max, i});
        }
    }
    std::vector<int> fen(MAX + 1);
    auto add{[&](int i, int x) -> void {
        assert(0 <= i and i < MAX);
        for (i++ ; i <= MAX ; i += i & -i) fen[i] += x;
    }};
    auto pref{[&](int r) -> int {
        assert(0 <= r and r <= MAX);
        int res{};
        for ( ; r ; r -= r & -r) res += fen[r];
        return res;
    }};
    auto prod{[&](int l, int r) -> int {
        return -pref(l) + pref(r);
    }};
    long long ans{};
    for (int x{N} ; x-- ;) {
        for (auto [y, i] : query[x]) {
            ans += prod(y, MAX);
        }
        if (max[x]) {
            add(xy[x].second, 1);
        }
    }
    std::cout << ans << '\n';
}
accuracy: 1
time_ms: 30
memory_kb: 21188
final_rank: 6