cpp-library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub shino16/cpp-library

:heavy_check_mark: test/bbst/implicit_treap.affine.test.cpp

Depends on

Code

// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/dynamic_sequence_range_affine_range_sum

#include <mod/modint.hpp>

#include "bbst/implicit_treap.hpp"
using mint = atcoder::modint998244353;

struct affine {
  mint a = 1, b = 0;
  void then(affine rhs) { *this = affine{a * rhs.a, b * rhs.a + rhs.b}; }
};

struct T {
  mint a, sum;
  affine f;
  bool rev;
};

int main() {
  auto prop = [](auto &n, auto &v) {
    if (v.f.a != 1 || v.f.b != 0) {
      v.a = v.a * v.f.a + v.f.b;
      v.sum = v.sum * v.f.a + v.f.b * n.cnt;
      if (n.l) n.l->val().f.then(v.f);
      if (n.r) n.r->val().f.then(v.f);
      v.f = affine{};
    }
    if (v.rev) {
      swap(n.l, n.r);
      if (n.l) n.l->val().rev ^= true;
      if (n.r) n.r->val().rev ^= true;
      v.rev = false;
    }
  };
  auto upd = [](auto &n, auto &v) {
    assert(v.f.a == 1 && v.f.b == 0);
    v.sum = v.a + (n.l ? n.l->val().sum : 0) + (n.r ? n.r->val().sum : 0);
  };

  int n, q;
  scanf("%d%d", &n, &q);
  vector<T> a(n);
  rep(i, n) {
    int x;
    scanf("%d", &x);
    a[i] = T{x, x, {}, false};
  }

  implicit_treap<T, decltype(upd), decltype(prop)> tree(all(a), upd, prop);

  while (q--) {
    int cmd;
    scanf("%d", &cmd);
    if (cmd == 0) {
      int i, x;
      scanf("%d%d", &i, &x);
      tree.insert(i, T{x, x, {}, false});
    } else if (cmd == 1) {
      int i;
      scanf("%d", &i);
      tree.erase(i);
    } else if (cmd == 2) {
      int l, r;
      scanf("%d%d", &l, &r);
      tree.apply_on(l, r, [](auto &v) { v.rev ^= true; });
    } else if (cmd == 3) {
      int l, r, b, c;
      scanf("%d%d%d%d", &l, &r, &b, &c);
      tree.apply_on(l, r, [=](auto &v) { v.f.then(affine{b, c}); });
    } else {
      int l, r;
      scanf("%d%d", &l, &r);
      mint ans;
      tree.apply_on(l, r, [&](auto &v) { ans = v.sum; });
      printf("%d\n", ans.val());
    }
  }
}
#line 1 "test/bbst/implicit_treap.affine.test.cpp"
// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/dynamic_sequence_range_affine_range_sum

#include <mod/modint.hpp>

#line 2 "bbst/implicit_treap.hpp"

#line 2 "prelude.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using vc = vector<char>;
#define rep2(i, m, n) for (auto i = (m); i < (n); i++)
#define rep(i, n) rep2(i, 0, n)
#define repr2(i, m, n) for (auto i = (n); i-- > (m);)
#define repr(i, n) repr2(i, 0, n)
#define all(x) begin(x), end(x)
auto ndvec(int n, auto e) { return vector(n, e); }
auto ndvec(int n, auto ...e) { return vector(n, ndvec(e...)); }
auto comp_key(auto&& f) { return [&](auto&& a, auto&& b) { return f(a) < f(b); }; }
auto& max(const auto& a, const auto& b) { return a < b ? b : a; }
auto& min(const auto& a, const auto& b) { return b < a ? b : a; }
#if __cpp_lib_ranges
namespace R = std::ranges;
namespace V = std::views;
#endif
#line 3 "bbst/iterator.hpp"

template <class Self, class Node, class T, bool Const>
class bidirectional_iterator_impl {
 public:
  using difference_type = ptrdiff_t;
  using value_type = T;
  using pointer = conditional_t<Const, const T *, T *>;
  using reference = conditional_t<Const, const T &, T &>;
  using iterator_category = bidirectional_iterator_tag;

 private:
  using self_type = Self;
  using node_pointer = conditional_t<Const, const Node *, Node *>;

 public:
  bidirectional_iterator_impl(Node *p) : ptr(p) { }
  bidirectional_iterator_impl(const Node *p = nullptr) : ptr(p) {} // ill-formed if !Const
  // reference operator*() const { return ptr->*Val(); }
  // pointer operator->() const { return &**this; }
  bool operator==(self_type rhs) const { return ptr == rhs.ptr; }
  bool operator!=(self_type rhs) const { return ptr != rhs.ptr; }
  self_type &operator++() {
    if (ptr->r) {
      ptr = ptr->r;
      while (ptr->l) ptr = ptr->l;
    } else {
      while (ptr->par->r == ptr) ptr = ptr->par;
      ptr = ptr->par;
    }
    return *(self_type *)this;
  }
  self_type operator++(int) {
    self_type res(ptr);
    ++*this;
    return res;
  }
  self_type &operator--() {
    if (ptr->l) {
      ptr = ptr->l;
      while (ptr->r) ptr = ptr->r;
    } else {
      while (ptr->par->l == ptr) ptr = ptr->par;
      ptr = ptr->par;
    }
    return *(self_type *)this;
  }
  self_type operator--(int) {
    self_type res(ptr);
    --*this;
    return res;
  }

  node_pointer ptr;
};
#line 3 "bit/clz.hpp"

#pragma GCC target("lzcnt")

template <class T>
int clz(T x) {
    if (!x) return sizeof(T) * 8;
    if constexpr (sizeof(T) <= sizeof(unsigned)) {
        return __builtin_clz((unsigned)x);
    } else if constexpr (sizeof(T) <= sizeof(unsigned long long)) {
        return __builtin_clzll((unsigned long long)x);
    } else if constexpr (sizeof(T) <= sizeof(unsigned long long) * 2) {
        int l = clz((unsigned long long)(x >> sizeof(unsigned long long) * 8));
        return l != sizeof(unsigned long long) * 8 ? l : l + clz((unsigned long long)x);
    }
}
#line 4 "bit/ilog2.hpp"

template <class T>
__attribute__((pure)) int ilog2(T x) { assert(x != 0); return sizeof(T) * 8 - 1 - clz(x); }

template <class T>
__attribute__((pure)) int ilog2_ceil(T x) { return x == 0 || x == 1 ? 0 : ilog2(x - 1) + 1; }

template <class T, enable_if_t<is_signed_v<T>>* = nullptr>
__attribute__((pure)) T bit_floor(T x) { return T(1) << ilog2(x); }

template <class T, enable_if_t<is_signed_v<T>>* = nullptr>
__attribute__((pure)) T bit_ceil(T x) { return T(1) << ilog2_ceil(x); }
#line 3 "func/no_op.hpp"

struct no_op {
  template <class... Ts>
  void operator()(Ts&&...) const {}
};
#line 3 "util/pool.hpp"

template <size_t N = 1 << 20>
struct pool {
  template <class T>
  struct alloc {
    static inline T* ptr =
        (T*)new (align_val_t(alignof(T))) unsigned char[sizeof(T) * N];
    static void* operator new(size_t) noexcept { return ptr++; }
    static void operator delete(void* p) { destroy_at((T*)p); }

   private:
    alloc() = default;
    friend T;
  };
};

struct no_pool {
  template <class T>
  struct alloc {};
};
#line 3 "util/seed.hpp"

auto seed() {
#if defined(LOCAL) && !defined(NO_FIX_SEED)
  return 314169265258979;
#endif
  return chrono::steady_clock::now().time_since_epoch().count();
}
#line 3 "util/rand.hpp"

uint32_t rand32() {
  static uint32_t x = seed();
  x ^= x << 13;
  x ^= x >> 17;
  x ^= x << 5;
  return x;
}

uint64_t rand64() {
  return uint64_t(rand32()) << 32 | rand32();
}
#line 8 "bbst/implicit_treap.hpp"

template <
    class T, class Update = no_op, class Propagate = no_op,
    class Alloc = pool<>>
class implicit_treap {
  class node;
  class node_base;
  using link = node_base*;
  using clink = const node_base*;

  class node_base {
   public:
    link l = nullptr, r = nullptr, par = nullptr;
    int cnt = 1;
    uint32_t key = rand32();
    T& val() { return ((node*)this)->val; }
    const T& val() const { return ((const node*)this)->val; }
  };
  class node : public node_base, public Alloc::template alloc<node> {
   public:
    T val;
    node(T val) : val(move(val)) { }
  };

 private:
 public:
  class iterator
      : public bidirectional_iterator_impl<iterator, node_base, T, false> {
    using base_type =
        bidirectional_iterator_impl<iterator, node_base, T, false>;

   public:
    using base_type::base_type;
    T& operator*() const { return base_type::ptr->val(); }
    T* operator->() const { return &**this; }
  };

  class const_iterator
      : public bidirectional_iterator_impl<const_iterator, node_base, T, true> {
    using base_type =
        bidirectional_iterator_impl<const_iterator, node_base, T, true>;

   public:
    using base_type::base_type;
    const_iterator(iterator it) : base_type(it.ptr) { }
    const T& operator*() const { return base_type::ptr->val(); }
    const T* operator->() const { return &**this; }
  };

 public:
  implicit_treap(Update upd = Update(), Propagate prop = Propagate())
      : upd(upd), prop(prop) { }
  template <class It>
  implicit_treap(
      It first, It last, Update upd = Update(), Propagate prop = Propagate())
      : upd(upd), prop(prop) {
    vector<link> ps(bit_ceil(distance(first, last)) * 2);
    for (auto it = ps.begin(); first != last; it++, first++)
      *it = (link) new node(*first);
    for (int i = 0; i + 1 < ps.size(); i += 2)
      ps.push_back(merge(ps[i], ps[i + 1]));
    set_l(&head, ps.back());
  }

  implicit_treap(const implicit_treap& rhs) : upd(rhs.upd), prop(rhs.prop) {
    set_l(&head, deep_copy(rhs.head.l));
  }
  implicit_treap(implicit_treap&& rhs) : upd(rhs.upd), prop(rhs.prop) {
    set_l(&head, rhs.head.l);
    rhs.head.l = nullptr;
  }
  implicit_treap& operator=(const implicit_treap& rhs) {
    set_l(&head, deep_copy(rhs.head.l));
    return *this;
  }
  implicit_treap& operator=(implicit_treap&& rhs) {
    set_l(&head, rhs.head.l), rhs.head.l = nullptr;
    return *this;
  }

  const_iterator begin() const {
    auto ptr = &head;
    while (ptr->l) ptr = ptr->l;
    return const_iterator(ptr);
  }
  const_iterator end() const { return const_iterator(&head); }
  const T& root() const { return head.l->val(); }
  T& root() { return head.l->val(); }
  const_iterator find(int k) const { return const_iterator(find(head.l, k)); }

  template <class F>
  void apply_on(int i, int j, F f) {
    if (i == j) return;
    auto [lm, r] = split(j);
    auto [l, m] = lm.split(i);
    f(m.root());
    *this = move(l.join(m).join(r));
  }

  int size() const { return count(head.l); }
  implicit_treap& join(implicit_treap& rhs) {
    set_l(&head, merge(head.l, rhs.head.l));
    rhs.head.l = nullptr;
    return *this;
  }
  pair<implicit_treap, implicit_treap> split(int k) {
    auto [l, r] = split(head.l, k);
    implicit_treap left(l, upd, prop);
    set_l(&head, r);
    return pair(move(left), move(*this));
  }
  const_iterator insert(int k, T v) {
    auto p = (link) new node(move(v));
    set_l(&head, insert(head.l, k, p));
    return const_iterator(p);
  }
  void erase(int i) { set_l(&head, erase(head.l, i)); }
  int index_of(const_iterator it) {
    auto p = const_cast<link>(it.ptr);
    propagate(*p);
    int ans = count(p->l);
    for (; p->par != &head; p = p->par) {
      if (p->par->r == p) ans += count(p->par->l) + 1;
      auto l0 = p->par->l;
      propagate(*p->par);
      if (l0 != p->par->l) ans = count(p->par) - ans - 1;
    }
    return ans;
  }
  void propagate_all() { propagate_all(head.l); }

 private:
  implicit_treap(link root, Update upd, Propagate prop) : upd(upd), prop(prop) {
    set_l(&head, root);
  }

  node_base head;
  Update upd;
  Propagate prop;
  int count(link p) const { return p ? p->cnt : 0; }
  void propagate(node_base& n) { prop(n, n.val()); }
  void update(node_base& n) { update((node&)n); }
  void update(node& n) {
    n.cnt = 1 + count(n.l) + count(n.r);
    if (n.l) propagate(*n.l);
    if (n.r) propagate(*n.r);
    upd((node_base&)n, n.val);
  }
  void set_l(link par, link p) {
    par->l = p;
    if (p) p->par = par;
  }
  void set_r(link par, link p) {
    par->r = p;
    if (p) p->par = par;
  }
  link merge(link l, link r) {
    if (!l) return r;
    if (!r) return l;
    if (l->key > r->key) {
      return propagate(*l), set_r(l, merge(l->r, r)), update(*l), l;
    } else {
      return propagate(*r), set_l(r, merge(l, r->l)), update(*r), r;
    }
  }
  pair<link, link> split(link p, int k) {
    if (!p) {
      assert(k == 0);
      return pair(nullptr, nullptr);
    }
    propagate(*p);
    if (count(p->l) < k) {
      auto [l, r] = split(p->r, k - count(p->l) - 1);
      set_r(p, l), update(*p);
      return pair(p, r);
    } else {
      auto [l, r] = split(p->l, k);
      set_l(p, r), update(*p);
      return pair(l, p);
    }
  }
  link insert(link p, int k, link x) {
    if (!p) {
      assert(k == 0);
      return x;
    }
    propagate(*p);
    if (x->key > p->key) {
      auto [l, r] = split(p, k);
      return set_l(x, l), set_r(x, r), update(*x), x;
    } else if (count(p->l) < k) {
      return set_r(p, insert(p->r, k - count(p->l) - 1, x)), update(*p), p;
    } else {
      return set_l(p, insert(p->l, k, x)), update(*p), p;
    }
  }
  link erase(link p, int k) {
    assert(p);
    propagate(*p);
    if (count(p->l) < k) {
      return set_r(p, erase(p->r, k - count(p->l) - 1)), update(*p), p;
    } else if (count(p->l) > k) {
      return set_l(p, erase(p->l, k)), update(*p), p;
    } else {
      return merge(p->l, p->r);
    }
  }
  link find(link p, int k) const {
    return count(p->l) == k  ? p
           : count(p->l) < k ? find(p->r, k - count(p->l) - 1)
                             : find(p->l, k);
  }
  link deep_copy(link p) {
    if (!p) return nullptr;
    link q = (link) new node(*(node*)p);
    set_l(q, deep_copy(p->l));
    set_r(q, deep_copy(p->r));
    return q;
  }
  void propagate_all(link p) {
    if (!p) return;
    propagate(*p);
    propagate_all(p->l), propagate_all(p->r);
  }
};
#line 6 "test/bbst/implicit_treap.affine.test.cpp"
using mint = atcoder::modint998244353;

struct affine {
  mint a = 1, b = 0;
  void then(affine rhs) { *this = affine{a * rhs.a, b * rhs.a + rhs.b}; }
};

struct T {
  mint a, sum;
  affine f;
  bool rev;
};

int main() {
  auto prop = [](auto &n, auto &v) {
    if (v.f.a != 1 || v.f.b != 0) {
      v.a = v.a * v.f.a + v.f.b;
      v.sum = v.sum * v.f.a + v.f.b * n.cnt;
      if (n.l) n.l->val().f.then(v.f);
      if (n.r) n.r->val().f.then(v.f);
      v.f = affine{};
    }
    if (v.rev) {
      swap(n.l, n.r);
      if (n.l) n.l->val().rev ^= true;
      if (n.r) n.r->val().rev ^= true;
      v.rev = false;
    }
  };
  auto upd = [](auto &n, auto &v) {
    assert(v.f.a == 1 && v.f.b == 0);
    v.sum = v.a + (n.l ? n.l->val().sum : 0) + (n.r ? n.r->val().sum : 0);
  };

  int n, q;
  scanf("%d%d", &n, &q);
  vector<T> a(n);
  rep(i, n) {
    int x;
    scanf("%d", &x);
    a[i] = T{x, x, {}, false};
  }

  implicit_treap<T, decltype(upd), decltype(prop)> tree(all(a), upd, prop);

  while (q--) {
    int cmd;
    scanf("%d", &cmd);
    if (cmd == 0) {
      int i, x;
      scanf("%d%d", &i, &x);
      tree.insert(i, T{x, x, {}, false});
    } else if (cmd == 1) {
      int i;
      scanf("%d", &i);
      tree.erase(i);
    } else if (cmd == 2) {
      int l, r;
      scanf("%d%d", &l, &r);
      tree.apply_on(l, r, [](auto &v) { v.rev ^= true; });
    } else if (cmd == 3) {
      int l, r, b, c;
      scanf("%d%d%d%d", &l, &r, &b, &c);
      tree.apply_on(l, r, [=](auto &v) { v.f.then(affine{b, c}); });
    } else {
      int l, r;
      scanf("%d%d", &l, &r);
      mint ans;
      tree.apply_on(l, r, [&](auto &v) { ans = v.sum; });
      printf("%d\n", ans.val());
    }
  }
}

Test cases

Env Name Status Elapsed Memory
g++-12 example_00 :heavy_check_mark: AC 22 ms 16 MB
g++-12 extreme_insertion_00 :heavy_check_mark: AC 1043 ms 38 MB
g++-12 extreme_insertion_01 :heavy_check_mark: AC 1069 ms 38 MB
g++-12 extreme_insertion_02 :heavy_check_mark: AC 1084 ms 38 MB
g++-12 max_00 :heavy_check_mark: AC 2455 ms 105 MB
g++-12 max_01 :heavy_check_mark: AC 1740 ms 76 MB
g++-12 max_02 :heavy_check_mark: AC 7201 ms 77 MB
g++-12 max_03 :heavy_check_mark: AC 12422 ms 77 MB
g++-12 max_04 :heavy_check_mark: AC 6376 ms 77 MB
g++-12 random_00 :heavy_check_mark: AC 6287 ms 75 MB
g++-12 random_01 :heavy_check_mark: AC 6506 ms 80 MB
g++-12 random_02 :heavy_check_mark: AC 4562 ms 23 MB
g++-12 random_03 :heavy_check_mark: AC 616 ms 73 MB
g++-12 random_04 :heavy_check_mark: AC 1574 ms 62 MB
g++-12 small_00 :heavy_check_mark: AC 24 ms 16 MB
g++-12 small_01 :heavy_check_mark: AC 23 ms 16 MB
g++-12 small_02 :heavy_check_mark: AC 23 ms 16 MB
g++-12 small_03 :heavy_check_mark: AC 23 ms 16 MB
g++-12 small_04 :heavy_check_mark: AC 27 ms 16 MB
g++-12 small_05 :heavy_check_mark: AC 24 ms 16 MB
g++-12 small_06 :heavy_check_mark: AC 24 ms 16 MB
g++-12 small_07 :heavy_check_mark: AC 24 ms 16 MB
g++-12 small_08 :heavy_check_mark: AC 24 ms 16 MB
g++-12 small_09 :heavy_check_mark: AC 25 ms 16 MB
g++-12 wrong_avl_killer_00 :heavy_check_mark: AC 1559 ms 38 MB
g++-12 wrong_avl_killer_01 :heavy_check_mark: AC 1238 ms 38 MB
Back to top page