結果

問題 No.956 Number of Unbalanced
ユーザー krotonkroton
提出日時 2019-12-05 01:39:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 252 ms / 2,000 ms
コード長 3,584 bytes
コンパイル時間 1,220 ms
コンパイル使用メモリ 114,896 KB
実行使用メモリ 17,892 KB
最終ジャッジ日時 2023-08-21 12:50:43
合計ジャッジ時間 6,686 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
10,436 KB
testcase_01 AC 7 ms
10,372 KB
testcase_02 AC 6 ms
10,332 KB
testcase_03 AC 7 ms
10,444 KB
testcase_04 AC 7 ms
10,392 KB
testcase_05 AC 6 ms
10,392 KB
testcase_06 AC 150 ms
14,140 KB
testcase_07 AC 162 ms
13,124 KB
testcase_08 AC 143 ms
13,916 KB
testcase_09 AC 112 ms
12,064 KB
testcase_10 AC 124 ms
11,800 KB
testcase_11 AC 185 ms
15,940 KB
testcase_12 AC 150 ms
14,176 KB
testcase_13 AC 76 ms
14,156 KB
testcase_14 AC 249 ms
17,788 KB
testcase_15 AC 77 ms
13,136 KB
testcase_16 AC 166 ms
16,328 KB
testcase_17 AC 73 ms
13,080 KB
testcase_18 AC 250 ms
17,776 KB
testcase_19 AC 81 ms
14,064 KB
testcase_20 AC 252 ms
17,892 KB
testcase_21 AC 78 ms
14,028 KB
testcase_22 AC 72 ms
13,136 KB
testcase_23 AC 167 ms
16,144 KB
testcase_24 AC 82 ms
13,720 KB
testcase_25 AC 114 ms
11,508 KB
testcase_26 AC 89 ms
11,536 KB
testcase_27 AC 112 ms
11,552 KB
testcase_28 AC 114 ms
11,892 KB
testcase_29 AC 252 ms
17,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
#define fst first
#define snd second

/* clang-format off */
template <class T, size_t D> struct _vec { using type = vector<typename _vec<T, D - 1>::type>; };
template <class T> struct _vec<T, 0> { using type = T; };
template <class T, size_t D> using vec = typename _vec<T, D>::type;
template <class T> vector<T> make_v(size_t size, const T& init) { return vector<T>(size, init); }
template <class... Ts> auto make_v(size_t size, Ts... rest) { return vector<decltype(make_v(rest...))>(size, make_v(rest...)); }
template <class T> inline void chmin(T &a, const T& b) { if (b < a) a = b; }
template <class T> inline void chmax(T &a, const T& b) { if (b > a) a = b; }
/* clang-format on */

template <class T>
struct time_array {
 private:
  int t;
  vector<int> ts;
  vector<T> v;

 public:
  time_array() {
  }
  time_array(int n)
      : t(0), ts(n, 0), v(n) {
  }

  T& operator[](int i) {
    if (ts[i] < t) {
      ts[i] = t;
      v[i] = T{};
    }
    return v[i];
  }

  int size() const {
    return v.size();
  }

  void clear() {
    ++t;
  }
};

template <class T>
struct time_fenwick_tree {
  time_array<T> x;
  time_fenwick_tree(int n)
      : x(n + 1) {
  }
  void add(int k, T a) {
    for (++k; k < x.size(); k += k & -k) x[k] += a;
  }
  void add(int l, int r, T a) {
    add(l, a);
    add(r, -a);
  }
  T sum(int k) {
    T s = 0;
    for (; k > 0; k &= k - 1) s += x[k];
    return s;
  }
  void clear() {
    x.clear();
  }
};

struct poly_bit {
 private:
  int n;
  time_fenwick_tree<ll> bit2, bit1, bit0;

  // c2 * k^2 + c1 * k + c0
  ll sum(int k) {
    ll c2 = bit2.sum(k + 1);
    ll c1 = bit1.sum(k + 1);
    ll c0 = bit0.sum(k + 1);
    return c2 * k * k + c1 * k + c0;
  }

 public:
  poly_bit(int n_)
      : n(n_), bit2(n), bit1(n), bit0(n) {
  }

  // [l, r) += x
  void add(int l, int r) {
    // [l, r)
    bit2.add(l, r, 1);
    bit1.add(l, r, 3 - 2 * l);
    bit0.add(l, r, 1ll * l * l - 3 * l + 2);
    // [r, inf)
    bit1.add(r, 2 * (r - l));
    bit0.add(r, 1ll * l * l - 3 * l - 1ll * r * r + 3 * r);
  }

  // [l, r)
  ll sum(int l, int r) {
    return (sum(r - 1) - sum(l - 1)) / 2;
  }

  void clear() {
    bit2.clear();
    bit1.clear();
    bit0.clear();
  }
};

const int M = 210000, GETA = 105000;

ll solve(const vector<int>& A) {
  int N = A.size();
  map<int, vector<int>> mp;
  for (int i = 0; i < N; i++) mp[A[i]].push_back(i + 1);
  ll res = 0;
  poly_bit pbit(M);
  for (auto& entry : mp) {
    vector<int> ind = entry.snd;
    ind.insert(ind.begin(), -1);
    ind.push_back(N + 1);
    vector<pair<int, int>> ps;
    for (int i = 1, c = 0; i < ind.size(); i++, c++) {
      int pre = 2 * c - ind[i - 1];
      int cur = 2 * (c + 1) - ind[i];
      ps.emplace_back(cur - 1, pre);
      if (i + 1 < ind.size()) {
        ps.emplace_back(cur, cur + 1);
      }
    }
    for (auto p : ps) {
      int L = p.fst + GETA, R = p.snd + GETA;
      res += pbit.sum(L - 1, R - 1);
      pbit.add(L, R);
    }
    pbit.clear();
  }
  return res;
}

int main() {
#ifdef DEBUG
  ifstream ifs("in.txt");
  cin.rdbuf(ifs.rdbuf());
#endif
  int N;
  while (cin >> N) {
    vector<int> A(N);
    for (int& x : A) cin >> x;
    cout << solve(A) << endl;
  }
  return 0;
}
0