結果

問題 No.924 紲星
ユーザー hashiryohashiryo
提出日時 2023-11-05 20:44:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,329 ms / 4,000 ms
コード長 11,164 bytes
コンパイル時間 2,674 ms
コンパイル使用メモリ 235,184 KB
実行使用メモリ 89,564 KB
最終ジャッジ日時 2024-09-25 22:41:43
合計ジャッジ時間 13,613 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 3 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 3 ms
6,944 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 1,329 ms
89,564 KB
testcase_09 AC 1,287 ms
89,384 KB
testcase_10 AC 1,251 ms
89,352 KB
testcase_11 AC 1,207 ms
89,432 KB
testcase_12 AC 1,305 ms
89,416 KB
testcase_13 AC 557 ms
33,664 KB
testcase_14 AC 492 ms
21,104 KB
testcase_15 AC 462 ms
25,732 KB
testcase_16 AC 475 ms
78,244 KB
testcase_17 AC 761 ms
34,036 KB
testcase_18 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
// clang-format off
std::ostream&operator<<(std::ostream&os,std::int8_t x){return os<<(int)x;}
std::ostream&operator<<(std::ostream&os,std::uint8_t x){return os<<(int)x;}
std::ostream&operator<<(std::ostream&os,const __int128_t &v){if(!v)os<<"0";__int128_t tmp=v<0?(os<<"-",-v):v;std::string s;while(tmp)s+='0'+(tmp%10),tmp/=10;return std::reverse(s.begin(),s.end()),os<<s;}
std::ostream&operator<<(std::ostream&os,const __uint128_t &v){if(!v)os<<"0";__uint128_t tmp=v;std::string s;while(tmp)s+='0'+(tmp%10),tmp/=10;return std::reverse(s.begin(),s.end()),os<<s;}
#define checkpoint() (void(0))
#define debug(...) (void(0))
#define debugArray(x,n) (void(0))
#define debugMatrix(x,h,w) (void(0))
// clang-format on
#include <type_traits>
template <class T> static constexpr bool tuple_like_v= false;
template <class... Args> static constexpr bool tuple_like_v<std::tuple<Args...>> = true;
template <class T, class U> static constexpr bool tuple_like_v<std::pair<T, U>> = true;
template <class T, size_t K> static constexpr bool tuple_like_v<std::array<T, K>> = true;
template <class T> auto to_tuple(const T &t) {
 if constexpr (tuple_like_v<T>) return std::apply([](auto &&...x) { return std::make_tuple(x...); }, t);
}
template <class T> auto forward_tuple(const T &t) {
 if constexpr (tuple_like_v<T>) return std::apply([](auto &&...x) { return std::forward_as_tuple(x...); }, t);
}
template <class T> static constexpr bool array_like_v= false;
template <class T, size_t K> static constexpr bool array_like_v<std::array<T, K>> = true;
template <class T, class U> static constexpr bool array_like_v<std::pair<T, U>> = std::is_convertible_v<T, U>;
template <class T> static constexpr bool array_like_v<std::tuple<T>> = true;
template <class T, class U, class... Args> static constexpr bool array_like_v<std::tuple<T, U, Args...>> = array_like_v<std::tuple<T, Args...>> && std::is_convertible_v<U, T>;
template <class T> auto to_array(const T &t) {
 if constexpr (array_like_v<T>) return std::apply([](auto &&...x) { return std::array{x...}; }, t);
}
template <class T> using to_tuple_t= decltype(to_tuple(T()));
template <class T> using to_array_t= decltype(to_array(T()));
template <class pos_t, class M> class SegmentTree_2D {
public:
 using T= typename M::T;
 using Pos= std::array<pos_t, 2>;
 std::vector<pos_t> xs;
 std::vector<Pos> yxs;
 std::vector<int> id, tol;
 std::vector<T> val;
 template <class P> using canbe_Pos= std::is_convertible<to_tuple_t<P>, std::tuple<pos_t, pos_t>>;
 template <class P> using canbe_PosV= std::is_convertible<to_tuple_t<P>, std::tuple<pos_t, pos_t, T>>;
 template <class P, class U> static constexpr bool canbe_Pos_and_T_v= std::conjunction_v<canbe_Pos<P>, std::is_convertible<U, T>>;
 int sz;
 inline int x2i(pos_t x) const { return std::lower_bound(xs.begin(), xs.end(), x) - xs.begin(); }
 inline int y2i(pos_t y) const {
  return std::lower_bound(yxs.begin(), yxs.end(), Pos{y, 0}, [](const Pos &a, const Pos &b) { return a[0] < b[0]; }) - yxs.begin();
 }
 inline int xy2i(pos_t x, pos_t y) const {
  Pos p{y, x};
  auto it= std::lower_bound(yxs.begin(), yxs.end(), p);
  return assert(p == *it), it - yxs.begin();
 }
 template <bool z, size_t k, class P> inline auto get_(const P &p) {
  if constexpr (z) return std::get<k>(p);
  else return std::get<k>(p.first);
 }
 template <bool z, class XYW> inline void build(const XYW *xyw, int n, const T &v= M::ti()) {
  xs.resize(n), yxs.resize(n);
  for (int i= n; i--;) xs[i]= get_<z, 0>(xyw[i]);
  std::sort(xs.begin(), xs.end()), xs.erase(std::unique(xs.begin(), xs.end()), xs.end()), id.resize((sz= 1 << (32 - __builtin_clz(xs.size()))) * 2 + 1);
  std::vector<int> ix(n), ord(n);
  for (int i= n; i--;) ix[i]= x2i(get_<z, 0>(xyw[i]));
  for (int i: ix)
   for (i+= sz; i; i>>= 1) ++id[i + 1];
  for (int i= 1, e= sz * 2; i < e; ++i) id[i + 1]+= id[i];
  val.assign(id.back() * 2, M::ti()), tol.resize(id[sz] + 1), std::iota(ord.begin(), ord.end(), 0), std::sort(ord.begin(), ord.end(), [&](int i, int j) { return get_<z, 1>(xyw[i]) == get_<z, 1>(xyw[j]) ? get_<z, 0>(xyw[i]) < get_<z, 0>(xyw[j]) : get_<z, 1>(xyw[i]) < get_<z, 1>(xyw[j]); });
  for (int i= n; i--;) yxs[i]= {get_<z, 1>(xyw[ord[i]]), get_<z, 0>(xyw[ord[i]])};
  std::vector<int> ptr= id;
  for (int r: ord)
   for (int i= ix[r] + sz, j= -1; i; j= i, i>>= 1) {
    int p= ptr[i]++;
    if constexpr (z) {
     if constexpr (std::tuple_size_v<XYW> == 3) val[id[i + 1] + p]= std::get<2>(xyw[r]);
     else val[id[i + 1] + p]= v;
    } else val[id[i + 1] + p]= xyw[r].second;
    if (j != -1) tol[p + 1]= !(j & 1);
   }
  for (int i= 1, e= id[sz]; i < e; ++i) tol[i + 1]+= tol[i];
  for (int i= 0, e= sz * 2; i < e; ++i) {
   auto dat= val.begin() + id[i] * 2;
   for (int j= id[i + 1] - id[i]; --j > 0;) dat[j]= M::op(dat[j * 2], dat[j * 2 + 1]);
  }
 }
 inline T fold(int i, int a, int b) const {
  int n= id[i + 1] - id[i];
  T ret= M::ti();
  auto dat= val.begin() + id[i] * 2;
  for (a+= n, b+= n; a < b; a>>= 1, b>>= 1) {
   if (a & 1) ret= M::op(ret, dat[a++]);
   if (b & 1) ret= M::op(dat[--b], ret);
  }
  return ret;
 }
 template <bool z> inline void seti(int i, int j, T v) {
  auto dat= val.begin() + id[i] * 2;
  j+= id[i + 1] - id[i];
  if constexpr (z) dat[j]= v;
  else dat[j]= M::op(dat[j], v);
  for (; j;) j>>= 1, dat[j]= M::op(dat[2 * j], dat[2 * j + 1]);
 }
 template <bool z> inline void set_(pos_t x, pos_t y, T v) {
  for (int i= 1, p= xy2i(x, y);;) {
   if (seti<z>(i, p - id[i], v); i >= sz) break;
   if (int lc= tol[p] - tol[id[i]], rc= (p - id[i]) - lc; tol[p + 1] - tol[p]) p= id[2 * i] + lc, i= 2 * i;
   else p= id[2 * i + 1] + rc, i= 2 * i + 1;
  }
 }
public:
 template <class P, typename= std::enable_if_t<std::disjunction_v<canbe_Pos<P>, canbe_PosV<P>>>> SegmentTree_2D(const P *p, size_t n) { build<1>(p, n); }
 template <class P, typename= std::enable_if_t<std::disjunction_v<canbe_Pos<P>, canbe_PosV<P>>>> SegmentTree_2D(const std::vector<P> &p): SegmentTree_2D(p.data(), p.size()) {}
 template <class P, typename= std::enable_if_t<canbe_Pos<P>::value>> SegmentTree_2D(const std::set<P> &p): SegmentTree_2D(std::vector(p.begin(), p.end())) {}
 template <class P, class U, typename= std::enable_if_t<canbe_Pos_and_T_v<P, U>>> SegmentTree_2D(const P *p, size_t n, const U &v) { build<1>(p, n, v); }
 template <class P, class U, typename= std::enable_if_t<canbe_Pos_and_T_v<P, U>>> SegmentTree_2D(const std::vector<P> &p, const U &v): SegmentTree_2D(p.data(), p.size(), v) {}
 template <class P, class U, typename= std::enable_if_t<canbe_Pos_and_T_v<P, U>>> SegmentTree_2D(const std::set<P> &p, const U &v): SegmentTree_2D(std::vector(p.begin(), p.end()), v) {}
 template <class P, class U, typename= std::enable_if_t<canbe_Pos_and_T_v<P, U>>> SegmentTree_2D(const std::pair<P, U> *p, size_t n) { build<0>(p, n); }
 template <class P, class U, typename= std::enable_if_t<canbe_Pos_and_T_v<P, U>>> SegmentTree_2D(const std::vector<std::pair<P, U>> &p): SegmentTree_2D(p.data(), p.size()) {}
 template <class P, class U, typename= std::enable_if_t<canbe_Pos_and_T_v<P, U>>> SegmentTree_2D(const std::map<P, U> &p): SegmentTree_2D(std::vector(p.begin(), p.end())) {}
 // [l,r) x [u,d)
 T fold(pos_t l, pos_t r, pos_t u, pos_t d) const {
  T ret= M::ti();
  int L= x2i(l), R= x2i(r);
  auto dfs= [&](auto &dfs, int i, int a, int b, int c, int d) -> void {
   if (c == d || R <= a || b <= L) return;
   if (L <= a && b <= R) return ret= M::op(ret, fold(i, c, d)), void();
   int m= (a + b) / 2, ac= tol[id[i] + c] - tol[id[i]], bc= c - ac, ad= tol[id[i] + d] - tol[id[i]], bd= d - ad;
   dfs(dfs, i * 2, a, m, ac, ad), dfs(dfs, i * 2 + 1, m, b, bc, bd);
  };
  return dfs(dfs, 1, 0, sz, y2i(u), y2i(d)), ret;
 }
 void set(pos_t x, pos_t y, T v) { set_<1>(x, y, v); }
 void mul(pos_t x, pos_t y, T v) { set_<0>(x, y, v); }
 T get(pos_t x, pos_t y) const { return val[xy2i(x, y) + id[2]]; }
};
template <class T> class WaveletMatrix {
 struct SuccinctIndexableDictionary {
  std::size_t len, blocks, zeros;
  std::vector<unsigned> bit, sum;
  SuccinctIndexableDictionary()= default;
  SuccinctIndexableDictionary(std::size_t len): len(len), blocks((len >> 5) + 1), bit(blocks, 0), sum(blocks, 0) {}
  void set(int k) { bit[k >> 5]|= 1U << (k & 31); }
  void build() {
   for (std::size_t i= 1; i < blocks; i++) sum[i]= sum[i - 1] + __builtin_popcount(bit[i - 1]);
   zeros= rank0(len);
  }
  bool operator[](int k) const { return (bit[k >> 5] >> (k & 31)) & 1; }
  std::size_t rank(std::size_t k) const { return (sum[k >> 5] + __builtin_popcount(bit[k >> 5] & ((1U << (k & 31)) - 1))); }
  std::size_t rank0(std::size_t k) const { return k - rank(k); }
 };
 std::size_t len, lg;
 std::vector<SuccinctIndexableDictionary> mat;
 std::vector<T> vec;
public:
 WaveletMatrix(const std::vector<T> &v): len(v.size()), lg(32 - __builtin_clz(std::max<int>(len, 1))), mat(lg, len), vec(v) {
  std::sort(vec.begin(), vec.end());
  vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
  std::vector<unsigned> cur(len), nex(len);
  for (int i= len; i--;) cur[i]= std::lower_bound(vec.begin(), vec.end(), v[i]) - vec.begin();
  for (auto h= lg; h--; cur.swap(nex)) {
   for (std::size_t i= 0; i < len; i++)
    if ((cur[i] >> h) & 1) mat[h].set(i);
   mat[h].build();
   std::array it{nex.begin(), nex.begin() + mat[h].zeros};
   for (std::size_t i= 0; i < len; i++) *it[mat[h][i]]++= cur[i];
  }
 }
 // k-th(0-indexed) smallest number in v[l,r)
 T kth_smallest(int l, int r, int k) const {
  assert(k < r - l);
  std::size_t ret= 0;
  for (auto h= lg; h--;)
   if (auto l0= mat[h].rank0(l), r0= mat[h].rank0(r); k >= r0 - l0) {
    k-= r0 - l0, ret|= 1 << h;
    l+= mat[h].zeros - l0, r+= mat[h].zeros - r0;
   } else l= l0, r= r0;
  return vec[ret];
 }
 // k-th(0-indexed) largest number in v[l,r)
 T kth_largest(int l, int r, int k) const { return kth_smallest(l, r, r - l - k - 1); }
 // count i s.t. (l <= i < r) && (v[i] < ub)
 std::size_t count(int l, int r, T ub) const {
  std::size_t x= std::lower_bound(vec.begin(), vec.end(), ub) - vec.begin();
  if (x >= 1u << lg) return r - l;
  if (x == 0) return 0;
  std::size_t ret= 0;
  for (auto h= lg; h--;)
   if (auto l0= mat[h].rank0(l), r0= mat[h].rank0(r); (x >> h) & 1) ret+= r0 - l0, l+= mat[h].zeros - l0, r+= mat[h].zeros - r0;
   else l= l0, r= r0;
  return ret;
 }
 // count i s.t. (l <= i < r) && (lb <= v[i] < ub)
 std::size_t count(int l, int r, T lb, T ub) const { return count(l, r, ub) - count(l, r, lb); }
};
using namespace std;
struct RSQ {
 using T= long long;
 static T ti() { return 0; }
 static T op(T a, T b) { return a + b; }
};
signed main() {
 cin.tie(0);
 ios::sync_with_stdio(0);
 int N, Q;
 cin >> N >> Q;
 vector<int> A(N);
 vector<array<int, 3>> xyv(N);
 for (int i= 0; i < N; ++i) cin >> A[i], xyv[i]= {i, A[i], A[i]};
 WaveletMatrix wm(A);
 SegmentTree_2D<int, RSQ> seg(xyv);
 for (int i= 0; i < Q; ++i) {
  int L, R;
  cin >> L >> R, --L;
  int k= (R - L - 1) / 2;
  long long u= wm.kth_smallest(L, R, k);
  long long ans= u * k - seg.fold(L, R, -1e9, u);
  ans+= seg.fold(L, R, u, 1e9) - u * (R - L - k);
  cout << ans << '\n';
 }
 return 0;
}
0