結果

問題 No.743 Segments on a Polygon
ユーザー PachicobuePachicobue
提出日時 2018-10-07 20:54:51
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 86 ms / 2,000 ms
コード長 3,567 bytes
コンパイル時間 2,448 ms
コンパイル使用メモリ 217,056 KB
実行使用メモリ 6,148 KB
最終ジャッジ日時 2023-08-09 06:27:18
合計ジャッジ時間 4,225 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 86 ms
6,148 KB
testcase_01 AC 85 ms
6,048 KB
testcase_02 AC 84 ms
5,988 KB
testcase_03 AC 84 ms
5,824 KB
testcase_04 AC 83 ms
5,780 KB
testcase_05 AC 84 ms
5,960 KB
testcase_06 AC 86 ms
6,004 KB
testcase_07 AC 83 ms
5,788 KB
testcase_08 AC 84 ms
5,988 KB
testcase_09 AC 58 ms
5,708 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define show(x) std::cerr << #x << " = " << x << std::endl
using ull = unsigned long long;
constexpr std::size_t PC(ull v) { return v = (v & 0x5555555555555555ULL) + (v >> 1 & 0x5555555555555555ULL), v = (v & 0x3333333333333333ULL) + (v >> 2 & 0x3333333333333333ULL), v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL, static_cast<std::size_t>(v * 0x0101010101010101ULL >> 56 & 0x7f); }
class WaveletMatrix
{
public:
    static constexpr std::size_t L = 18;
    using T = uint;
    template <typename InIt>
    WaveletMatrix(const InIt first, const InIt last) : sz(std::distance(first, last))
    {
        std::vector<bool> b(sz);
        std::vector<T> v(first, last);
        for (std::size_t i = 0, j = L - 1; i < L; i++, j--) {
            std::vector<T> z, o;
            for (std::size_t i = 0; i < sz; i++) { b[i] = (v[i] >> j) & 1, (b[i] ? o : z).push_back(v[i]); }
            table.push_back(FullyIndexableDictionary{b}), v = z, zero.push_back(z.size());
            for (const T e : o) { v.push_back(e); }
        }
    }
    std::size_t noLessThan(std::size_t l, std::size_t r, const T v) const
    {
        std::size_t off = 0;
        for (std::size_t i = 0, j = L - 1; i < L; i++, j--) {
            const bool b = (v >> j) & 1;
            const std::size_t rl = table[i].rank(l), rr = table[i].rank(r);
            l = (b ? rl + zero[i] : l - rl), r = (b ? rr + zero[i] : r - rr), off += (b ? 0 : rr - rl);
        }
        return r - l + off;
    }
    std::size_t rangeFreq(const std::size_t l, const std::size_t r, const T vinf, const T vsup) const { return noLessThan(l, r, vinf) - noLessThan(l, r, vsup); }
    std::size_t rank(const std::size_t l, const std::size_t r, const T v) const { return rangeFreq(l, r, v, v + 1); }

private:
    class FullyIndexableDictionary
    {
    private:
        using B = unsigned long long;
        static constexpr std::size_t BS = sizeof(B) * 8;
        const std::size_t sz;
        std::vector<B> data;
        std::vector<std::size_t> large;

    public:
        FullyIndexableDictionary(const std::vector<bool>& b) : sz(b.size()), data((sz + BS) / BS, 0), large((sz + BS) / BS, 0)
        {
            std::size_t one = 0;
            for (std::size_t i = 0; i < sz; i++) {
                data[i / BS] |= ((B)b[i] << (i % BS)), one += b[i];
                if (i % BS == BS - 1) { large[(i + 1) / BS] = one; }
            }
        }
        bool operator[](const std::size_t n) const { return (data[n / BS] >> (n % BS)) & 1; }
        std::size_t rank(const std::size_t n) const { return large[n / BS] + (n % BS == 0 ? 0 : PC(data[n / BS] & ((1ULL << (n % BS)) - 1))); }
    };
    const std::size_t sz;
    std::vector<std::size_t> zero;
    std::vector<FullyIndexableDictionary> table;
};

int main()
{
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);
    std::size_t N, M;
    std::cin >> N >> M;
    using P = std::pair<uint, uint>;
    std::vector<P> p(N);
    for (std::size_t i = 0; i < N; i++) {
        std::cin >> p[i].first >> p[i].second;
        if (p[i].first > p[i].second) { std::swap(p[i].first, p[i].second); };
    }
    std::sort(p.begin(), p.end());
    std::vector<uint> b(N);
    for (std::size_t i = 0; i < N; i++) { b[i] = p[i].second; }
    WaveletMatrix wm{b.begin(), b.end()};
    ull ans = 0;
    for (std::size_t i = 0; i < N - 1; i++) {
        const std::size_t isup = std::upper_bound(p.begin(), p.end(), P{b[i], 0}) - p.begin();
        ans += wm.noLessThan(i + 1, isup, b[i]);
    }
    std::cout << ans << std::endl;
    return 0;
}
0