結果

問題 No.956 Number of Unbalanced
ユーザー pekempeypekempey
提出日時 2019-12-19 00:43:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,757 bytes
コンパイル時間 1,576 ms
コンパイル使用メモリ 170,096 KB
実行使用メモリ 4,816 KB
最終ジャッジ日時 2023-09-21 04:34:25
合計ジャッジ時間 25,730 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,376 KB
testcase_01 AC 3 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 874 ms
4,728 KB
testcase_07 AC 796 ms
4,496 KB
testcase_08 AC 907 ms
4,648 KB
testcase_09 AC 909 ms
4,508 KB
testcase_10 AC 793 ms
4,652 KB
testcase_11 AC 905 ms
4,548 KB
testcase_12 AC 898 ms
4,588 KB
testcase_13 AC 1,037 ms
4,648 KB
testcase_14 AC 930 ms
4,532 KB
testcase_15 TLE -
testcase_16 AC 755 ms
4,552 KB
testcase_17 AC 813 ms
4,588 KB
testcase_18 AC 930 ms
4,504 KB
testcase_19 AC 973 ms
4,500 KB
testcase_20 AC 931 ms
4,588 KB
testcase_21 AC 1,044 ms
4,580 KB
testcase_22 AC 867 ms
4,504 KB
testcase_23 AC 753 ms
4,576 KB
testcase_24 AC 982 ms
4,520 KB
testcase_25 AC 753 ms
4,588 KB
testcase_26 AC 1,050 ms
4,496 KB
testcase_27 AC 739 ms
4,736 KB
testcase_28 AC 751 ms
4,532 KB
testcase_29 AC 928 ms
4,556 KB
権限があれば一括ダウンロードができます

ソースコード

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;
  }

  void clear() {
    fill(range(dat), 0);
  }
};

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 = 1200;
  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(i + 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(i + 2*S+10, N); j++) {
      cnt[A[j]]--;
    }
  }
  BIT<int> bit(100001);
  for (int v : large) {
    int mn = 0;
    int mx = 0;
    int f = 0;
    for (int i = 0; i < N; i++) {
      if (A[i] == v) {
        f++;
      } else {
        f--;
      }
      mn = min(mn, f);
      mx = max(mx, f);
    }
    f = -mn;
    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);
    }
    bit.clear();
  }
  cout << ans << endl;
}
0