結果

問題 No.1919 Many Monster Battles
ユーザー ei1333333ei1333333
提出日時 2022-04-29 22:12:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 7,929 bytes
コンパイル時間 4,997 ms
コンパイル使用メモリ 284,132 KB
実行使用メモリ 34,592 KB
最終ジャッジ日時 2023-09-11 13:28:03
合計ジャッジ時間 9,957 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 8 ms
4,380 KB
testcase_04 AC 8 ms
4,376 KB
testcase_05 AC 9 ms
4,380 KB
testcase_06 AC 9 ms
4,380 KB
testcase_07 AC 9 ms
4,376 KB
testcase_08 AC 8 ms
4,376 KB
testcase_09 AC 9 ms
4,380 KB
testcase_10 AC 8 ms
4,376 KB
testcase_11 AC 9 ms
4,376 KB
testcase_12 AC 9 ms
4,380 KB
testcase_13 TLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>

using namespace std;

using mint = atcoder::modint1000000007;
const int inf = (1 << 30) - 1;

#line 1 "structure/wavelet/succinct-indexable-dictionary.cpp"

/**
 * @brief Succinct Indexable Dictionary(完備辞書)
 */
struct SuccinctIndexableDictionary {
  size_t length;
  size_t blocks;
  vector< unsigned > bit, sum;

  SuccinctIndexableDictionary() = default;

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

#line 1 "structure/others/binary-indexed-tree.cpp"

/**
 * @brief Binary-Indexed-Tree(BIT)
 * @docs docs/binary-indexed-tree.md
 */
template< typename T >
struct BinaryIndexedTree {
private:
  int n;
  vector< T > data;

public:
  BinaryIndexedTree() = default;

  explicit BinaryIndexedTree(int n) : n(n) {
    data.assign(n + 1, 0);
  }

  explicit BinaryIndexedTree(const vector< T > &v) :
      BinaryIndexedTree((int) v.size()) {
    build(v);
  }

  void build(const vector< T > &v) {
    assert(n == (int) v.size());
    for(int i = 1; i <= n; i++) data[i] = v[i - 1];
    for(int i = 1; i <= n; i++) {
      int j = i + (i & -i);
      if(j <= n) data[j] += data[i];
    }
  }

  void apply(int k, const T &x) {
    for(++k; k <= n; k += k & -k) data[k] += x;
  }

  T prod(int r) const {
    T ret = T();
    for(; r > 0; r -= r & -r) ret += data[r];
    return ret;
  }

  T prod(int l, int r) const {
    return prod(r) - prod(l);
  }

  int lower_bound(T x) const {
    int i = 0;
    for(int k = 1 << (__lg(n) + 1); k > 0; k >>= 1) {
      if(i + k <= n && data[i + k] < x) {
        x -= data[i + k];
        i += k;
      }
    }
    return i;
  }

  int upper_bound(T x) const {
    int i = 0;
    for(int k = 1 << (__lg(n) + 1); k > 0; k >>= 1) {
      if(i + k <= n && data[i + k] <= x) {
        x -= data[i + k];
        i += k;
      }
    }
    return i;
  }
};

#line 3 "structure/wavelet/wavelet-matrix-point-add-rectangle-sum.cpp"

/*
 * @brief Wavelet Matrix Point Add Rectangle Sum
 * @docs docs/wavelet-matrix-point-add-rectangle-sum.md
 */
template< typename T, int MAXLOG, typename D >
struct WaveletMatrixPointAddRectangleSum {
  size_t length;
  SuccinctIndexableDictionary matrix[MAXLOG];
  BinaryIndexedTree< D > ds[MAXLOG];
  vector< T > v;
  int mid[MAXLOG];

  WaveletMatrixPointAddRectangleSum() = default;

  WaveletMatrixPointAddRectangleSum(const vector< T > &v, const vector< D > &d) : length(v.size()), v(v) {
    assert(v.size() == d.size());
    vector< int > l(length), r(length), ord(length);
    iota(begin(ord), end(ord), 0);
    vector< D > dd(length);
    for(int level = MAXLOG - 1; level >= 0; level--) {
      matrix[level] = SuccinctIndexableDictionary(length + 1);
      int left = 0, right = 0;
      for(int i = 0; i < length; i++) {
        if(((v[ord[i]] >> level) & 1)) {
          matrix[level].set(i);
          r[right++] = ord[i];
        } else {
          l[left++] = ord[i];
        }
      }
      mid[level] = left;
      matrix[level].build();
      ord.swap(l);
      for(int i = 0; i < right; i++) {
        ord[left + i] = r[i];
      }
      for(int i = 0; i < length; i++) {
        dd[i] = d[ord[i]];
      }
      ds[level] = BinaryIndexedTree< D >(dd);
    }
  }

  pair< int, int > succ(bool f, int l, int r, int level) {
    return {matrix[level].rank(f, l) + mid[level] * f, matrix[level].rank(f, r) + mid[level] * f};
  }

  // count d[i] s.t. (l <= i < r) && (v[i] < upper)
  D rect_sum(int l, int r, T upper) {
    D ret = 0;
    for(int level = MAXLOG - 1; level >= 0; level--) {
      if(((upper >> level) & 1)) {
        auto nxt = succ(false, l, r, level);
        ret += ds[level].prod(nxt.first, nxt.second);
        l = l - nxt.first + mid[level];
        r = r - nxt.second + mid[level];
      } else {
        tie(l, r) = succ(false, l, r, level);
      }
    }
    return ret;
  }

  D rect_sum(int l, int r, T lower, T upper) {
    return rect_sum(l, r, upper) - rect_sum(l, r, lower);
  }

  void point_add(int k, const D &x) {
    auto &y = v[k];
    for(int level = MAXLOG - 1; level >= 0; level--) {
      bool f = ((y >> level) & 1);
      k = matrix[level].rank(f, k) + mid[level] * f;
      ds[level].apply(k, x);
    }
  }
};

template< typename T, int MAXLOG, typename D >
struct CompressedWaveletMatrixPointAddRectangleSum {
  WaveletMatrixPointAddRectangleSum< int, MAXLOG, D > mat;
  vector< T > ys;

  CompressedWaveletMatrixPointAddRectangleSum(const vector< T > &v, const vector< D > &d) : ys(v) {
    sort(begin(ys), end(ys));
    ys.erase(unique(begin(ys), end(ys)), end(ys));
    vector< int > t(v.size());
    for(int i = 0; i < v.size(); i++) t[i] = get(v[i]);
    mat = WaveletMatrixPointAddRectangleSum< int, MAXLOG, D >(t, d);
  }

  inline int get(const T &x) {
    return lower_bound(begin(ys), end(ys), x) - begin(ys);
  }

  D rect_sum(int l, int r, T upper) {
    return mat.rect_sum(l, r, get(upper));
  }

  D rect_sum(int l, int r, T lower, T upper) {
    return mat.rect_sum(l, r, get(lower), get(upper));
  }

  void point_add(int k, const D &x) {
    mat.point_add(k, x);
  }
};

mint solve(const vector< int > &A, const vector< int > &B) {
  int N = (int) A.size();
  mint ret = 0;
  {
    vector< int > ord(N);
    iota(begin(ord), end(ord), 0);
    sort(begin(ord), end(ord), [&](int a, int b) {
      return A[a] < A[b];
    });
    vector< pair< int, int > > pts(N);
    for(int i = 0; i < N; i++) pts[i] = {A[i] - B[i], A[i] + B[i]};
    sort(begin(pts), end(pts));
    pts.erase(unique(begin(pts), end(pts)), end(pts));
    vector< int > xs(pts.size()), ys(pts.size()), ds(pts.size());
    for(int i = 0; i < (int) pts.size(); i++) {
      tie(xs[i], ys[i]) = pts[i];
    }
    CompressedWaveletMatrixPointAddRectangleSum< int, 18, int > bs(ys, ds);
    for(int i: ord) {
      {
        int left = lower_bound(begin(xs), end(xs), A[i] - B[i]) - begin(xs);
        int right = (int) pts.size();
        int pct = bs.rect_sum(0, left, 0, A[i] + B[i]);
        ret += mint(A[i]) * pct;
      }
      {
        int idx = lower_bound(begin(pts), end(pts), make_pair(A[i] - B[i], A[i] + B[i])) - begin(pts);
        bs.point_add(idx, 1);
      }
    }
  }
  {
    vector< int > ord(N);
    iota(begin(ord), end(ord), 0);
    sort(begin(ord), end(ord), [&](int a, int b) {
      return A[a] > A[b];
    });

    vector< pair< int, int > > pts(N);
    for(int i = 0; i < N; i++) pts[i] = {A[i] + B[i], A[i] - B[i]};
    sort(begin(pts), end(pts));
    pts.erase(unique(begin(pts), end(pts)), end(pts));
    vector< int > xs(pts.size()), ys(pts.size()), ds(pts.size());
    for(int i = 0; i < (int) pts.size(); i++) {
      tie(xs[i], ys[i]) = pts[i];
    }
    CompressedWaveletMatrixPointAddRectangleSum< int, 18, int > bs(ys, ds);
    for(int i: ord) {
      {
        int left = lower_bound(begin(xs), end(xs), A[i] + B[i] + 1) - begin(xs);
        int right = (int) pts.size();
        int pct = bs.rect_sum(left, xs.size(), A[i] - B[i] + 1, inf);
        ret -= mint(A[i]) * pct;
      }
      {
        int idx = lower_bound(begin(pts), end(pts), make_pair(A[i] + B[i], A[i] - B[i])) - begin(pts);
        bs.point_add(idx, 1);
      }
    }
  }
  return ret * 2;
}

int main() {
  int N;
  cin >> N;
  vector< int > A(N), B(N);
  for(auto &a: A) cin >> a;
  for(auto &b: B) cin >> b;
  cout << solve(A, B).val() << " " << solve(B, A).val() << "\n";
}
0