結果
| 問題 |
No.743 Segments on a Polygon
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2018-10-07 20:54:51 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 78 ms / 2,000 ms |
| コード長 | 3,567 bytes |
| コンパイル時間 | 2,168 ms |
| コンパイル使用メモリ | 212,464 KB |
| 最終ジャッジ日時 | 2025-01-06 14:26:14 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 10 |
ソースコード
#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;
}