結果

問題 No.956 Number of Unbalanced
ユーザー parukiparuki
提出日時 2020-02-03 19:18:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 362 ms / 2,000 ms
コード長 2,682 bytes
コンパイル時間 1,946 ms
コンパイル使用メモリ 173,716 KB
実行使用メモリ 9,588 KB
最終ジャッジ日時 2023-10-19 08:43:09
合計ジャッジ時間 5,867 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,020 KB
testcase_01 AC 5 ms
6,016 KB
testcase_02 AC 5 ms
6,020 KB
testcase_03 AC 4 ms
6,020 KB
testcase_04 AC 5 ms
6,020 KB
testcase_05 AC 5 ms
6,020 KB
testcase_06 AC 77 ms
8,796 KB
testcase_07 AC 74 ms
8,532 KB
testcase_08 AC 75 ms
8,796 KB
testcase_09 AC 69 ms
8,268 KB
testcase_10 AC 68 ms
8,268 KB
testcase_11 AC 150 ms
9,060 KB
testcase_12 AC 75 ms
8,796 KB
testcase_13 AC 14 ms
7,980 KB
testcase_14 AC 87 ms
9,588 KB
testcase_15 AC 17 ms
8,208 KB
testcase_16 AC 71 ms
9,060 KB
testcase_17 AC 17 ms
8,208 KB
testcase_18 AC 87 ms
9,588 KB
testcase_19 AC 17 ms
8,112 KB
testcase_20 AC 87 ms
9,588 KB
testcase_21 AC 14 ms
7,980 KB
testcase_22 AC 17 ms
8,208 KB
testcase_23 AC 70 ms
9,060 KB
testcase_24 AC 16 ms
8,188 KB
testcase_25 AC 362 ms
8,268 KB
testcase_26 AC 73 ms
8,268 KB
testcase_27 AC 67 ms
8,268 KB
testcase_28 AC 361 ms
8,268 KB
testcase_29 AC 88 ms
9,588 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) {
      |                     ~~^~~~~

ソースコード

diff #

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