結果
| 問題 | No.3469 ジャッジ結果の逆転数 |
| コンテスト | |
| ユーザー |
zawakasu
|
| 提出日時 | 2026-03-06 22:51:27 |
| 言語 | C++23 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 53 ms / 2,000 ms |
| コード長 | 4,461 bytes |
| 記録 | |
| コンパイル時間 | 2,036 ms |
| コンパイル使用メモリ | 213,756 KB |
| 実行使用メモリ | 7,848 KB |
| 最終ジャッジ日時 | 2026-03-06 22:51:51 |
| 合計ジャッジ時間 | 3,223 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 17 |
ソースコード
#include <iostream>
#include <iomanip>
#include <cassert>
#include <vector>
#include <algorithm>
#include <utility>
#include <numeric>
#include <tuple>
#include <ranges>
#include <random>
// #include "Src/Number/IntegerDivision.hpp"
// #include "Src/Utility/BinarySearch.hpp"
// #include "Src/Sequence/CompressedSequence.hpp"
// #include "Src/Sequence/RunLengthEncoding.hpp"
// #include "Src/Algebra/Group/AdditiveGroup.hpp"
// #include "Src/DataStructure/FenwickTree/FenwickTree.hpp"
// #include "Src/DataStructure/SegmentTree/SegmentTree.hpp"
// #include "Src/DataStructure/DisjointSetUnion/DisjointSetUnion.hpp"
// #include "Src/DataStructure/Heap/BinaryHeap.hpp"
#include <cstdint>
#include <cstddef>
namespace zawa {
using i16 = std::int16_t;
using i32 = std::int32_t;
using i64 = std::int64_t;
using i128 = __int128_t;
using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
using usize = std::size_t;
} // namespace zawa
#include <iterator>
#include <type_traits>
namespace zawa {
template <class InputIterator>
u64 InversionNumber(InputIterator first, InputIterator last) {
assert((usize)std::distance(first, last) >= usize{0});
std::vector<std::remove_reference_t<decltype(*first)>> A(first, last);
auto rec{[&](auto rec, usize L, usize R) -> u64 {
if (R - L <= usize{1}) return 0;
usize M{(L + R) >> 1};
u64 res{rec(rec, L, M) + rec(rec, M, R)};
std::vector<u64> tmp(R - L);
usize i{L}, j{M}, k{};
while (i < M and j < R) {
if (A[i] <= A[j]) {
tmp[k++] = A[i++];
}
else {
res += M - i;
tmp[k++] = A[j++];
}
}
while (i < M) tmp[k++] = A[i++];
while (j < R) tmp[k++] = A[j++];
for (usize l{L} ; l < R ; l++) {
A[l] = tmp[l - L];
}
return res;
}};
return rec(rec, usize{0}, usize{A.size()});
}
template <class InputIterator>
bool InversionParity(InputIterator first, InputIterator last) {
assert((usize)std::distance(first, last) >= usize{0});
std::vector<std::remove_reference_t<decltype(*first)>> A(first, last);
auto rec{[&](auto rec, usize L, usize R) -> bool {
if (R - L <= usize{1}) return 0;
usize M{(L + R) >> 1};
bool res{rec(rec, L, M) != rec(rec, M, R)};
std::vector<u64> tmp(R - L);
usize i{L}, j{M}, k{};
while (i < M and j < R) {
if (A[i] <= A[j]) {
tmp[k++] = A[i++];
}
else {
res ^= (M - i) & 1;
tmp[k++] = A[j++];
}
}
while (i < M) tmp[k++] = A[i++];
while (j < R) tmp[k++] = A[j++];
for (usize l{L} ; l < R ; l++) {
A[l] = tmp[l - L];
}
return res;
}};
return rec(rec, usize{0}, usize{A.size()});
}
} // namespace zawa
namespace zawa {}
using namespace zawa;
// #include "atcoder/modint"
// using mint = atcoder::modint998244353;
// #include <array>
// #include <bit>
// #include <bitset>
// #include <climits>
// #include <cmath>
// #include <set>
// #include <unordered_set>
// #include <map>
// #include <unordered_map>
// #include <optional>
// #include <queue>
// #include <stack>
// #include <deque>
// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
using namespace std;
template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
os << '(' << p.first << ',' << p.second << ')';
return os;
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
for (int i = 0 ; i < ssize(v) ; i++)
os << v[i] << (i + 1 == ssize(v) ? "" : " ");
return os;
}
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
cout << fixed << setprecision(20);
#if !defined DEBUG
int N;
cin >> N;
vector<int> A(N);
for (auto& x : A)
cin >> x;
cout << InversionNumber(A.begin(),A.end()) << '\n';
#else
mt19937_64 mt{random_device{}()};
for (int testcase = 0 ; ; ) {
cerr << "----------" << ++testcase << "----------" << endl;
auto a = solve(), b = naive();
if (a != b) {
// print testcase
cerr << "you: " << a << endl;
cout << "correct: " << b << endl;
exit(0);
}
}
#endif
}
zawakasu