結果

問題 No.743 Segments on a Polygon
ユーザー PachicobuePachicobue
提出日時 2018-10-05 22:02:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 86 ms / 2,000 ms
コード長 2,636 bytes
コンパイル時間 2,015 ms
コンパイル使用メモリ 199,648 KB
実行使用メモリ 8,320 KB
最終ジャッジ日時 2024-04-26 18:05:56
合計ジャッジ時間 3,586 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
8,064 KB
testcase_01 AC 81 ms
8,320 KB
testcase_02 AC 81 ms
8,064 KB
testcase_03 AC 86 ms
8,192 KB
testcase_04 AC 83 ms
8,192 KB
testcase_05 AC 83 ms
8,064 KB
testcase_06 AC 82 ms
8,192 KB
testcase_07 AC 83 ms
8,064 KB
testcase_08 AC 79 ms
8,192 KB
testcase_09 AC 73 ms
8,064 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define show(x) std::cerr << #x << " = " << x << std::endl
using ll = long long;
using ull = unsigned long long;
using ld = long double;
constexpr ll MOD = 1000000007LL;
template <typename T>
constexpr T INF = std::numeric_limits<T>::max() / 10;
std::mt19937 mt{std::random_device{}()};
constexpr std::size_t PC(const ull v)
{
    ull count = (v & 0x5555555555555555ULL) + ((v >> 1) & 0x5555555555555555ULL);
    count = (count & 0x3333333333333333ULL) + ((count >> 2) & 0x3333333333333333ULL);
    count = (count & 0x0f0f0f0f0f0f0f0fULL) + ((count >> 4) & 0x0f0f0f0f0f0f0f0fULL);
    count = (count & 0x00ff00ff00ff00ffULL) + ((count >> 8) & 0x00ff00ff00ff00ffULL);
    count = (count & 0x0000ffff0000ffffULL) + ((count >> 16) & 0x0000ffff0000ffffULL);
    return static_cast<std::size_t>((count & 0x00000000ffffffffULL) + ((count >> 32) & 0x00000000ffffffffULL));
}
constexpr std::size_t LG(ull v) { return v == 0 ? 0 : (v--, v |= (v >> 1), v |= (v >> 2), v |= (v >> 4), v |= (v >> 8), v |= (v >> 16), v |= (v >> 32), PC(v)); }
constexpr ull SZ(const ull v) { return 1ULL << LG(v); }
template <typename Monoid>
class SegmentTree
{
public:
    using BaseMonoid = Monoid;
    using T = typename Monoid::T;

    SegmentTree(const std::size_t n) : data_num(n), half(SZ(n)), value(half << 1, Monoid::id()) {}
    void set(std::size_t a, const T& val)
    {
        value[a += half] = val;
        while (a >>= 1) { up(a); }
    }
    T accumulate(std::size_t L, std::size_t R) const
    {
        T accl = Monoid::id(), accr = Monoid::id();
        for (L += half, R += half; L < R; L >>= 1, R >>= 1) {
            if (L & 1) { accl = acc(accl, value[L++]); }
            if (R & 1) { accr = acc(value[--R], accr); }
        }
        return acc(accl, accr);
    }

private:
    void up(const std::size_t i) { value[i] = acc(value[i << 1], value[i << 1 | 1]); }
    const std::size_t data_num, half;
    std::vector<T> value;
    const Monoid acc{};
};

struct Sum
{
    using T = ll;
    T operator()(const T& a, const T& b) const { return a + b; }
    static T inv(const T& a) { return -a; }
    static constexpr T id() { return 0; }
};

int main()
{
    int N, M;
    std::cin >> N >> M;
    std::vector<int> t(M + 1, -1);
    SegmentTree<Sum> seg(M);
    for (int i = 0; i < N; i++) {
        int a, b;
        std::cin >> a >> b;
        if (a > b) { std::swap(a, b); }
        t[a] = b;
    }
    ll ans = 0;
    for (int i = 0; i < M; i++) {
        if (t[i] == -1) { continue; }
        ans += seg.accumulate(i, t[i] + 1);
        seg.set(t[i], 1);
    }
    std::cout << ans << std::endl;
    return 0;
}
0