Luzhiled's Library

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

View the Project on GitHub ei1333/library

:heavy_check_mark: test/verify/aoj-1508.test.cpp

Depends on

Code

#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1508"

#include "../../template/template.hpp"

#include "../../structure/bbst/randomized-binary-search-tree.hpp"

int main() {
  int N, Q;
  cin >> N >> Q;
  vector< int > A(N);
  for(auto &a : A) cin >> a;
  auto f = [](int a, int b) { return min(a, b); };
  RandomizedBinarySearchTree< int > rbst(N + Q, f, inf);
  auto V = rbst.build(A);
  while(Q--) {
    int X, Y, Z;
    cin >> X >> Y >> Z;
    if(X == 0) {
      auto S = rbst.split3(V, Y, Z + 1);
      auto val = rbst.pop_back(get< 1 >(S));
      rbst.push_front(get< 1 >(S), val);
      V = rbst.merge(get< 0 >(S), get< 1 >(S), get< 2 >(S));
    } else if(X == 1) {
      cout << rbst.query(V, Y, Z + 1) << "\n";
    } else {
      rbst.set_element(V, Y, Z);
    }
  }
}
#line 1 "test/verify/aoj-1508.test.cpp"
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1508"

#line 1 "template/template.hpp"
#include<bits/stdc++.h>

using namespace std;

using int64 = long long;
const int mod = 1e9 + 7;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;

template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 >& p) {
  os << p.first << " " << p.second;
  return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}

template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e : t) fill_v(e, v);
}

template< typename F >
struct FixPoint : F {
  explicit FixPoint(F &&f) : F(forward< F >(f)) {}

  template< typename... Args >
  decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, forward< Args >(args)...);
  }
};
 
template< typename F >
inline decltype(auto) MFP(F &&f) {
  return FixPoint< F >{forward< F >(f)};
}
#line 4 "test/verify/aoj-1508.test.cpp"

#line 1 "structure/bbst/randomized-binary-search-tree.hpp"
template< typename T >
class RandomizedBinarySearchTree {

  using F = function< T(T, T) >;

private:

  struct Node {
    Node *l, *r;
    size_t cnt;
    T key, sum;

    Node() = default;

    Node(const T &k) : cnt(1), key(k), sum(k), l(nullptr), r(nullptr) {}
  };

  inline int xor128() {
    static int x = 123456789;
    static int y = 362436069;
    static int z = 521288629;
    static int w = 88675123;
    int t;

    t = x ^ (x << 11);
    x = y;
    y = z;
    z = w;
    return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
  }

  Node *build(int l, int r, const vector< T > &v) {
    if(l + 1 >= r) return alloc(v[l]);
    return merge(build(l, (l + r) >> 1, v), build((l + r) >> 1, r, v));
  }

  void dump(Node *t, typename vector< T >::iterator &it) const {
    if(!t) return;
    dump(t->l, it);
    *it = t->key;
    dump(t->r, ++it);
  }

  inline size_t count(const Node *t) const {
    return t ? t->cnt : 0;
  }

  inline T sum(const Node *t) const {
    return t ? t->sum : e;
  }

  inline Node *update(Node *t) {
    t->cnt = count(t->l) + count(t->r) + 1;
    t->sum = f(f(sum(t->l), t->key), sum(t->r));
    return t;
  }

  vector< Node > pool;
  int ptr;
  const F f;
  const T e;

public:

  RandomizedBinarySearchTree(size_t sz, const F &f, const T &e) : pool(sz), f(f), ptr(0), e(e) {}

  inline Node *alloc(const T &v) {
    return &(pool[ptr++] = Node(v));
  }

  Node *merge(Node *l, Node *r) {
    if(!l || !r) return l ? l : r;
    if(xor128() % (l->cnt + r->cnt) < l->cnt) {
      l->r = merge(l->r, r);
      return update(l);
    } else {
      r->l = merge(l, r->l);
      return update(r);
    }
  }

  template< typename... Args >
  Node *merge(Node *p, Args... args) {
    return merge(p, merge(args...));
  }

  pair< Node *, Node * > split(Node *t, int k) {
    if(!t) return {t, t};
    if(k <= count(t->l)) {
      auto s = split(t->l, k);
      t->l = s.second;
      return {s.first, update(t)};
    } else {
      auto s = split(t->r, k - count(t->l) - 1);
      t->r = s.first;
      return {update(t), s.second};
    }
  }

  Node *build(const vector< T > &v) {
    ptr = 0;
    return build(0, (int) v.size(), v);
  }

  vector< T > dump(Node *t) const {
    vector< T > v(count(t));
    auto it = begin(v);
    dump(t, it);
    return v;
  }

  string to_string(Node *r) {
    auto s = dump(r);
    string ret;
    for(int i = 0; i < s.size(); i++) ret += std::to_string(s[i]) + ", ";
    return ret;
  }

  void insert(Node *&t, int k, const T &v) {
    auto x = split(t, k);
    t = merge(merge(x.first, alloc(v)), x.second);
  }

  void push_front(Node *&t, const T &v) {
    t = merge(alloc(v), t);
  }

  void push_back(Node *&t, const T &v) {
    t = merge(t, alloc(v));
  }

  T pop_front(Node *&t) {
    auto ret = split(t, 1);
    t = ret.second;
    return ret.first->key;
  }

  T pop_back(Node *&t) {
    auto ret = split(t, count(t) - 1);
    t = ret.first;
    return ret.second->key;
  }

  void erase(Node *&t, int k) {
    auto x = split(t, k);
    t = merge(x.first, split(x.second, 1).second);
  }

  T query(Node *&t, int a, int b) {
    auto x = split(t, a);
    auto y = split(x.second, b - a);
    auto ret = sum(y.first);
    t = merge(x.first, merge(y.first, y.second));
    return ret;
  }

  tuple< Node *, Node *, Node * > split3(Node *t, int a, int b) {
    auto x = split(t, a);
    auto y = split(x.second, b - a);
    return make_tuple(x.first, y.first, y.second);
  }

  void set_element(Node *&t, int k, const T &x) {
    if(k < count(t->l)) set_element(t->l, k, x);
    else if(k == count(t->l)) t->key = t->sum = x;
    else set_element(t->r, k - count(t->l) - 1, x);
    t = update(t);
  }

  size_t size(Node *t) const {
    return count(t);
  }

  bool empty(Node *t) const {
    return !t;
  }
};

#line 6 "test/verify/aoj-1508.test.cpp"

int main() {
  int N, Q;
  cin >> N >> Q;
  vector< int > A(N);
  for(auto &a : A) cin >> a;
  auto f = [](int a, int b) { return min(a, b); };
  RandomizedBinarySearchTree< int > rbst(N + Q, f, inf);
  auto V = rbst.build(A);
  while(Q--) {
    int X, Y, Z;
    cin >> X >> Y >> Z;
    if(X == 0) {
      auto S = rbst.split3(V, Y, Z + 1);
      auto val = rbst.pop_back(get< 1 >(S));
      rbst.push_front(get< 1 >(S), val);
      V = rbst.merge(get< 0 >(S), get< 1 >(S), get< 2 >(S));
    } else if(X == 1) {
      cout << rbst.query(V, Y, Z + 1) << "\n";
    } else {
      rbst.set_element(V, Y, Z);
    }
  }
}
Back to top page