結果

問題 No.1282 Display Elements
ユーザー kk
提出日時 2020-12-25 15:59:52
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 215 ms / 2,000 ms
コード長 1,398 bytes
コンパイル時間 3,093 ms
コンパイル使用メモリ 215,732 KB
実行使用メモリ 15,632 KB
最終ジャッジ日時 2023-10-22 07:43:35
合計ジャッジ時間 5,812 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 3 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 3 ms
4,348 KB
testcase_15 AC 186 ms
14,840 KB
testcase_16 AC 61 ms
7,976 KB
testcase_17 AC 151 ms
12,728 KB
testcase_18 AC 81 ms
9,296 KB
testcase_19 AC 33 ms
4,808 KB
testcase_20 AC 32 ms
4,808 KB
testcase_21 AC 213 ms
15,632 KB
testcase_22 AC 50 ms
4,808 KB
testcase_23 AC 215 ms
15,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define REP(i,n) for(int i=0; i<(int)(n); i++)

template <typename T>
class fenwick_tree {
  vector<T> data;
public:
  fenwick_tree() {}
  // manage data in [1, n]
  fenwick_tree(int n) : data(n + 1) {}

  void init() {
    fill(data.begin(), data.end(), 0);
  }

  // return sum in [1, i]
  T sum(int i){
    T s = 0;
    while(i > 0){
      s += data[i];
      i -= i & -i;
    }
    return s;
  }

  // return sum in [l, r]
  T sum(int l, int r) {
    return sum(r) - sum(l-1);
  }
  
  void add(int i, T x){
    while(i < (int)data.size()){
      data[i] += x;
      i += i & -i;
    }
  }
};

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n;
  cin >> n;

  vector<int> a(n), b(n);
  vector<int> v(2*n);

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    v[i] = a[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    v[n+i] = b[i];
  }

  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  int m = v.size();

  map<int, int> idx;
  for (int i = 0; i < m; i++)
    idx[v[i]] = i;
  
  for (int i = 0; i < n; i++) {
    a[i] = idx[a[i]] + 1;
    b[i] = idx[b[i]] + 1;
  }
  
  sort(a.begin(), a.end());
  
  fenwick_tree<long long> tree(m);
  long long ret = 0;
  for (int i = 0; i < n; i++) {
    tree.add(b[i], 1);
    ret += tree.sum(a[i] - 1);
  }
  cout << ret << endl;
  
  return 0;
}
0