跳到正文

剪贴板

剪贴板 wlumsdxx

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#ifndef SPN_SET_H
#define SPN_SET_H

#include <vector>
#include <utility>
#include <algorithm>
#include <stdexcept>
#include <functional>

template <class T, class Cmp, const bool Diff>
class _spn_set_base {
private:
    static constexpr size_t     _load = 150;
    Cmp                         cmp;
    size_t                      _size;
    std::vector<std::vector<T>> lists;
    std::vector<T>              maxes;
    size_t*                     index;

    void build                      ()               noexcept;
    std::pair<size_t, size_t> pos   (size_t)         noexcept;
    size_t                    loc   (size_t, size_t) noexcept;
    void                      split (size_t)         noexcept;
    void                      merge (size_t)         noexcept;
public:
    _spn_set_base() : cmp(), _size(0), index(nullptr) {}
    template <class It> _spn_set_base(const It& _begin, const It& _end)
    : cmp(), _size(0), index(nullptr) {extend(_begin, _end);}
    template <class It>
    void build_from_sorted(const It& _begin, const It& _end) noexcept;

    size_t size          ()                     const noexcept;
    bool   empty         ()                     const noexcept;
    void   clear         ()                     noexcept;
    void   shrink_to_fit ()                     noexcept;
    template <class It>
    void   extend        (const It&, const It&)         ;
    bool   insert        (const T&)                     ;
    bool   erase         (const T&)                     ;
    T      kth           (size_t)                       ;
    T      operator[]    (size_t)                       ;
    T      back          ()                        const;
    size_t lower_bound   (const T&)             noexcept;
    size_t upper_bound   (const T&)             noexcept;
    size_t count         (const T&)             noexcept;
    bool   contains      (const T&)             noexcept;
    T      precursor     (const T&)                     ;
    T      successor     (const T&)                     ;
};

template <class T, class Cmp, bool Diff> template <class It>
void _spn_set_base<T, Cmp, Diff>::build_from_sorted(const It& _begin, const It& _end) noexcept {
    clear(), _size = std::distance(_begin, _end);
    for (size_t i = 0; i < _size; i += _load) {
        lists.emplace_back(_begin + i, _begin + std::min(_size, i + _load));
        maxes.emplace_back(lists.back().back());
    }
};

template <class T, class Cmp, bool Diff>
void _spn_set_base<T, Cmp, Diff>::build() noexcept {
    size_t n = lists.size();
    index = new size_t[n];
    std::fill(index, index + n, 0);
    for (size_t i = 1; i < n; i++) {
        index[i] += lists[i - 1].size();
        if (i + (i & -i) < n) index[i + (i & -i)] += index[i];
    }
}
template <class T, class Cmp, bool Diff>
std::pair<size_t, size_t> _spn_set_base<T, Cmp, Diff>::pos(size_t idx) noexcept {
    if (idx < lists[0].size()) return {0, idx};
    if (!index) build();
    size_t n = lists.size(), p = 0;
    for (size_t i = std::__lg(n); i != (size_t) -1; i--) {
        size_t x = p + (1 << i);
        if (x < n && idx >= index[x]) idx -= index[x], p = x;
    }
    return {p, idx};
}
template <class T, class Cmp, bool Diff>
size_t _spn_set_base<T, Cmp, Diff>::loc(size_t pos, size_t idx) noexcept {
    if (pos == 0) return idx;
    if (!index) build();
    while (pos > 0) idx += index[pos], pos &= pos - 1;
    return idx;
}

template <class T, class Cmp, bool Diff>
size_t _spn_set_base<T, Cmp, Diff>::size() const noexcept {return _size;}
template <class T, class Cmp, bool Diff>
bool _spn_set_base<T, Cmp, Diff>::empty() const noexcept {return maxes.empty();}
template <class T, class Cmp, bool Diff>
void _spn_set_base<T, Cmp, Diff>::clear() noexcept
{_size = 0, lists.clear(), maxes.clear(), index = nullptr;}
template <class T, class Cmp, bool Diff>
void _spn_set_base<T, Cmp, Diff>::shrink_to_fit() noexcept
{lists.shrink_to_fit(), maxes.shrink_to_fit();}

template <class T, class Cmp, bool Diff>
bool _spn_set_base<T, Cmp, Diff>::insert(const T& val) {
    if (maxes.empty()) {
        lists.emplace_back(1, val), maxes.emplace_back(val), _size++;
        return true;
    }
    size_t pos = std::upper_bound(maxes.begin(), maxes.end(), val, cmp) - maxes.begin();
    if (pos == maxes.size()) {
        pos--, lists[pos].emplace_back(val), maxes[pos] = val;
    } else {
        auto it = std::upper_bound(lists[pos].begin(), lists[pos].end(), val, cmp);
        if (Diff && it != lists[pos].begin()) {
            T val0 = *std::prev(it);
            if (!cmp(val, val0) && !cmp(val0, val)) return false;
        }
        lists[pos].insert(it, val);
    }
    _size++, split(pos);
    return true;
}
template <class T, class Cmp, bool Diff>
void _spn_set_base<T, Cmp, Diff>::split(size_t pos) noexcept {
    if (lists[pos].size() > (_load << 1)) {
        std::vector<T> half(lists[pos].begin() + _load, lists[pos].end());
        lists[pos].erase(lists[pos].begin() + _load, lists[pos].end());
        maxes[pos] = lists[pos].back();
        lists.insert(lists.begin() + pos + 1, half);
        maxes.insert(maxes.begin() + pos + 1, half.back());
        index = nullptr;
    } else if (index) {
        size_t n = lists.size();
        for (size_t i = pos + 1; i < n; i += (i & -i)) index[i]++;
    }
}
template <class T, class Cmp, bool Diff> template <class It>
void _spn_set_base<T, Cmp, Diff>::extend(const It& _begin, const It& _end) {
    ptrdiff_t len = std::distance(_begin, _end);
    if ((len << 2) < (ptrdiff_t) _size) {
        for (It it = _begin; it != _end; it++) insert(*it);
        return;
    }
    std::vector<T> a(_begin, _end);
    for (const std::vector<T>& vec : lists) a.insert(a.end(), vec.begin(), vec.end());
    std::sort(a.begin(), a.end(), cmp);
    clear(), _size = a.size();
    for (size_t i = 0; i < _size; i += _load) {
        lists.emplace_back(a.begin() + i, a.begin() + std::min(_size, i + _load));
        maxes.emplace_back(lists.back().back());
    }
}

template <class T, class Cmp, bool Diff>
bool _spn_set_base<T, Cmp, Diff>::erase(const T& val) {
    if (maxes.empty()) return false;
    size_t pos = std::lower_bound(maxes.begin(), maxes.end(), val, cmp) - maxes.begin();
    if (pos == maxes.size()) return false;
    size_t idx = std::lower_bound(lists[pos].begin(), lists[pos].end(), val, cmp) - lists[pos].begin();
    if (cmp(lists[pos][idx], val) || cmp(val, lists[pos][idx])) return false;

    lists[pos].erase(lists[pos].begin() + idx), _size--;
    size_t n = lists[pos].size();
    if (n > (_load >> 1)) {
        maxes[pos] = lists[pos].back();
        if (index) {
            size_t n = lists.size();
            for (size_t i = pos + 1; i < n; i += (i & -i)) index[i]--;
        }
        return true;
    }
    if (lists.size() > 1) {
        if (pos == 0) pos++;
        merge(pos), split(pos - 1);
        return true;
    }
    if (n > 0) maxes[pos] = lists[pos].back();
    else lists.erase(lists.begin() + pos), maxes.erase(maxes.begin() + pos), index = nullptr;
    return true;
}
template <class T, class Cmp, bool Diff>
void _spn_set_base<T, Cmp, Diff>::merge(size_t pos) noexcept {
    lists[pos - 1].insert(lists[pos - 1].end(), lists[pos].begin(), lists[pos].end());
    maxes[pos - 1] = lists[pos - 1].back();
    lists.erase(lists.begin() + pos), maxes.erase(maxes.begin() + pos), index = nullptr;
}

template <class T, class Cmp, bool Diff>
T _spn_set_base<T, Cmp, Diff>::kth(size_t k) {
    // if (k >= _size) throw std::out_of_range("index out of range");
    const auto& [a, b] = pos(k);
    return lists[a][b];
}
template <class T, class Cmp, bool Diff>
T _spn_set_base<T, Cmp, Diff>::operator[] (size_t k) {return kth(k);}
template <class T, class Cmp, bool Diff>
T _spn_set_base<T, Cmp, Diff>::back() const {return maxes.back();}

template <class T, class Cmp, bool Diff>
size_t _spn_set_base<T, Cmp, Diff>::lower_bound(const T& val) noexcept {
    if (maxes.empty()) return 0;
    size_t pos = std::lower_bound(maxes.begin(), maxes.end(), val, cmp) - maxes.begin();
    if (pos == maxes.size()) return _size;
    return loc(pos, std::lower_bound(lists[pos].begin(), lists[pos].end(), val, cmp) - lists[pos].begin());
}
template <class T, class Cmp, bool Diff>
size_t _spn_set_base<T, Cmp, Diff>::upper_bound(const T& val) noexcept {
    if (maxes.empty()) return 0;
    size_t pos = std::upper_bound(maxes.begin(), maxes.end(), val, cmp) - maxes.begin();
    if (pos == maxes.size()) return _size;
    return loc(pos, std::upper_bound(lists[pos].begin(), lists[pos].end(), val, cmp) - lists[pos].begin());
}
template <class T, class Cmp, bool Diff>
bool _spn_set_base<T, Cmp, Diff>::contains(const T& val) noexcept {
    if (maxes.empty()) return false;
    size_t pos = std::lower_bound(maxes.begin(), maxes.end(), val, cmp) - maxes.begin();
    if (pos == maxes.size()) return false;
    size_t idx = std::lower_bound(lists[pos].begin(), lists[pos].end(), val, cmp) - lists[pos].begin();
    return !cmp(val, lists[pos][idx]) && !cmp(lists[pos][idx], val);
}
template <class T, class Cmp, bool Diff>
size_t _spn_set_base<T, Cmp, Diff>::count(const T& val) noexcept {
    if (Diff) return contains(val);
    if (maxes.empty()) return 0;
    size_t l = std::lower_bound(maxes.begin(), maxes.end(), val, cmp) - maxes.begin();
    if (l == maxes.size()) return 0;
    size_t r = std::upper_bound(maxes.begin(), maxes.end(), val, cmp) - maxes.begin();
    size_t x = std::lower_bound(lists[l].begin(), lists[l].end(), val, cmp) - lists[l].begin();
    if (r == maxes.size()) return _size - loc(l, x);
    size_t y = std::upper_bound(lists[r].begin(), lists[r].end(), val, cmp) - lists[r].begin();
    if (l == r) return y - x;
    return loc(r, y) - loc(l, x);
}

template <class T, class Cmp, bool Diff>
T _spn_set_base<T, Cmp, Diff>::precursor(const T& val) {
    if (cmp(maxes.back(), val)) return maxes.back();
    size_t pos = std::lower_bound(maxes.begin(), maxes.end(), val, cmp) - maxes.begin();
    // if (pos == maxes.size()) throw std::out_of_range("precursor not found");
    auto it = std::lower_bound(lists[pos].begin(), lists[pos].end(), val, cmp);
    if (it != lists[pos].begin()) return *std::prev(it);
    // if (pos == 0) throw std::out_of_range("precursor not found");
    return maxes[pos - 1];
}
template <class T, class Cmp, bool Diff>
T _spn_set_base<T, Cmp, Diff>::successor(const T& val){
    size_t pos = std::upper_bound(maxes.begin(), maxes.end(), val, cmp) - maxes.begin();
    // if (pos == maxes.size()) throw std::out_of_range("successor not found");
    auto it = std::upper_bound(lists[pos].begin(), lists[pos].end(), val, cmp);
    // if (it == lists[pos].end()) throw std::out_of_range("successor not found");
    return *it;
}

template <class T, class Cmp = std::less<T>>
using spn_set = _spn_set_base<T, Cmp, true>;
template <class T, class Cmp = std::less<T>>
using spn_multiset = _spn_set_base<T, Cmp, false>;

#endif //SPN_SET_H

#include <bits/stdc++.h>
#include <bits/extc++.h>
#define gcd __gcd
#define pbds __gnu_pbds
#define popcount __builtin_popcountll
#define seed chrono::steady_clock::now().time_since_epoch().count
using namespace std;
using u16=unsigned short;
using i16=short;
using u32=unsigned int;
using u64=unsigned long long;
using i64=long long;
#ifdef __SIZEOF_INT128__
using u128=unsigned __int128;
using i128=__int128;
#else
using u128=unsigned long long;
using i128=long long;
#endif
struct hash64
{static constexpr u64 _s(u64 x)
{return x+=0x9e3779b97f4a7c15,x=(x^(x>>30))*0xbf58476d1ce4e5b9,x=(x^(x>>27))*0x94d049bb133111eb,x^(x>>31);}
size_t operator()(u64 x)const{static const u64 f=seed();return _s(x+f);}};
template<class K,class V>using umap=pbds::gp_hash_table<K,V,hash64>;
template<class T,class C=less<T>>using heap=pbds::priority_queue<T,C>;
template<class T,class C=less<T>>using bst=pbds::tree<T,pbds::null_type,C,pbds::rb_tree_tag,pbds::tree_order_statistics_node_update>;
template<class T>constexpr T lowbit(T x){return x&-x;}
template<class T>constexpr T lg(T x){return x?__lg(x):0;}
template<class T>constexpr T ctz(T x){return x?__builtin_ctzll(x):0;}
constexpr u32 popcount128(u128 x){return popcount((u64)x)+popcount((u64)(x>>64));}
constexpr u32 ctz128(u128 x){return x?(u64)x?ctz((u64)x):64+ctz((u64)(x>>64)):0;}
template<class T>constexpr void chkmax(T&x,T y){x=max(x,y);}
template<class T>constexpr void chkmin(T&x,T y){x=min(x,y);}
template<class T1,class T2>ostream&operator<<(ostream&out,const pair<T1,T2>&p)
{return out<<"("<<p.first<<", "<<p.second<<")";}
template<class T1,class T2>istream&operator>>(istream&in,pair<T1,T2>&p)
{return in>>p.first>>p.second;}
template<class T,typename=decltype(declval<T>().begin()),typename=enable_if_t<!is_same<decay_t<T>,string>::value>>
ostream&operator<<(ostream&out,const T&v){int n=v.size();out<<"[";for(auto&x:v)out<<x<<(--n?", ":"");return out<<"]";}
mt19937 rand32(seed());
mt19937_64 rand64(seed());
#undef assert
#ifdef SPN_LOCAL
#define assert(x) (x)?void():(cerr<<"Assertion Failed On Line "<<__LINE__<<endl,exit(0))
void _o(auto&&...x){int f=1;((cerr<<(f?"":", ")<<x,f=0),...);}
#define mdebug(...) (cerr<<"On Line "<<__LINE__<<": ["<<#__VA_ARGS__<<"] = {",_o(__VA_ARGS__),cerr<<"}\n")
#define debug(x) (cerr<<"On Line "<<__LINE__<<": "<<#x<<" = "<<(x)<<endl)
#define FILE(x) void()
#define FLOCAL(x) freopen(x".in","r",stdin),freopen(x".out","w",stdout)
#define FDEBUG(x) freopen(x".log","w",stderr)
#else
#define assert(x) void()
#define mdebug(...) void()
#define debug(x) void()
#define FILE(x) freopen(x".in","r",stdin),freopen(x".out","w",stdout)
#define FLOCAL(x) void()
#define FDEBUG(x) void()
#endif //SPN_LOCAL

namespace FastIO {
#ifndef SPN_LOCAL
	const int SZ = 1 << 18; char *p1, *p2, buf[SZ];
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, SZ, stdin), p1 == p2) ? EOF : *p1++)
#endif
    inline void skip() {for (char ch = getchar(); ch == ' ' || ch == '\n' || ch == '\t'; ch = getchar());}
	int t; char ch;
	template <class T> inline void read(T& x) {x = 0, t = 1, ch = getchar(); for (; ch < '0' || ch > '9'; ch = getchar()) if (ch == '-') t = -1; for (; '0' <= ch && ch <= '9'; ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48); x *= t;}
	inline void read(char& x) {skip(), x = getchar();}
	template <class T, class... Args> inline void read(T& t, Args&... args) {read(t), read(args...);}
	template <class It> inline void read(const It& begin, const It& end) {for (It it = begin; it != end; it++) read(*it);}
	template <class T1, class T2> inline void read(pair<T1, T2>& p) {read(p.first), read(p.second);}
} using FastIO::read;

// #define int long long
const int N = 1e6 + 5;
int n, a[N];
using node = pair<int, int>;

int work() {
    spn_multiset<node> q;
    for (int i = 1; i <= n; i++) q.insert(node(a[i], i));
    while (q.size() > 1) {
        node mn = q[0], mx = q.back();
        q.erase(mn), q.erase(mx);
        node nxt = {mx.first - mn.first, mx.second};
        if (q.empty() || nxt > q[0]) {
            q.insert(nxt);
            continue;
        }
        int res = q.size() + 2, cnt = 1;
        while (!q.empty()) {
            node u = q.back(); q.erase(u), cnt++;
            node v = {u.first - nxt.first, u.second};
            if (!q.empty() && v > q[0]) break;
            nxt = v;
        }
        if (cnt & 1) return res - 1;
        return res;
    } return 1;
}

void _main() {
    int T; read(T);
    for (int i = 1, k, x, y; i <= T; i++) {
        if (i == 1) read(n), read(a + 1, a + n + 1);
        else for (read(k); k--; ) read(x, y), a[x] = y;
        cout << work() << '\n';
    }
} signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr), cerr.tie(nullptr);

    int t = 1; for (/*cin >> t*/; t--; ) _main();
    return 0;
}