結果

問題 No.3313 Matryoshka
コンテスト
ユーザー otoshigo
提出日時 2025-10-24 22:39:40
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 339 ms / 4,000 ms
コード長 1,478 bytes
コンパイル時間 3,455 ms
コンパイル使用メモリ 289,600 KB
実行使用メモリ 23,104 KB
最終ジャッジ日時 2025-10-24 22:39:51
合計ジャッジ時間 10,827 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (ll i = (ll)s; i < (ll)(t); i++)
#define rrep(i, s, t) for (ll i = (ll)(t) - 1; i >= (ll)(s); i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)

#define TT template <typename T>
template <class T1, class T2>
bool chmin(T1& x, T2 y) { return x > y ? (x = y, true) : false; }
template <class T1, class T2>
bool chmax(T1& x, T2 y) { return x < y ? (x = y, true) : false; }

struct io_setup {
    io_setup() {
        ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        cout << fixed << setprecision(15);
        srand(time(NULL));
    }
} io_setup;

#include <atcoder/fenwicktree>

const int MAX = 1'000'000;

void solve() {
    int N;
    cin >> N;
    vector<int> L(N), R(N);
    rep(i, 0, N) {
        cin >> L[i] >> R[i];
        L[i]--;
        R[i]--;
    }
    ll ans = 0;
    atcoder::fenwick_tree<int> fw1(MAX), fw2(MAX);
    rrep(i, 0, N) {
        int l = L[i], r = R[i];
        ans += fw1.sum(l + 1, r);
        ans += fw2.sum(l + 1, r);
        fw1.add(l, 1);
        fw2.add(r, 1);
    }
    vector<pair<int, int>> vp;
    rep(i, 0, N) vp.push_back({R[i], L[i]});
    sort(all(vp));
    atcoder::fenwick_tree<int> fw3(MAX);
    for (auto [r, l] : vp) {
        ans -= fw3.sum(l + 1, r);
        fw3.add(l, -1);
        fw3.add(r, 1);
    }
    assert(ans % 2 == 0);
    ans /= 2;
    cout << ans << "\n";
}

int main() {
    solve();
}
0