結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー ninoinuininoinui
提出日時 2021-04-04 08:58:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 39 ms / 2,000 ms
コード長 4,948 bytes
コンパイル時間 2,202 ms
コンパイル使用メモリ 206,660 KB
実行使用メモリ 9,856 KB
最終ジャッジ日時 2024-06-08 00:39:46
合計ジャッジ時間 4,670 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 34 ms
8,960 KB
testcase_04 AC 39 ms
9,728 KB
testcase_05 AC 32 ms
8,832 KB
testcase_06 AC 31 ms
8,320 KB
testcase_07 AC 38 ms
9,728 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 17 ms
7,040 KB
testcase_10 AC 29 ms
9,728 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 39 ms
9,728 KB
testcase_13 AC 37 ms
9,856 KB
testcase_14 AC 38 ms
9,728 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 5 ms
5,376 KB
testcase_24 AC 14 ms
5,504 KB
testcase_25 AC 29 ms
8,064 KB
testcase_26 AC 8 ms
5,376 KB
testcase_27 AC 17 ms
6,144 KB
testcase_28 AC 23 ms
7,040 KB
testcase_29 AC 29 ms
8,320 KB
testcase_30 AC 36 ms
9,344 KB
testcase_31 AC 10 ms
5,376 KB
testcase_32 AC 7 ms
5,376 KB
testcase_33 AC 29 ms
8,832 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 1 ms
5,376 KB
testcase_36 AC 1 ms
5,376 KB
testcase_37 AC 1 ms
5,376 KB
testcase_38 AC 2 ms
5,376 KB
testcase_39 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifndef _DEBUG
#define _DEBUG
#include __FILE__

signed main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) cin >> A.at(i), A.at(i)--;
  vector<int> B(N);
  for (int i = 0; i < N; i++) cin >> B.at(i), B.at(i)--;

  cout << inv_count(A, B) << "\n";
}

#else
// #undef _DEBUG
#pragma GCC diagnostic warning "-Wextra"
#pragma GCC diagnostic warning "-Wshadow"
#include <bits/stdc++.h>
using namespace std;

template <typename T> struct FenwickTree {
private:
  int n;
  vector<T> bit;

public:
  FenwickTree() {}
  FenwickTree(int sz) : n(sz + 1), bit(n, 0) {}
  FenwickTree(const vector<T>& v) : n(v.size() + 1), bit(n, 0) {
    for (int i = 0; i < n - 1; i++) add(i, v[i]);
  }

  void add(int i, T x) {
    i++;
    while (i < n) bit[i] += x, i += i & -i;
  }

  void add(int l, int r, T x) { add(l, x), add(r, -x); }

  T sum(int i) {
    i++;
    T ret = 0;
    while (i > 0) ret += bit[i], i -= i & -i;
    return ret;
  }

  T sum(int l, int r) { return sum(r - 1) - sum(l - 1); }

  T at(int i) { return sum(i, i + 1); }

  void print() {
    cout << "[FT/at]: {";
    for (int i = 0; i < n; i++) {
      cout << at(i) << (i != n - 1 ? ", " : "}\n");
    }
    cout << "[FT/cs]: {";
    for (int i = 0; i < n; i++) {
      cout << sum(i) << (i != n - 1 ? ", " : "}\n");
    }
  }
};

long inv_count(const vector<int>& u) {
  int n = u.size();
  FenwickTree<int> bt(n);
  long ans = 0;
  for (int i = 0; i < n; i++) {
    ans += i - bt.sum(u[i]);
    bt.add(u[i], 1);
  }
  return ans;
}

long inv_count(const vector<int>& u, const vector<int>& v) {
  int n = u.size();
  vector<vector<int>> p(n);
  FenwickTree<int> bt(n);
  for (int i = n - 1; i >= 0; --i) {
    p[u[i]].push_back(i);
  }
  long ans = 0;
  for (int i = 0; i < n; ++i) {
    int pos = p[v[i]].back();
    p[v[i]].pop_back();
    ans += pos - bt.sum(pos);
    bt.add(pos, 1);
  }
  return ans;
}

struct io_setup {
  io_setup() { cin.tie(nullptr)->sync_with_stdio(false); }
} io_setup;

template <class A, class B> bool cmin(A& a, const B& b) {
  if (a > b) return a = b, true;
  return false;
}

template <class A, class B> bool cmax(A& a, const B& b) {
  if (a < b) return a = b, true;
  return false;
}

template <class A, class B> ostream& operator<<(ostream& os, const pair<A, B>& p);

template <class A, class B, class C> ostream& operator<<(ostream& os, const tuple<A, B, C>& t);

template <class A, class B, class C, class D> ostream& operator<<(ostream& os, const tuple<A, B, C, D>& t);

#define foreach(it, a) for (auto it = a.begin(); it != a.end(); it++)

ostream& operator<<(ostream& os, const vector<char>& vec) {
  os << "{";
  foreach (it, vec) { os << (it == vec.begin() ? "" : " ") << *it; }
  return os << "}";
}

template <class A> ostream& operator<<(ostream& os, const vector<A>& vec) {
  os << "{";
  foreach (it, vec) { os << (it == vec.begin() ? "" : ", ") << *it; }
  return os << "}";
}

template <class A> ostream& operator<<(ostream& os, const vector<vector<A>>& vec) {
  os << "{\n";
  foreach (it, vec) { os << (it == vec.begin() ? " " : "\n ") << *it; }
  return os << "\n}";
}

template <class A> ostream& operator<<(ostream& os, const vector<vector<vector<A>>>& vec) {
  os << "{\n";
  foreach (it, vec) { os << (it == vec.begin() ? "" : "\n") << it - vec.begin() << ": " << *it; }
  return os << "\n}";
}

template <class A> ostream& operator<<(ostream& os, const set<A>& se) {
  os << "{";
  foreach (it, se) { os << (it == se.begin() ? "" : ", ") << *it; }
  return os << "}";
}

template <class A> ostream& operator<<(ostream& os, const multiset<A>& ms) {
  os << "{";
  foreach (it, ms) { os << (it == ms.begin() ? "" : ", ") << *it; }
  return os << "}";
}

template <class A, class B> ostream& operator<<(ostream& os, const map<A, B>& ma) {
  os << "{";
  foreach (it, ma) { os << (it == ma.begin() ? "" : ", ") << *it; }
  return os << "}";
}

template <class A> ostream& operator<<(ostream& os, priority_queue<A> pq) {
  os << "{";
  while (!pq.empty()) {
    os << pq.top(), pq.pop();
    if (!pq.empty()) os << ", ";
  }
  return os << "}";
}

template <class A, class B> ostream& operator<<(ostream& os, const pair<A, B>& p) {
  const auto& [a, b] = p;
  return os << "(" << a << ", " << b << ")";
}

template <class A, class B, class C> ostream& operator<<(ostream& os, const tuple<A, B, C>& t) {
  const auto& [a, b, c] = t;
  return os << "(" << a << ", " << b << ", " << c << ")";
}

template <class A, class B, class C, class D> ostream& operator<<(ostream& os, const tuple<A, B, C, D>& t) {
  const auto& [a, b, c, d] = t;
  return os << "(" << a << ", " << b << ", " << c << ", " << d << ")";
}

void dump_func() {}
template <class A, class... B> void dump_func(const A& a, const B&... b) {
  cout << a << (sizeof...(b) ? ", " : "\n");
  dump_func(b...);
}

#ifdef _DEBUG
#define dump(...) cout << "[" << string(#__VA_ARGS__) << "]: ", dump_func(__VA_ARGS__)
#else
#define dump(...)
#endif
#endif
0