結果
問題 | No.743 Segments on a Polygon |
ユーザー | Pachicobue |
提出日時 | 2018-10-05 21:55:55 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 394 ms / 2,000 ms |
コード長 | 3,589 bytes |
コンパイル時間 | 2,278 ms |
コンパイル使用メモリ | 214,432 KB |
実行使用メモリ | 64,976 KB |
最終ジャッジ日時 | 2024-11-14 07:57:30 |
合計ジャッジ時間 | 7,102 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 390 ms
64,836 KB |
testcase_01 | AC | 383 ms
64,840 KB |
testcase_02 | AC | 393 ms
64,768 KB |
testcase_03 | AC | 388 ms
64,648 KB |
testcase_04 | AC | 390 ms
64,772 KB |
testcase_05 | AC | 382 ms
64,960 KB |
testcase_06 | AC | 393 ms
64,776 KB |
testcase_07 | AC | 391 ms
64,976 KB |
testcase_08 | AC | 394 ms
64,860 KB |
testcase_09 | AC | 167 ms
64,976 KB |
ソースコード
#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()) {} template <typename InIt> SegmentTree(const InIt first, const InIt last) : data_num(distance(first, last)), half(SZ(data_num)), value(half << 1, Monoid::id()) { std::copy(first, last, value.begin() + half); for (std::size_t i = half - 1; i >= 1; i--) { up(i); } } T get(const std::size_t a) const { return value[a + half]; } void set(std::size_t a, const T& val) { value[a += half] = val; while (a >>= 1) { up(a); } } template <typename F, typename QueryOp, typename AccOp> F query(std::size_t L, std::size_t R, const QueryOp& query_op, const AccOp& acc_op) const { F accl{}, accr{}; for (L += half, R += half; L < R; L >>= 1, R >>= 1) { if (L & 1) { accl = acc_op(accl, query_op(value[L++])); } if (R & 1) { accr = acc_op(query_op(value[--R]), accr); } } return acc_op(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 MergedSequence { using F = ll; using T = std::vector<F>; T operator()(const T& a, const T& b) const { T c(a.size() + b.size()); std::merge(a.begin(), a.end(), b.begin(), b.end(), c.begin()); return c; } static T id() { return T{}; } }; int main() { int N, M; std::cin >> N >> M; std::vector<std::vector<ll>> v(M + 1, {-1}); using P = std::pair<int, int>; std::vector<P> p(N); for (int i = 0; i < N; i++) { int a, b; std::cin >> a >> b; if (a > b) { std::swap(a, b); } v[a] = {b}; p[i] = {a, b}; } SegmentTree<MergedSequence> seg(v.begin(), v.end()); ll ans = 0; for (int i = 0; i < N; i++) { const int s = p[i].first, t = p[i].second; const auto f1 = [&](const auto& interval) { return interval.end() - upper_bound(interval.begin(), interval.end(), t); }; static const auto f2 = [](const std::size_t x, const std::size_t y) { return x + y; }; const std::size_t val = seg.query<std::size_t>(s, t, f1, f2); ans += val; } std::cout << ans << std::endl; return 0; }