#pragma GCC optimize("Ofast") ///////////////////////////////////////////////////////// /** * This code should require C++14. * However, it's only been tested with C++17. */ #include using namespace std; /* macro helpers */ #define __NARGS(...) std::tuple_size::value #define __DECOMPOSE_S(a, x) auto x = a; #define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a; constexpr void __() {} #define __AS_PROCEDURE(...) __(); __VA_ARGS__; __() #define __as_typeof(container) remove_reference::type /* type aliases */ #if LONG_LONG_MAX != INT64_MAX using ll = int64_t; using ull = uint64_t; #else using ll = long long; using ull = unsigned long long; using ld = long double; #endif using int128 = __int128_t; using uint128 = __uint128_t; using ld = long double; using pii = pair; using pil = pair; using pli = pair; using pll = pair; using pid = pair; using pdi = pair; using pld = pair; using pdl = pair; using pdd = pair; using tlll = tuple; using tlld = tuple; using tlli = tuple; using tldl = tuple; using tldd = tuple; using tldi = tuple; using tlil = tuple; using tlid = tuple; using tlii = tuple; using tdll = tuple; using tdld = tuple; using tdli = tuple; using tddl = tuple; using tddd = tuple; using tddi = tuple; using tdil = tuple; using tdid = tuple; using tdii = tuple; using till = tuple; using tild = tuple; using tili = tuple; using tidl = tuple; using tidd = tuple; using tidi = tuple; using tiil = tuple; using tiid = tuple; using tiii = tuple; template using max_heap = priority_queue; template using min_heap = priority_queue, greater<>>; template using oi = ostream_iterator; template using ii = istream_iterator; /* constants */ constexpr int INF = 0x3f3f3f3f; constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL; constexpr ll MDL = 1e9 + 7; constexpr ll PRIME = 998'244'353; constexpr ll MDL1 = 8784491; constexpr ll MDL2 = PRIME; constexpr int128 INT128_MAX = numeric_limits::max(); constexpr uint128 UINT128_MAX = numeric_limits::max(); constexpr int128 INT128_MIN = numeric_limits::min(); constexpr uint128 UINT128_MIN = numeric_limits::min(); /* random */ mt19937 rd(chrono::duration_cast(chrono::system_clock::now().time_since_epoch()).count()); /* bit-wise operations */ #define lowbit(x) ((x) & -(x)) #define popcount(x) (__builtin_popcountll(ll(x))) #define parity(x) (__builtin_parityll(ll(x))) #define msp(x) (63LL - __builtin_clzll(ll(x))) #define lsp(x) (__builtin_ctzll(ll(x))) /* arithmetic operations */ #define mod(x, y) ((((x) % (y)) + (y)) % (y)) /* fast pairs */ #define upair ull #define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1))) #define u1(p) ((p) >> 32) #define u2(p) ((p) & ((1ULL << 32) - 1)) #define ult std::less #define ugt std::greater #define ipair ull #define imake(x, y) (umake(x, y)) #define i1(p) (int(u1(ll(p)))) #define i2(p) (ll(u2(p) << 32) >> 32) struct ilt { bool operator()(const ipair& a, const ipair& b) const { if (i1(a) == i1(b)) return i2(a) < i2(b); else return i1(a) < i1(b); } }; struct igt { bool operator()(const ipair& a, const ipair& b) const { if (i1(a) == i1(b)) return i2(a) > i2(b); else return i1(a) > i1(b); } }; /* conditions */ #define loop while (1) #define if_or(var, val) if (!(var == val)) var = val; else #define continue_or(var, val) __AS_PROCEDURE(if (var == val) continue; var = val;) #define break_or(var, val) __AS_PROCEDURE(if (var == val) break; var = val;) /* hash */ struct safe_hash { // https://codeforces.com/blog/entry/62393 static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; struct pair_hash { template size_t operator()(const pair& a) const { auto hash1 = safe_hash()(a.first); auto hash2 = safe_hash()(a.second); if (hash1 != hash2) { return hash1 ^ hash2; } return hash1; } }; uniform_int_distribution dist(PRIME); const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd); struct array_hash { template size_t operator()(const Sequence& arr) const { size_t pw1 = 1, pw2 = 1; size_t res1 = 0, res2 = 0; for (auto&& x : arr) { res1 = (res1 + x * pw1) % __array_hash_mdl1; res2 = (res2 + x * pw2) % __array_hash_mdl2; pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1; pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2; } return res1 + res2; } }; /* build data structures */ #define faster(um) __AS_PROCEDURE((um).reserve(1024); (um).max_load_factor(0.25);) #define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];) #define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];) #define pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);) #define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};) #define adj(ch, n) __AS_PROCEDURE(vector> ch((n) + 1);) #define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);) #define edgew(ch, u, v, w) __AS_PROCEDURE(ch[u].emplace_back(v, w), ch[v].emplace_back(u, w);) #define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);) #define Edgew(ch, u, v, w) __AS_PROCEDURE(ch[u].emplace_back(v, w);) template pair> discretize(Iterator __first, Iterator __last) { set st(__first, __last); size_t N = 0; map mp; for (auto&& x : st) mp[x] = ++N; return {N, mp}; } template pair> unordered_discretize(Iterator __first, Iterator __last) { set st(__first, __last); size_t N = 0; unordered_map mp; for (auto&& x : st) mp[x] = ++N; return {N, mp}; } /* io */ #define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL)) template void __read(T& x) { cin >> x; } template void __read(T& x, U&... args) { cin >> x; __read(args...); } #define read(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);) #define readvec(type, a, n) __AS_PROCEDURE(vector a(n); for (auto& x : a) cin >> x;) #define readvec1(type, a, n) __AS_PROCEDURE(vector a((n) + 1); copy_n(ii(cin), (n), a.begin() + 1);) #define putvec(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) #define putvec1(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;) #define putvec_eol(a) __AS_PROCEDURE(copy(a.begin(), a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) #define putvec1_eol(a) __AS_PROCEDURE(copy(a.begin() + 1, a.end(), oi<__as_typeof(a)::value_type>(cout, "\n"));) #define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;) #define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;) template istream& operator>>(istream& in, pair& p) { return in >> p.first >> p.second; } template ostream& operator<<(ostream& out, const pair& p) { out << "{" << p.first << ", " << p.second << "}"; return out; } template void print_tuple_impl(std::basic_ostream& os, const Tuple& t, std::index_sequence) { using swallow = int[]; // guaranties left to right order (void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get(t)), 0)... }; } template decltype(auto) operator<<(std::basic_ostream& os, const std::tuple& t) { os << "{"; print_tuple_impl(os, t, std::index_sequence_for{}); return os << "}"; } template ostream& operator<<(ostream& out, const vector& vec) { for (auto&& i : vec) out << i << ' '; return out; } std::ostream& operator<<(std::ostream& dest, const int128& value) { // https://stackoverflow.com/a/25115163/23881100 std::ostream::sentry s( dest ); if ( s ) { uint128 tmp = value < 0 ? -value : value; char buffer[ 128 ]; char* d = std::end( buffer ); do { -- d; *d = "0123456789"[ tmp % 10 ]; tmp /= 10; } while ( tmp != 0 ); if ( value < 0 ) { -- d; *d = '-'; } int len = std::end( buffer ) - d; if ( dest.rdbuf()->sputn( d, len ) != len ) { dest.setstate( std::ios_base::badbit ); } } return dest; } /* pops */ #define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();) #define popback(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.back(); q.pop_back();) #define popfront(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.front();q.pop_front();) /* math */ template return_t qpow(ll b, ll p) { if (b == 0 and p != 0) return 0; if (p == 0) return 1; return_t half = qpow(b, p / 2); if (p % 2 == 1) return half * half * b; else return half * half; } #define comb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] / fact[k] / fact[(n) - (k)]) constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); } void __exgcd(ll a, ll b, ll& x, ll& y) { if (b == 0) { x = 1, y = 0; return; } __exgcd(b, a % b, y, x); y -= a / b * x; } ll inverse(ll a, ll b) { ll x, y; __exgcd(a, b, x, y); return mod(x, b); } vector> decompose(ll x) { // return (factor, count, factor ** count) vector> res; for (int i = 2; i * i <= x; i++) { if (x % i == 0) { int cnt = 0; ll pw = 1; while (x % i == 0) ++cnt, x /= i, pw *= i; res.emplace_back(i, cnt, pw); } } if (x != 1) { res.emplace_back(x, 1, x); } return res; } vector decompose_prime(int N) { // return (factor, count) vector result; for (int i = 2; i * i <= N; i++) { if (N % i == 0) { int cnt = 0; while (N % i == 0) N /= i, ++cnt; result.emplace_back(i, cnt); } } if (N != 1) { result.emplace_back(N, 1); } return result; } /* string algorithms */ vector calc_next(string t) { // pi function of t int n = (int)t.length(); vector pi(n); for (int i = 1; i < n; i++) { int j = pi[i - 1]; while (j > 0 && t[i] != t[j]) j = pi[j - 1]; if (t[i] == t[j]) j++; pi[i] = j; } return pi; } vector calc_z(string t) { // z function of t int m = t.length(); vector z; z.push_back(m); pair prev = {1, -1}; for (int i = 1; i < m; ++i) { if (z[i - prev.first] + i <= prev.second) { z.push_back(z[i - prev.first]); } else { int j = max(i, prev.second + 1); while (j < m && t[j] == t[j - i]) ++j; z.push_back(j - i); prev = {i, j - 1}; } } return z; } vector kmp(string s, string t) { // find all t in s string cur = t + '#' + s; int sz1 = s.size(), sz2 = t.size(); vector v; vector lps = calc_next(cur); for (int i = sz2 + 1; i <= sz1 + sz2; i++) { if (lps[i] == sz2) v.push_back(i - 2 * sz2); } return v; } int period(string s) { // find the length of shortest recurring period int n = s.length(); auto z = calc_z(s); for (int i = 1; i <= n / 2; ++i) { if (n % i == 0 && z[i] == n - i) { return i; } } return n; } /* modular arithmetic */ template struct MLL { ll val; MLL(ll v = 0) : val(mod(v, mdl)) {} MLL(const MLL& other) : val(other.val) {} friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); } friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); } friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * rhs.val, mdl); } friend MLL operator/(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * mod(inverse(rhs.val, mdl), mdl), mdl); } friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); } friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; } friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; } void operator+=(const MLL& rhs) { val = (*this + rhs).val; } void operator-=(const MLL& rhs) { val = (*this - rhs).val; } void operator*=(const MLL& rhs) { val = (*this * rhs).val; } void operator/=(const MLL& rhs) { val = (*this / rhs).val; } void operator%=(const MLL& rhs) { val = (*this % rhs).val; } }; struct MLLd { ll val, mdl; MLLd(ll mdl, ll v = 0) : mdl(mdl), val(mod(v, mdl)) {} MLLd(const MLLd& other) : mdl(other.mdl), val(other.val) {} friend MLLd operator+(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val + rhs.val, lhs.mdl)); } friend MLLd operator-(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val - rhs.val, lhs.mdl)); } friend MLLd operator*(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val * rhs.val, lhs.mdl)); } friend MLLd operator/(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val * mod(inverse(rhs.val, lhs.mdl), lhs.mdl), lhs.mdl)); } friend MLLd operator%(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val - (lhs / rhs).val, lhs.mdl)); } friend bool operator==(const MLLd& lhs, const MLLd& rhs) { return lhs.val == rhs.val; } friend bool operator!=(const MLLd& lhs, const MLLd& rhs) { return lhs.val != rhs.val; } void operator+=(const MLLd& rhs) { val = (*this + rhs).val; } void operator-=(const MLLd& rhs) { val = (*this - rhs).val; } void operator*=(const MLLd& rhs) { val = (*this * rhs).val; } void operator/=(const MLLd& rhs) { val = (*this / rhs).val; } void operator%=(const MLLd& rhs) { val = (*this % rhs).val; } }; template ostream& operator<<(ostream& out, const MLL& num) { return out << num.val; } ostream& operator<<(ostream& out, const MLLd& num) { return out << num.val; } template istream& operator>>(istream& in, MLL& num) { return in >> num.val; } istream& operator>>(istream& in, MLLd& num) { return in >> num.val; } // miscancellous #define functor(func) [&](auto&&... val) \ noexcept(noexcept(func(std::forward(val)...))) -> decltype(auto) \ {return func(std::forward(val)...);} template void sort_by_key(RandomIt first, RandomIt last, Func extractor) { std::sort(first, last, [&] (auto&& a, auto&& b) { return std::less<>()(extractor(a), extractor(b)); }); } template void sort_by_key(RandomIt first, RandomIt last, Func extractor, Compare comp) { std::sort(first, last, [&] (auto&& a, auto&& b) { return comp(extractor(a), extractor(b)); }); } template vector> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) { vector> res; auto a_it = a_first; auto b_it = b_first; for (; not (a_it == a_last) and not (b_it == b_last); ++a_it, ++b_it) { res.emplace_back(*a_it, *b_it); } return res; } template vector> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) { vector> res; if (n > 0) { res.emplace_back(*a_first, *b_first); for (size_t i = 1; i != n; ++i) { res.emplace_back(*++a_first, *++b_first); } } return res; } template class ArithmeticIterator : bidirectional_iterator_tag { public: using difference_type = ptrdiff_t; using value_type = T; private: value_type value; public: ArithmeticIterator(const T& value) : value(value) {} value_type operator*() const { return value; } ArithmeticIterator& operator++() { ++value; return *this; } ArithmeticIterator& operator--() { --value; return *this; } bool operator==(const ArithmeticIterator& rhs) const { return value == rhs.value; } }; template vector> enumerate(const vector& container) { return zip(ArithmeticIterator(0), ArithmeticIterator(INT_MAX), container.begin(), container.end()); } #define initarray(init, N) (__initarray::type, (N)>(init)) template array __initarray(const T& init) { array res; for (size_t i = 0; i < N; ++i) { res[i] = init; } return res; } ///////////////////////////////////////////////////////// // #define SINGLE_TEST_CASE // #define DUMP_TEST_CASE 7219 // #define TOT_TEST_CASE 10000 void dump() {} void dump_ignore() {} void prep() { } // @link https://www.acwing.com/file_system/file/content/whole/index/content/8807719/ // Everything starts from 1 namespace treap_link { template class prev { public: T operator()(const T& a) { return a - 1; } }; template class next { public: T operator()(const T& a) { return a + 1; } }; } template , typename Prev = treap_link::prev, typename Next = treap_link::next> class treap { #define lson fhq[u].l #define rson fhq[u].r private: using size_type = size_t; using value_type = T; using reference = value_type&; using const_reference = const reference; struct Node { int key; size_type l, r, size; value_type val; }; Compare __compare; Prev __prev; Next __next; vector fhq; size_type cnt, root; size_type x, y, z; size_type _size; void pushup(size_type u) { fhq[u].size = fhq[lson].size + fhq[rson].size + 1; } size_type node(value_type val) { if (cnt + 1 >= fhq.size()) { fhq.push_back({}); } fhq[++cnt].val = val; fhq[cnt].key = rand(); fhq[cnt].size = 1; return cnt; } void split(size_type u, value_type val, size_type &x, size_type &y) { if (!u) x = y = 0; else { if (!__compare(val, fhq[u].val)) x = u, split(rson, val, rson, y); else y = u, split(lson, val, x, lson); pushup(u); } } size_type merge(size_type x, size_type y) { if (!x || !y) return x + y; if (fhq[x].key <= fhq[y].key) { fhq[x].r = merge(fhq[x].r, y); pushup(x); return x; } else { fhq[y].l = merge(x, fhq[y].l); pushup(y); return y; } } value_type askNum(size_type u, size_type rank) { if (fhq[lson].size + 1 == rank) return fhq[u].val; if (fhq[lson].size >= rank) return askNum(lson, rank); else return askNum(rson, rank - fhq[lson].size - 1); } public: treap(Compare __compare = std::less(), Prev __prev = treap_link::prev(), Next __next = treap_link::next()) : fhq(1), cnt(0), root(0), _size(0), __compare(__compare), __prev(__prev), __next(__next) {} treap(size_type n, Compare __compare = std::less(), Prev __prev = treap_link::prev(), Next __next = treap_link::next()) : fhq(1), cnt(0), root(0), _size(0) { fhq.reserve(n + 1); } template> treap(_InputIterator __first, _InputIterator __last) : fhq(1), cnt(0), root(0), _size(0) { __try { for (; __first != __last; ++__first) { insert(*__first); } } __catch(...) { clear(); __throw_exception_again; } } void clear() { _size = root = cnt = 0; fill(fhq.begin(), fhq.end(), Node()); } void insert(value_type val) { ++_size; split(root, val, x, y); root = merge(merge(x, node(val)), y); } void remove(value_type val) { assert(contains(val)); --_size; split(root, val, x, z); split(x, __prev(val), x, y); y = merge(fhq[y].l, fhq[y].r); root = merge(merge(x, y), z); } size_type index_of(value_type val) { split(root, __prev(val), x, y); size_type res = fhq[x].size + 1; root = merge(x, y); return res; } value_type at(size_type rank) { assert(rank > 0 && rank <= _size); return askNum(root, rank); } value_type prev_element(value_type val) { split(root, __prev(val), x, y); size_type u = x; while (rson) u = rson; root = merge(x, y); return fhq[u].val; } value_type next_element(value_type val) { split(root, val, x, y); size_type u = y; while (lson) u = lson; root = merge(x, y); return fhq[u].val; } bool contains(value_type val) { size_type idx = index_of(val); return idx <= _size && at(idx) == val; } size_type size() { return _size; } #undef lson #undef rson }; template struct BIT { int n; vector c; BIT(size_t n) : n(n), c(n + 1) {} void add(size_t i, const T& k) { while (i <= n) { c[i] += k; i += lowbit(i); } } T getsum(size_t i) { T res = {}; while (i) { res += c[i]; i -= lowbit(i); } return res; } }; void solve() { read(int, n, m); readvec(int, a, n); readvec(int, b, m); set> st; treap tr; for (int i = 0; i < n; ++i) { tr.insert(a[i]); st.emplace(tr.at(i / 2 + 1), i + 1); } st.emplace(0, 0); sort(b.begin(), b.end(), greater()); vector> seq(n + 1); // optimal placement for (int i = 0; i < m; ++i) { while (st.size() and st.begin()->first > b[i]) { st.erase(st.begin()); } debug(st.begin()->second); seq[st.begin()->second].emplace_back(b[i]); } vector c; sort(seq[0].begin(), seq[0].end()); c.insert(c.end(), seq[0].begin(), seq[0].end()); for (int i = 1; i <= n; ++i) { c.emplace_back(a[i - 1]); sort(seq[i].begin(), seq[i].end()); c.insert(c.end(), seq[i].begin(), seq[i].end()); } // calculate the final result auto [N, M] = unordered_discretize(c.begin(), c.end()); BIT bt(N); int res = 0; for (int i = 0; i < n + m; ++i) { res += bt.getsum(N) - bt.getsum(M[c[i]]); bt.add(M[c[i]], 1); } cout << res << '\n'; } int main() { #if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__) assert(false && "incompatible compiler variant detected."); #endif untie; prep(); #ifdef SINGLE_TEST_CASE solve(); #else read(int, t); for (int i = 0; i < t; ++i) { #ifdef DUMP_TEST_CASE if (t != (TOT_TEST_CASE)) { solve(); } else if (i + 1 == (DUMP_TEST_CASE)) { dump(); } else { dump_ignore(); } #else solve(); #endif } #endif }