結果
問題 |
No.956 Number of Unbalanced
|
ユーザー |
|
提出日時 | 2019-12-18 00:18:12 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,009 bytes |
コンパイル時間 | 988 ms |
コンパイル使用メモリ | 103,204 KB |
実行使用メモリ | 12,988 KB |
最終ジャッジ日時 | 2024-07-05 00:44:51 |
合計ジャッジ時間 | 4,921 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 6 |
other | TLE * 1 -- * 23 |
ソースコード
#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 fenwick_tree { vector<T> x; fenwick_tree(int n) : x(n + 1) { } void add(int k, T a) { for (++k; k < x.size(); k += k & -k) x[k] += a; } T sum(int k) { T s = 0; for (; k > 0; k &= k - 1) s += x[k]; return s; } }; ll solve(const vector<int>& A) { int N = A.size(); set<int> st(A.begin(), A.end()); ll res = 0; for (int x : st) { vector<int> S(N + 1, 0); for (int i = 0; i < N; i++) S[i + 1] = S[i] + (A[i] == x); for (int i = 0; i <= N; i++) S[i] = 2 * S[i] - i + N; // S[i] は -N ~ +N まで動く // 0 ~ 2N fenwick_tree<int> bit(2 * N + 1); for (int x : S) { res += bit.sum(x); bit.add(x, 1); } } 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; }