結果

問題 No.956 Number of Unbalanced
ユーザー pekempeypekempey
提出日時 2019-12-19 00:32:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,522 bytes
コンパイル時間 1,657 ms
コンパイル使用メモリ 169,588 KB
実行使用メモリ 11,476 KB
最終ジャッジ日時 2023-09-21 04:30:06
合計ジャッジ時間 5,439 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
11,476 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 TLE -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
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 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n) - 1; i >= 0; i--)
#define range(a) a.begin(), a.end()

template<class T>
struct BIT {
  vector<T> dat;

  BIT(int n) : dat(n + 1) {}

  void update(int k, T v) {
    for (int i = k + 1; i < dat.size(); i += i & -i) {
      dat[i] += v;
    }
  }

  T query(int k) {
    T res = 0;
    for (int i = k + 1; i > 0; i -= i & -i) {
      res += dat[i];
    }
    return res;
  }
};

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int N; cin >> N;
  vector<int> A(N); rep(i, N) cin >> A[i];
  vector<int> freq(100001);
  constexpr int S = 1000;
  rep(i, N) {
    freq[A[i]]++;
  }
  vector<int> large;
  rep(i, 100001) {
    if (freq[i] >= S) {
      large.push_back(i);
    }
  }
  vector<int> cnt(100001);
  ll ans = 0;
  rep(i, N) {
    pair<int, int> mx(0, 0);
    for (int j = i; j < min(j + 2*S+10, N); j++) {
      cnt[A[j]]++;
      mx = max(mx, {cnt[A[j]], A[j]});
      if (mx.first * 2 > j - i + 1 && freq[mx.second] < S) {
        ans++;
      }
    }
    for (int j = i; j < min(j + 2*S+10, N); j++) {
      cnt[A[j]]--;
    }
  }
  for (int v : large) {
    BIT<int> bit(200200);
    constexpr int B = 100100;
    int f = B;
    bit.update(f, 1);
    for (int i = 0; i < N; i++) {
      if (A[i] == v) {
        f++;
      } else {
        f--;
      }
      ans += bit.query(f - 1);
      bit.update(f, 1);
    }
  }
  cout << ans << endl;
}
0