結果
問題 |
No.742 にゃんにゃんにゃん 猫の挨拶
|
ユーザー |
|
提出日時 | 2018-10-05 21:38:15 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 13 ms / 2,500 ms |
コード長 | 2,669 bytes |
コンパイル時間 | 2,316 ms |
コンパイル使用メモリ | 197,148 KB |
最終ジャッジ日時 | 2025-01-06 14:17:10 |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 16 |
ソースコード
#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 Base> class BinaryIndexedTree { public: using T = typename Base::T; using AbelGroup = Base; BinaryIndexedTree(const std::size_t n) : data_num(n), size(SZ(n)), value(size + 1, Base::id()) {} template <typename InIt> BinaryIndexedTree(const InIt first, const InIt last) : data_num(distance(first, last)), size(SZ(data_num)), value(size + 1, Base::id()) { copy(first, last, value.begin() + 1); for (std::size_t x = 1; x < size; x++) { value[x + (x & -x)] = op(value[x + (x & -x)], value[x]); } } T accumulate(const std::size_t a) const // [0,a] { T sum = Base::id(); for (std::size_t ind = a + 1; ind != 0; ind &= ind - 1) { sum = op(sum, value[ind]); } return sum; } void add(const std::size_t a, const T& val) { for (std::size_t ind = a + 1; ind <= size; ind += ind & (-ind)) { value[ind] = op(value[ind], val); } } private: const std::size_t data_num, size; const Base op{}; std::vector<T> value; }; 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; std::cin >> N; std::vector<int> M(N); for (int i = 0; i < N; i++) { std::cin >> M[i]; } BinaryIndexedTree<Sum> bit(N + 1); int ans = 0; for (int i = N - 1; i >= 0; i--) { ans += bit.accumulate(M[i]); bit.add(M[i], 1); } std::cout << ans << std::endl; return 0; }