結果
問題 | No.956 Number of Unbalanced |
ユーザー | paruki |
提出日時 | 2020-02-03 19:18:50 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 310 ms / 2,000 ms |
コード長 | 2,682 bytes |
コンパイル時間 | 1,857 ms |
コンパイル使用メモリ | 172,576 KB |
実行使用メモリ | 9,472 KB |
最終ジャッジ日時 | 2024-09-19 04:56:53 |
合計ジャッジ時間 | 4,507 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 3 ms
5,888 KB |
testcase_01 | AC | 4 ms
5,888 KB |
testcase_02 | AC | 3 ms
5,760 KB |
testcase_03 | AC | 3 ms
5,888 KB |
testcase_04 | AC | 3 ms
5,888 KB |
testcase_05 | AC | 3 ms
5,888 KB |
testcase_06 | AC | 67 ms
8,576 KB |
testcase_07 | AC | 64 ms
8,448 KB |
testcase_08 | AC | 64 ms
8,704 KB |
testcase_09 | AC | 60 ms
8,192 KB |
testcase_10 | AC | 59 ms
8,192 KB |
testcase_11 | AC | 129 ms
9,088 KB |
testcase_12 | AC | 64 ms
8,704 KB |
testcase_13 | AC | 13 ms
8,076 KB |
testcase_14 | AC | 66 ms
9,472 KB |
testcase_15 | AC | 14 ms
8,192 KB |
testcase_16 | AC | 63 ms
9,088 KB |
testcase_17 | AC | 14 ms
8,192 KB |
testcase_18 | AC | 67 ms
9,344 KB |
testcase_19 | AC | 14 ms
8,212 KB |
testcase_20 | AC | 70 ms
9,472 KB |
testcase_21 | AC | 12 ms
8,080 KB |
testcase_22 | AC | 14 ms
8,192 KB |
testcase_23 | AC | 62 ms
9,088 KB |
testcase_24 | AC | 14 ms
8,160 KB |
testcase_25 | AC | 310 ms
8,320 KB |
testcase_26 | AC | 64 ms
8,192 KB |
testcase_27 | AC | 59 ms
8,064 KB |
testcase_28 | AC | 307 ms
8,320 KB |
testcase_29 | AC | 67 ms
9,472 KB |
コンパイルメッセージ
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/vector:64, from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/queue:61, from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/x86_64-pc-linux-gnu/bits/stdc++.h:86, from main.cpp:2: In member function 'std::vector<_Tp, _Alloc>::size_type std::vector<_Tp, _Alloc>::size() const [with _Tp = int; _Alloc = std::allocator<int>]', inlined from 'void solve()' at main.cpp:71:23: /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/stl_vector.h:988:40: warning: iteration 100000 invokes undefined behavior [-Waggressive-loop-optimizations] 988 | { return size_type(this->_M_impl._M_finish - this->_M_impl._M_start); } | ~~~~~~~~~~~~~~^~~~~~~~~ main.cpp: In function 'void solve()': main.cpp:69:23: note: within this loop 69 | for (int A = 1; A <= MA; ++A) { | ~~^~~~~
ソースコード
#define _USE_MATH_DEFINES #include "bits/stdc++.h" using namespace std; #define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i)) #define rep(i,j) FOR(i,0,j) #define each(x,y) for(auto &(x):(y)) #define mp make_pair #define MT make_tuple #define all(x) (x).begin(),(x).end() #define debug(x) cout<<#x<<": "<<(x)<<endl #define smax(x,y) (x)=max((x),(y)) #define smin(x,y) (x)=min((x),(y)) #define MEM(x,y) memset((x),(y),sizeof (x)) #define sz(x) (int)(x).size() #define RT return using ll = long long; using pii = pair<int, int>; using vi = vector<int>; using vll = vector<ll>; template<class Val> struct BinaryIndexedTree { int n; vector<Val> t; BinaryIndexedTree() {} BinaryIndexedTree(int _n) :n(_n + 1), t(_n + 1) {} void add(int k, Val val) { k++; while (k < n) { t[k] += val; k += (k&-k); } } void set(int k, Val val) { add(k, -sum(k, k + 1)); add(k, val); } Val sum(int k) { Val r = 0; while (k > 0) { r += t[k]; k -= (k&-k); } return r; } Val sum(int l, int r) { return sum(r) - sum(l); } }; const int MA = 100001, SQ = 320; vi indices[MA]; void solve() { int N; cin >> N; rep(i, N) { int A; cin >> A; indices[A].push_back(i); } vi flag(N, -1); ll ans = 0; BinaryIndexedTree<ll> bit(2 * N + 2); for (int A = 1; A <= MA; ++A) { auto &ids = indices[A]; const int M = sz(ids); each(id, ids) { flag[id] = 1; } if (M < SQ) { // 最も左の要素を固定 rep(i, M) { int p = ids[i]; // 左にいくつ0があるか int leftZeroes = (i ? ids[i] - ids[i - 1] - 1 : p); int cnt = 0; FOR(q, p, min(N, p + 2 * SQ)) { cnt += flag[q]; // cnt, cnt-1, cnt-2, ,,, cnt-leftZeroes // 1以上を数える if (cnt > 0) { ans += min(cnt, leftZeroes + 1); } } } } else { int sum = 0; fill(all(bit.t), 0); bit.add(N, 1); for (int i = 0; i < N; ++i) { sum += flag[i]; ans += bit.sum(N + sum); bit.add(N + sum, 1); } } each(id, ids) { flag[id] = -1; } } cout << ans << endl; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(15); solve(); return 0; }