firiexp's Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub firiexp/library

:heavy_check_mark: test/yosupo_aplusb_top_kth.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"

#include <algorithm>
#include <cassert>
#include <numeric>
#include <random>
#include <vector>
using namespace std;

using ll = long long;

#include <cstdio>
#include <cstring>
#include <string>
#include <type_traits>

#include "../util/fastio.cpp"
#include "../datastructure/top_kth.cpp"

template<bool Largest>
ll brute_sum_k(const vector<ll>& a, int k) {
    if (k <= 0) return 0;
    vector<ll> b = a;
    sort(b.begin(), b.end());
    if constexpr (Largest) reverse(b.begin(), b.end());
    k = min(k, (int)b.size());
    return accumulate(b.begin(), b.begin() + k, 0LL);
}

template<bool Largest>
ll brute_kth(const vector<ll>& a, int k) {
    vector<ll> b = a;
    sort(b.begin(), b.end());
    if constexpr (Largest) reverse(b.begin(), b.end());
    return b[k - 1];
}

template<bool Largest>
void verify_one(const vector<ll>& cur, const TopKTreap<ll, ll, Largest>& ds) {
    assert(ds.size() == (int)cur.size());
    assert(ds.empty() == cur.empty());
    assert(ds.total_sum() == accumulate(cur.begin(), cur.end(), 0LL));
    for (ll x = -8; x <= 8; ++x) {
        int cnt = 0;
        for (ll y : cur) cnt += x == y;
        assert(ds.count(x) == cnt);
        assert(ds.contains(x) == (cnt > 0));
    }
    for (int k = 0; k <= (int)cur.size() + 2; ++k) {
        assert(ds.sum_k(k) == brute_sum_k<Largest>(cur, k));
    }
    if (!cur.empty()) {
        for (int k = 1; k <= (int)cur.size(); ++k) {
            assert(ds.kth(k) == brute_kth<Largest>(cur, k));
        }
    }
    if (ds.has_kth()) {
        assert(ds.kth() == brute_kth<Largest>(cur, ds.k()));
    } else {
        assert(ds.k() <= 0 || ds.k() > (int)cur.size());
    }
    assert(ds.sum_topk() == brute_sum_k<Largest>(cur, ds.k()));
}

void self_check() {
    mt19937 rng(0);
    for (int tc = 0; tc < 300; ++tc) {
        TopKTreap<ll, ll, true> largest(0, rng());
        TopKTreap<ll, ll, false> smallest(0, rng());
        largest.reserve(256);
        smallest.reserve(256);
        vector<ll> cur;

        for (int step = 0; step < 200; ++step) {
            int op = rng() % 4;
            if (op <= 1) {
                ll x = (int)(rng() % 17) - 8;
                largest.insert(x);
                smallest.insert(x);
                cur.push_back(x);
            } else if (op == 2) {
                ll x = (int)(rng() % 17) - 8;
                bool ok1 = largest.erase_one(x);
                bool ok2 = smallest.erase_one(x);
                auto it = find(cur.begin(), cur.end(), x);
                bool ok3 = it != cur.end();
                if (ok3) cur.erase(it);
                assert(ok1 == ok2);
                assert(ok2 == ok3);
            } else {
                int new_k = rng() % 12;
                largest.set_k(new_k);
                smallest.set_k(new_k);
            }
            verify_one<true>(cur, largest);
            verify_one<false>(cur, smallest);
        }
    }
}

int main() {
    self_check();

    Scanner sc;
    Printer pr;
    ll a, b;
    sc.read(a, b);
    pr.println(a + b);
    return 0;
}
#line 1 "test/yosupo_aplusb_top_kth.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"

#include <algorithm>
#include <cassert>
#include <numeric>
#include <random>
#include <vector>
using namespace std;

using ll = long long;

#include <cstdio>
#include <cstring>
#include <string>
#include <type_traits>

#line 1 "util/fastio.cpp"
using namespace std;

extern "C" int fileno(FILE *);
extern "C" int isatty(int);

template<class T, class = void>
struct is_fastio_range : false_type {};

template<class T>
struct is_fastio_range<T, void_t<decltype(declval<T &>().begin()), decltype(declval<T &>().end())>> : true_type {};

template<class T, class = void>
struct has_fastio_value : false_type {};

template<class T>
struct has_fastio_value<T, void_t<decltype(declval<const T &>().value())>> : true_type {};

struct FastIoDigitTable {
    char num[40000];

    constexpr FastIoDigitTable() : num() {
        for (int i = 0; i < 10000; ++i) {
            int x = i;
            for (int j = 3; j >= 0; --j) {
                num[i * 4 + j] = char('0' + x % 10);
                x /= 10;
            }
        }
    }
};

struct Scanner {
    static constexpr int BUFSIZE = 1 << 17;
    static constexpr int OFFSET = 64;
    char buf[BUFSIZE + 1];
    int idx, size;
    bool interactive;

    Scanner() : idx(0), size(0), interactive(isatty(fileno(stdin))) {}

    inline void load() {
        int len = size - idx;
        memmove(buf, buf + idx, len);
        if (interactive) {
            if (fgets(buf + len, BUFSIZE + 1 - len, stdin)) size = len + (int)strlen(buf + len);
            else size = len;
        } else {
            size = len + (int)fread(buf + len, 1, BUFSIZE - len, stdin);
        }
        idx = 0;
        buf[size] = 0;
    }

    inline void ensure() {
        if (idx + OFFSET > size) load();
    }

    inline void ensure_interactive() {
        if (idx == size) load();
    }

    inline char skip() {
        if (interactive) {
            ensure_interactive();
            while (buf[idx] && buf[idx] <= ' ') {
                ++idx;
                ensure_interactive();
            }
            return buf[idx++];
        }
        ensure();
        while (buf[idx] && buf[idx] <= ' ') {
            ++idx;
            ensure();
        }
        return buf[idx++];
    }

    template<class T, typename enable_if<is_integral<T>::value, int>::type = 0>
    void read(T &x) {
        if (interactive) {
            char c = skip();
            bool neg = false;
            if constexpr (is_signed<T>::value) {
                if (c == '-') {
                    neg = true;
                    ensure_interactive();
                    c = buf[idx++];
                }
            }
            x = 0;
            while (c >= '0') {
                x = x * 10 + (c & 15);
                ensure_interactive();
                c = buf[idx++];
            }
            if constexpr (is_signed<T>::value) {
                if (neg) x = -x;
            }
            return;
        }
        char c = skip();
        bool neg = false;
        if constexpr (is_signed<T>::value) {
            if (c == '-') {
                neg = true;
                c = buf[idx++];
            }
        }
        x = 0;
        while (c >= '0') {
            x = x * 10 + (c & 15);
            c = buf[idx++];
        }
        if constexpr (is_signed<T>::value) {
            if (neg) x = -x;
        }
    }

    template<class T, typename enable_if<!is_integral<T>::value && !is_fastio_range<T>::value && !is_same<typename decay<T>::type, string>::value && has_fastio_value<T>::value, int>::type = 0>
    void read(T &x) {
        long long v;
        read(v);
        x = T(v);
    }

    template<class Head, class Next, class... Tail>
    void read(Head &head, Next &next, Tail &...tail) {
        read(head);
        read(next, tail...);
    }

    template<class T, class U>
    void read(pair<T, U> &p) {
        read(p.first, p.second);
    }

    template<class T, typename enable_if<is_fastio_range<T>::value && !is_same<typename decay<T>::type, string>::value, int>::type = 0>
    void read(T &a) {
        for (auto &x : a) read(x);
    }

    void read(char &c) {
        c = skip();
    }

    void read(string &s) {
        s.clear();
        if (interactive) {
            ensure_interactive();
            while (buf[idx] && buf[idx] <= ' ') {
                ++idx;
                ensure_interactive();
            }
            while (true) {
                int start = idx;
                while (idx < size && buf[idx] > ' ') ++idx;
                s.append(buf + start, idx - start);
                if (idx < size) break;
                load();
                if (size == 0) break;
            }
            if (idx < size) ++idx;
            return;
        }
        ensure();
        while (buf[idx] && buf[idx] <= ' ') {
            ++idx;
            ensure();
        }
        while (true) {
            int start = idx;
            while (idx < size && buf[idx] > ' ') ++idx;
            s.append(buf + start, idx - start);
            if (idx < size) break;
            load();
        }
        if (idx < size) ++idx;
    }
};

struct Printer {
    static constexpr int BUFSIZE = 1 << 17;
    static constexpr int OFFSET = 64;
    char buf[BUFSIZE];
    int idx;
    bool interactive;
    inline static constexpr FastIoDigitTable table{};

    Printer() : idx(0), interactive(isatty(fileno(stdout))) {}
    ~Printer() { flush(); }

    inline void flush() {
        if (idx) {
            fwrite(buf, 1, idx, stdout);
            idx = 0;
        }
    }

    inline void pc(char c) {
        if (idx > BUFSIZE - OFFSET) flush();
        buf[idx++] = c;
        if (interactive && c == '\n') flush();
    }

    inline void print_range(const char *s, size_t n) {
        size_t pos = 0;
        while (pos < n) {
            if (idx == BUFSIZE) flush();
            size_t chunk = min(n - pos, (size_t)(BUFSIZE - idx));
            memcpy(buf + idx, s + pos, chunk);
            idx += (int)chunk;
            pos += chunk;
        }
    }

    void print(const char *s) {
        print_range(s, strlen(s));
    }

    void print(const string &s) {
        print_range(s.data(), s.size());
    }

    void print(char c) {
        pc(c);
    }

    void print(bool b) {
        pc(char('0' + (b ? 1 : 0)));
    }

    template<class T, typename enable_if<is_integral<T>::value && !is_same<T, bool>::value, int>::type = 0>
    void print(T x) {
        if (idx > BUFSIZE - 100) flush();
        using U = typename make_unsigned<T>::type;
        U y;
        if constexpr (is_signed<T>::value) {
            if (x < 0) {
                buf[idx++] = '-';
                y = U(0) - static_cast<U>(x);
            } else {
                y = static_cast<U>(x);
            }
        } else {
            y = x;
        }
        if (y == 0) {
            buf[idx++] = '0';
            return;
        }
        static constexpr int TMP_SIZE = sizeof(U) * 10 / 4;
        char tmp[TMP_SIZE];
        int pos = TMP_SIZE;
        while (y >= 10000) {
            pos -= 4;
            memcpy(tmp + pos, table.num + (y % 10000) * 4, 4);
            y /= 10000;
        }
        if (y >= 1000) {
            memcpy(buf + idx, table.num + (y << 2), 4);
            idx += 4;
        } else if (y >= 100) {
            memcpy(buf + idx, table.num + (y << 2) + 1, 3);
            idx += 3;
        } else if (y >= 10) {
            unsigned q = (unsigned(y) * 205) >> 11;
            buf[idx] = char('0' + q);
            buf[idx + 1] = char('0' + (unsigned(y) - q * 10));
            idx += 2;
        } else {
            buf[idx++] = char('0' + y);
        }
        memcpy(buf + idx, tmp + pos, TMP_SIZE - pos);
        idx += TMP_SIZE - pos;
    }

    template<class T, typename enable_if<!is_integral<T>::value && !is_fastio_range<T>::value && !is_same<typename decay<T>::type, string>::value && has_fastio_value<T>::value, int>::type = 0>
    void print(const T &x) {
        print(x.value());
    }

    template<class T, typename enable_if<is_fastio_range<T>::value && !is_same<typename decay<T>::type, string>::value, int>::type = 0>
    void print(const T &a) {
        bool first = true;
        for (auto &&x : a) {
            if (!first) pc(' ');
            first = false;
            print(x);
        }
    }

    template<class T>
    void println(const T &x) {
        print(x);
        pc('\n');
    }

    template<class Head, class... Tail>
    void println(const Head &head, const Tail &...tail) {
        print(head);
        ((pc(' '), print(tail)), ...);
        pc('\n');
    }

    void println() {
        pc('\n');
    }
};

template<class T>
Scanner &operator>>(Scanner &in, T &x) {
    in.read(x);
    return in;
}

template<class T>
Printer &operator<<(Printer &out, const T &x) {
    out.print(x);
    return out;
}

/**
 * @brief 高速入出力(Fast IO)
 */
#line 1 "datastructure/top_kth.cpp"
template<class T, class SumT = long long, bool Largest = true>
class TopKTreap {
private:
    struct Node {
        T key;
        int cnt;
        int sz;
        uint32_t pri;
        SumT sum;
        int l;
        int r;

        Node(const T& key_, uint32_t pri_)
            : key(key_), cnt(1), sz(1), pri(pri_), sum((SumT)key_), l(-1), r(-1) {}
    };

    int root_ = -1;
    int K_ = 0;
    uint32_t rng_ = 2463534242u;
    vector<Node> nodes_;
    vector<int> free_nodes_;

    static bool goes_left(const T& x, const T& key) {
        if constexpr (Largest) return x < key;
        else return x > key;
    }

    int size(int t) const {
        return t == -1 ? 0 : nodes_[t].sz;
    }

    SumT subtree_sum(int t) const {
        return t == -1 ? SumT(0) : nodes_[t].sum;
    }

    void pull(int t) {
        if (t == -1) return;
        Node& node = nodes_[t];
        node.sz = node.cnt + size(node.l) + size(node.r);
        node.sum = subtree_sum(node.l) + subtree_sum(node.r) + (SumT)node.key * node.cnt;
    }

    uint32_t next_rand() {
        uint32_t x = rng_;
        x ^= x << 13;
        x ^= x >> 17;
        x ^= x << 5;
        rng_ = x;
        return x;
    }

    int make_node(const T& x) {
        uint32_t pri = next_rand();
        if (!free_nodes_.empty()) {
            int idx = free_nodes_.back();
            free_nodes_.pop_back();
            nodes_[idx] = Node(x, pri);
            return idx;
        }
        nodes_.emplace_back(x, pri);
        return (int)nodes_.size() - 1;
    }

    void recycle_node(int t) {
        if (t != -1) free_nodes_.push_back(t);
    }

    void rotate_left(int& t) {
        int r = nodes_[t].r;
        nodes_[t].r = nodes_[r].l;
        nodes_[r].l = t;
        pull(t);
        pull(r);
        t = r;
    }

    void rotate_right(int& t) {
        int l = nodes_[t].l;
        nodes_[t].l = nodes_[l].r;
        nodes_[l].r = t;
        pull(t);
        pull(l);
        t = l;
    }

    void insert(int& t, const T& x) {
        if (t == -1) {
            t = make_node(x);
            return;
        }

        if (x == nodes_[t].key) {
            ++nodes_[t].cnt;
            pull(t);
            return;
        }

        if (goes_left(x, nodes_[t].key)) {
            int child = nodes_[t].l;
            insert(child, x);
            nodes_[t].l = child;
            if (nodes_[nodes_[t].l].pri > nodes_[t].pri) rotate_right(t);
        } else {
            int child = nodes_[t].r;
            insert(child, x);
            nodes_[t].r = child;
            if (nodes_[nodes_[t].r].pri > nodes_[t].pri) rotate_left(t);
        }
        pull(t);
    }

    bool erase_one(int& t, const T& x) {
        if (t == -1) return false;
        bool ok = false;
        if (goes_left(x, nodes_[t].key)) {
            int child = nodes_[t].l;
            ok = erase_one(child, x);
            nodes_[t].l = child;
        } else if (goes_left(nodes_[t].key, x)) {
            int child = nodes_[t].r;
            ok = erase_one(child, x);
            nodes_[t].r = child;
        } else {
            ok = true;
            if (nodes_[t].cnt > 1) {
                --nodes_[t].cnt;
                pull(t);
                return true;
            }
            if (nodes_[t].l == -1 || nodes_[t].r == -1) {
                int old = t;
                t = (nodes_[old].l != -1 ? nodes_[old].l : nodes_[old].r);
                recycle_node(old);
                return true;
            }
            if (nodes_[nodes_[t].l].pri > nodes_[nodes_[t].r].pri) {
                rotate_right(t);
                int child = nodes_[t].r;
                ok = erase_one(child, x);
                nodes_[t].r = child;
            } else {
                rotate_left(t);
                int child = nodes_[t].l;
                ok = erase_one(child, x);
                nodes_[t].l = child;
            }
        }

        if (t != -1) pull(t);
        return ok;
    }

public:
    explicit TopKTreap(int K = 0, uint32_t seed = 2463534242u)
        : root_(-1), K_(K), rng_(seed) {
        assert(K >= 0);
        if (rng_ == 0) rng_ = 2463534242u;
    }

    TopKTreap(const TopKTreap&) = delete;
    TopKTreap& operator=(const TopKTreap&) = delete;

    void reserve(int capacity) {
        assert(capacity >= 0);
        nodes_.reserve(capacity);
        free_nodes_.reserve(capacity);
    }

    int k() const {
        return K_;
    }

    void set_k(int new_k) {
        assert(new_k >= 0);
        K_ = new_k;
    }

    int size() const {
        return size(root_);
    }

    bool empty() const {
        return root_ == -1;
    }

    SumT total_sum() const {
        return subtree_sum(root_);
    }

    void insert(const T& x) {
        insert(root_, x);
    }

    bool erase_one(const T& x) {
        return erase_one(root_, x);
    }

    int count(const T& x) const {
        int t = root_;
        while (t != -1) {
            const Node& node = nodes_[t];
            if (x == node.key) return node.cnt;
            t = goes_left(x, node.key) ? node.l : node.r;
        }
        return 0;
    }

    bool contains(const T& x) const {
        return count(x) > 0;
    }

    T kth(int kth) const {
        assert(1 <= kth && kth <= size());

        int t = root_;
        while (t != -1) {
            const Node& node = nodes_[t];
            int preferred_sz = size(node.r);
            if (kth <= preferred_sz) {
                t = node.r;
            } else if (kth <= preferred_sz + node.cnt) {
                return node.key;
            } else {
                kth -= preferred_sz + node.cnt;
                t = node.l;
            }
        }

        assert(false);
        return T();
    }

    bool has_kth() const {
        return 1 <= K_ && K_ <= size();
    }

    T kth() const {
        assert(has_kth());
        return kth(K_);
    }

    SumT sum_k(int k) const {
        if (k <= 0 || root_ == -1) return SumT(0);
        if (k >= size()) return total_sum();
        SumT res = 0;
        int t = root_;
        while (t != -1 && k > 0) {
            const Node& node = nodes_[t];
            int preferred = node.r;
            int other = node.l;
            int preferred_sz = size(preferred);
            if (k <= preferred_sz) {
                t = preferred;
                continue;
            }
            res += subtree_sum(preferred);
            k -= preferred_sz;
            int take = std::min(k, node.cnt);
            res += (SumT)node.key * take;
            k -= take;
            if (k == 0) break;
            t = other;
        }
        return res;
    }

    SumT sum_topk() const {
        return sum_k(K_);
    }
};

/**
 * @brief 上位K個を管理するTreap
 */
#line 19 "test/yosupo_aplusb_top_kth.test.cpp"

template<bool Largest>
ll brute_sum_k(const vector<ll>& a, int k) {
    if (k <= 0) return 0;
    vector<ll> b = a;
    sort(b.begin(), b.end());
    if constexpr (Largest) reverse(b.begin(), b.end());
    k = min(k, (int)b.size());
    return accumulate(b.begin(), b.begin() + k, 0LL);
}

template<bool Largest>
ll brute_kth(const vector<ll>& a, int k) {
    vector<ll> b = a;
    sort(b.begin(), b.end());
    if constexpr (Largest) reverse(b.begin(), b.end());
    return b[k - 1];
}

template<bool Largest>
void verify_one(const vector<ll>& cur, const TopKTreap<ll, ll, Largest>& ds) {
    assert(ds.size() == (int)cur.size());
    assert(ds.empty() == cur.empty());
    assert(ds.total_sum() == accumulate(cur.begin(), cur.end(), 0LL));
    for (ll x = -8; x <= 8; ++x) {
        int cnt = 0;
        for (ll y : cur) cnt += x == y;
        assert(ds.count(x) == cnt);
        assert(ds.contains(x) == (cnt > 0));
    }
    for (int k = 0; k <= (int)cur.size() + 2; ++k) {
        assert(ds.sum_k(k) == brute_sum_k<Largest>(cur, k));
    }
    if (!cur.empty()) {
        for (int k = 1; k <= (int)cur.size(); ++k) {
            assert(ds.kth(k) == brute_kth<Largest>(cur, k));
        }
    }
    if (ds.has_kth()) {
        assert(ds.kth() == brute_kth<Largest>(cur, ds.k()));
    } else {
        assert(ds.k() <= 0 || ds.k() > (int)cur.size());
    }
    assert(ds.sum_topk() == brute_sum_k<Largest>(cur, ds.k()));
}

void self_check() {
    mt19937 rng(0);
    for (int tc = 0; tc < 300; ++tc) {
        TopKTreap<ll, ll, true> largest(0, rng());
        TopKTreap<ll, ll, false> smallest(0, rng());
        largest.reserve(256);
        smallest.reserve(256);
        vector<ll> cur;

        for (int step = 0; step < 200; ++step) {
            int op = rng() % 4;
            if (op <= 1) {
                ll x = (int)(rng() % 17) - 8;
                largest.insert(x);
                smallest.insert(x);
                cur.push_back(x);
            } else if (op == 2) {
                ll x = (int)(rng() % 17) - 8;
                bool ok1 = largest.erase_one(x);
                bool ok2 = smallest.erase_one(x);
                auto it = find(cur.begin(), cur.end(), x);
                bool ok3 = it != cur.end();
                if (ok3) cur.erase(it);
                assert(ok1 == ok2);
                assert(ok2 == ok3);
            } else {
                int new_k = rng() % 12;
                largest.set_k(new_k);
                smallest.set_k(new_k);
            }
            verify_one<true>(cur, largest);
            verify_one<false>(cur, smallest);
        }
    }
}

int main() {
    self_check();

    Scanner sc;
    Printer pr;
    ll a, b;
    sc.read(a, b);
    pr.println(a + b);
    return 0;
}
Back to top page