結果

問題 No.924 紲星
ユーザー ei1333333ei1333333
提出日時 2019-11-08 22:10:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,419 ms / 4,000 ms
コード長 13,149 bytes
コンパイル時間 2,759 ms
コンパイル使用メモリ 227,720 KB
実行使用メモリ 225,632 KB
最終ジャッジ日時 2024-09-15 01:32:10
合計ジャッジ時間 22,242 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 5 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 6 ms
5,376 KB
testcase_06 AC 5 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 2,419 ms
225,632 KB
testcase_09 AC 2,324 ms
225,624 KB
testcase_10 AC 2,337 ms
225,628 KB
testcase_11 AC 2,255 ms
225,632 KB
testcase_12 AC 2,283 ms
225,628 KB
testcase_13 AC 1,080 ms
84,676 KB
testcase_14 AC 1,079 ms
55,668 KB
testcase_15 AC 1,012 ms
68,584 KB
testcase_16 AC 819 ms
193,776 KB
testcase_17 AC 1,718 ms
87,252 KB
testcase_18 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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 {
  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)};
}

// のしさんありがとう!!

template< std::size_t BitLength, class Abelian >
struct foldable_wavelet_matrix {
  using mapped_structure = Abelian;
  using mapped_type = typename mapped_structure::value_type;
  using key_type = std::bitset< BitLength >;

  struct bitvector {
    struct node_type;
    using container_type = std::vector< node_type >;
    using size_type = typename container_type::size_type;

    struct node_type {
      size_type count;
      mapped_type sum;

      constexpr node_type()
          : count(static_cast<size_type>(0)),
            sum(mapped_structure::identity()) {}
    };

    container_type vec;

    constexpr bitvector() : vec() {}

    constexpr void resize(const size_type size) {
      vec.resize(size + static_cast<size_type>(1));
    }

    constexpr void set(const size_type index) {
      vec[index].count = static_cast<size_type>(1);
    }

    template< class InputIterator >
    constexpr void build(const std::vector< InputIterator > &a) {
      for(size_type i = a.size(); i != static_cast<size_type>(0);) {
        --i;
        vec[i].count += vec[i + static_cast<size_type>(1)].count;
        vec[i].sum = mapped_structure::operation(
            a[i]->second, vec[i + static_cast<size_type>(1)].sum);
      }
    }

    constexpr size_type count(const size_type index) const {
      return vec[index].count;
    }

    constexpr size_type zeros() const { return vec.front().count; }

    constexpr mapped_type fold(const size_type first,
                               const size_type last) const {
      return mapped_structure::operation(
          vec[first].sum, mapped_structure::inverse(vec[last].sum));
    }
  };

  using size_type = typename bitvector::size_type;

  std::array< bitvector, BitLength > matrix;

  constexpr foldable_wavelet_matrix() : matrix() {}

  template< class InputIterator >
  explicit constexpr foldable_wavelet_matrix(InputIterator first,
                                             const InputIterator last) {
    using iterator = InputIterator;

    const size_type size = static_cast<size_type>(std::distance(first, last));
    std::vector< iterator > cur, pre;
    cur.reserve(size);
    for(; first != last; ++first) {
      cur.push_back(first);
    }
    pre = cur;
    std::size_t i = BitLength;
    while(i != static_cast<std::size_t>(0)) {
      --i;
      bitvector &vec = matrix[i];
      vec.resize(size);
      std::swap(cur, pre);
      typename std::vector< iterator >::iterator zero_itr = cur.begin(),
          one_itr = cur.end();
      for(size_type k = static_cast<size_type>(0); zero_itr != one_itr; ++k) {
        if(pre[k]->first.test(i)) {
          --one_itr;
          *one_itr = pre[k];
        } else {
          vec.set(k);
          *zero_itr = pre[k];
          ++zero_itr;
        }
      }
      std::reverse(one_itr, cur.end());
      vec.build(cur);
    }
  }

  constexpr std::tuple< mapped_type, mapped_type, mapped_type >
  fold_leg(size_type first, size_type last, const key_type &key) const {
    mapped_type less = mapped_structure::identity(),
        greater = mapped_structure::identity();
    std::size_t i = BitLength;
    while(i != static_cast<std::size_t>(0)) {
      --i;
      const bitvector &vec = matrix[i];
      const size_type f = vec.count(first), l = vec.count(last),
          z = vec.zeros();
      if(key.test(i)) {
        less = mapped_structure::operation(std::move(less),
                                           vec.fold(z - f, z - l));
        first += f;
        last += l;
      } else {
        greater = mapped_structure::operation(vec.fold(first + f, last + l),
                                              std::move(greater));
        first = z - f;
        last = z - l;
      }
    }
    return std::forward_as_tuple(
        std::move(less), matrix.front().fold(first, last), std::move(greater));
  }

  constexpr mapped_type fold_range(const size_type first, const size_type last,
                                   const key_type &lower,
                                   const key_type &upper) const {
    return mapped_structure::operation(
        mapped_structure::inverse(std::get< 0 >(fold_leg(first, last, lower))),
        std::get< 0 >(fold_leg(first, last, upper)));
  }

  constexpr std::pair< mapped_type, mapped_type >
  fold_quantile(size_type first, size_type last, size_type k) const {
    mapped_type less = mapped_structure::identity(),
        greater = mapped_structure::identity();
    std::size_t i = BitLength;
    while(i != static_cast<std::size_t>(0)) {
      --i;
      const bitvector &vec = matrix[i];
      const size_type f = vec.count(first), l = vec.count(last),
          z = vec.zeros();
      if(f - l <= k) {
        k -= f - l;
        less = mapped_structure::operation(std::move(less),
                                           vec.fold(z - f, z - l));
        first += f;
        last += l;
      } else {
        greater = mapped_structure::operation(vec.fold(first + f, last + l),
                                              std::move(greater));
        first = z - f;
        last = z - l;
      }
    }
    return std::make_pair(
        mapped_structure::operation(std::move(less),
                                    matrix.front().fold(first, first + k)),
        mapped_structure::operation(matrix.front().fold(first + k, last),
                                    std::move(greater)));
  }
};

template< class T >
class plus_abelian {
public:
  using value_type = T;

  static value_type operation(const value_type &x, const value_type &y) {
    return x + y;
  }

  static value_type identity() { return static_cast<value_type>(0); }

  static value_type inverse(const value_type &x) { return -x; }

  static value_type reverse(const value_type &x) { return x; }
};


struct SuccinctIndexableDictionary {
  size_t length;
  size_t blocks;
  vector< unsigned > bit, sum;

  SuccinctIndexableDictionary() {
  }

  SuccinctIndexableDictionary(size_t _length) {
    length = _length;
    blocks = (length + 31) >> 5;
    bit.assign(blocks, 0U);
    sum.assign(blocks, 0U);
  }

  void set(int k) {
    bit[k >> 5] |= 1U << (k & 31);
  }

  void build() {
    sum[0] = 0U;
    for(int i = 1; i < blocks; i++) {
      sum[i] = sum[i - 1] + __builtin_popcount(bit[i - 1]);
    }
  }

  bool operator[](int k) const {
    return (bool((bit[k >> 5] >> (k & 31)) & 1));
  }

  int rank(int k) {
    return (sum[k >> 5] + __builtin_popcount(bit[k >> 5] & ((1U << (k & 31)) - 1)));
  }

  int rank(bool val, int k) {
    return (val ? rank(k) : k - rank(k));
  }

  int select(bool val, int k) {
    if(k < 0 || rank(val, length) <= k) return (-1);
    int low = 0, high = length;
    while(high - low > 1) {
      int mid = (low + high) >> 1;
      if(rank(val, mid) >= k + 1) high = mid;
      else low = mid;
    }
    return (high - 1);
  }

  int select(bool val, int i, int l) {
    return select(val, i + rank(val, l));
  }
};

template< class T, int MAXLOG >
struct WaveletMatrix {
  size_t length;
  SuccinctIndexableDictionary matrix[MAXLOG];
  int zs[MAXLOG];
  int buff1[MAXLOG], buff2[MAXLOG];

  int freq_dfs(int d, int l, int r, T val, T a, T b) {
    if(l == r) return 0;
    if(d == MAXLOG) return (a <= val && val < b) ? r - l : 0;
    T nv = 1ULL << (MAXLOG - d - 1) | val, nnv = ((1ULL << (MAXLOG - d - 1)) - 1) | nv;
    if(nnv < a || b <= val) return 0;
    if(a <= val && nnv < b) return r - l;
    int lc = matrix[d].rank(1, l), rc = matrix[d].rank(1, r);
    return freq_dfs(d + 1, l - lc, r - rc, val, a, b) +
           freq_dfs(d + 1, lc + zs[d], rc + zs[d], nv, a, b);
  }

  WaveletMatrix(vector< T > data) {
    length = data.size();
    vector< T > l(length), r(length);
    for(int depth = 0; depth < MAXLOG; depth++) {
      matrix[depth] = SuccinctIndexableDictionary(length + 1);
      int left = 0, right = 0;
      for(int i = 0; i < length; i++) {
        bool k = (data[i] >> (MAXLOG - depth - 1)) & 1;
        if(k) r[right++] = data[i], matrix[depth].set(i);
        else l[left++] = data[i];
      }
      zs[depth] = left;
      matrix[depth].build();
      swap(l, data);
      for(int i = 0; i < right; i++) data[left + i] = r[i];
    }
  }

  T access(int k) {
    int ret = 0;
    bool bit;
    for(int depth = 0; depth < MAXLOG; depth++) {
      bit = matrix[depth][k];
      ret = (ret << 1) | bit;
      k = matrix[depth].rank(bit, k) + zs[depth] * bit;
    }
    return (ret);
  }

  int rank(T val, int k) {
    int l = 0, r = k;
    for(int depth = 0; depth < MAXLOG; depth++) {
      buff1[depth] = l, buff2[depth] = r;
      bool bit = (val >> (MAXLOG - depth - 1)) & 1;
      l = matrix[depth].rank(bit, l) + zs[depth] * bit;
      r = matrix[depth].rank(bit, r) + zs[depth] * bit;
    }
    return (r - l);
  }

  int select(T val, int kth) {
    rank(val, length);
    for(int depth = MAXLOG - 1; depth >= 0; depth--) {
      bool bit = (val >> (MAXLOG - depth - 1)) & 1;
      kth = matrix[depth].select(bit, kth, buff1[depth]);
      if(kth >= buff2[depth] || kth < 0) return (-1);
      kth -= buff1[depth];
    }
    return (kth);
  }

  int select(T val, int k, int l) {
    return select(val, k + rank(val, l));
  }

  int quantile(int left, int right, int kth) {
    if(right - left <= kth || kth < 0) return (-1);
    T ret = 0;
    for(int depth = 0; depth < MAXLOG; depth++) {
      int l = matrix[depth].rank(1, left);
      int r = matrix[depth].rank(1, right);
      if(r - l > kth) {
        left = l + zs[depth];
        right = r + zs[depth];
        ret |= 1ULL << (MAXLOG - depth - 1);
      } else {
        kth -= r - l;
        left -= l;
        right -= r;
      }
    }
    return ret;
  }

  int rangefreq(int left, int right, T lower, T upper) {
    return freq_dfs(0, left, right, 0, lower, upper);
  }
};

int main() {
  int N, Q;
  cin >> N >> Q;
  vector< int64 > A(N);
  cin >> A;
  for(int i = 0; i < N; i++) A[i] += inf;
  WaveletMatrix< int64, 33 > mat(A);
  vector< int > L(Q), R(Q);
  vector< int64 > mid(Q);
  for(int i = 0; i < Q; i++) {
    cin >> L[i] >> R[i];
    --L[i];
    int md = (R[i] - L[i]) / 2;
    auto ret = mat.quantile(L[i], R[i], md);
    mid[i] = ret;
  }

  vector< pair< bitset< 33 >, int64 > > tap1;
  vector< pair< bitset< 33 >, int64 > > tap2;

  for(int i = 0; i < N; i++) {
    tap1.emplace_back(A[i], A[i]);
    tap2.emplace_back(A[i], 1);
  }

  const foldable_wavelet_matrix< 33, plus_abelian< int64 >> wm1(tap1.cbegin(),
                                                                tap1.cend());
  const foldable_wavelet_matrix< 33, plus_abelian< int64 >> wm2(tap2.cbegin(),
                                                                tap2.cend());

  for(int i = 0; i < Q; i++) {
    int64 upcnt = wm2.fold_range(L[i], R[i], mid[i], infll);
    int64 upsum = wm1.fold_range(L[i], R[i], mid[i], infll);
    upsum -= upcnt * inf;

    int64 lowcnt = wm2.fold_range(L[i], R[i], 0, mid[i]);
    int64 lowsum = wm1.fold_range(L[i], R[i], 0, mid[i]);
    lowsum -= lowcnt * inf;

    mid[i] -= inf;

    cout << (upsum - upcnt * mid[i]) + (mid[i] * lowcnt - lowsum) << endl;

  }


}
0