結果

問題 No.956 Number of Unbalanced
ユーザー 👑 emthrmemthrm
提出日時 2019-12-19 02:40:07
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 102 ms / 2,000 ms
コード長 3,886 bytes
コンパイル時間 1,603 ms
コンパイル使用メモリ 143,644 KB
実行使用メモリ 17,180 KB
最終ジャッジ日時 2023-09-21 04:47:34
合計ジャッジ時間 4,573 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,392 KB
testcase_01 AC 3 ms
5,412 KB
testcase_02 AC 3 ms
5,360 KB
testcase_03 AC 3 ms
5,560 KB
testcase_04 AC 2 ms
5,420 KB
testcase_05 AC 4 ms
5,300 KB
testcase_06 AC 51 ms
6,600 KB
testcase_07 AC 55 ms
6,096 KB
testcase_08 AC 54 ms
6,360 KB
testcase_09 AC 39 ms
6,108 KB
testcase_10 AC 52 ms
5,940 KB
testcase_11 AC 72 ms
6,948 KB
testcase_12 AC 53 ms
6,672 KB
testcase_13 AC 80 ms
17,080 KB
testcase_14 AC 63 ms
7,248 KB
testcase_15 AC 87 ms
9,988 KB
testcase_16 AC 45 ms
6,908 KB
testcase_17 AC 76 ms
10,012 KB
testcase_18 AC 61 ms
7,200 KB
testcase_19 AC 102 ms
11,916 KB
testcase_20 AC 60 ms
7,248 KB
testcase_21 AC 88 ms
17,180 KB
testcase_22 AC 79 ms
10,060 KB
testcase_23 AC 46 ms
6,884 KB
testcase_24 AC 96 ms
12,088 KB
testcase_25 AC 72 ms
6,080 KB
testcase_26 AC 72 ms
5,888 KB
testcase_27 AC 71 ms
6,276 KB
testcase_28 AC 73 ms
6,424 KB
testcase_29 AC 62 ms
7,196 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
// const int dy[] = {1, 1, 0, -1, -1, -1, 0, 1},
//           dx[] = {0, -1, -1, -1, 0, 1, 1, 1};

struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    cerr << fixed << setprecision(10);
  }
} iosetup;
/*-------------------------------------------------*/
template <typename Abelian>
struct BIT {
  BIT(int n, const Abelian &UNITY = 0) : n(n), UNITY(UNITY), dat(n, UNITY) {}

  void add(int idx, const Abelian &value) {
    while (idx < n) {
      dat[idx] += value;
      idx |= idx + 1;
    }
  }

  Abelian sum(int idx) {
    Abelian res = UNITY;
    while (idx >= 0) {
      res += dat[idx];
      idx = (idx & (idx + 1)) - 1;
    }
    return res;
  }

  Abelian sum(int left, int right) {
    if (right < left) return UNITY;
    return sum(right) - sum(left - 1);
  }

  Abelian operator[](const int idx) { return sum(idx, idx); }

  int lower_bound(Abelian value) {
    if (value <= UNITY) return 0;
    int res = 0, exponent = 1;
    while (exponent <= n) exponent <<= 1;
    for (int mask = exponent >> 1; mask > 0; mask >>= 1) {
      if (res + mask - 1 < n && dat[res + mask - 1] < value) {
        value -= dat[res + mask - 1];
        res += mask;
      }
    }
    return res;
  }

private:
  int n;
  const Abelian UNITY;
  vector<Abelian> dat;
};

long long solve(vector<int> &a) {
  int n = a.size();
  vector<int> b(a);
  FOR(i, 1, n) a[i] += a[i - 1];
  map<int, int> cnt;
  REP(i, n) ++cnt[a[i]];
  int m = cnt.size();
  BIT<long long> bit(m);
  int idx = 0;
  for (auto it = cnt.begin(); it != cnt.end(); ++it, ++idx) {
    bit.add(idx, (*it).second);
  }
  int fro = (*cnt.begin()).first, border = 1 - fro;
  long long ans = 0;
  REP(i, n) {
    ans += bit.sum(max(border, 0), m - 1);
    bit.add(a[i] - fro, -1);
    border += b[i];
  }
  return ans;
}

int main() {
  const int A = 100000;
  int n; cin >> n;
  vector<vector<int> > idx(A);
  REP(i, n) {
    int a; cin >> a; --a;
    idx[a].emplace_back(i);
  }
  long long ans = 0;
  REP(now, A) {
    if (idx[now].empty()) continue;
    vector<int> conc;
    int cnt = 0;
    REP(i, idx[now].size()) {
      conc.emplace_back(idx[now][i]);
      ++cnt;
      int j = idx[now][i] + 1;
      while (cnt > 0 && j < n && (i + 1 == idx[now].size() || j < idx[now][i + 1])) {
        conc.emplace_back(j++);
        --cnt;
      }
    }
    cnt = 0;
    for (int i = idx[now].size() - 1; i >= 0; --i) {
      ++cnt;
      int j = idx[now][i] - 1;
      while (cnt > 0 && j >= 0 && (i == 0 || idx[now][i - 1] < j)) {
        conc.emplace_back(j--);
        --cnt;
      }
    }
    sort(ALL(conc));
    conc.erase(unique(ALL(conc)), conc.end());
    set<int> st;
    for (int e : idx[now]) st.emplace(e);
    for (int i = 0; i < conc.size();) {
      vector<int> sub{st.count(conc[i]) == 1 ? 1 : -1};
      int j = i + 1;
      while (j < conc.size() && conc[j] - conc[j - 1] == 1) {
        sub.emplace_back(st.count(conc[j]) == 1 ? 1 : -1);
        ++j;
      }
      ans += solve(sub);
      i = j;
    }
  }
  cout << ans << '\n';
  return 0;
}
0