結果

問題 No.1371 交換門松列・松
ユーザー 👑 hitonanodehitonanode
提出日時 2021-01-29 22:11:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 129 ms / 4,000 ms
コード長 2,148 bytes
コンパイル時間 2,050 ms
コンパイル使用メモリ 206,896 KB
実行使用メモリ 29,012 KB
最終ジャッジ日時 2023-09-09 15:35:41
合計ジャッジ時間 5,046 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 63 ms
28,820 KB
testcase_19 AC 63 ms
28,624 KB
testcase_20 AC 65 ms
28,624 KB
testcase_21 AC 65 ms
28,724 KB
testcase_22 AC 65 ms
28,660 KB
testcase_23 AC 122 ms
28,700 KB
testcase_24 AC 126 ms
28,704 KB
testcase_25 AC 129 ms
29,004 KB
testcase_26 AC 128 ms
28,704 KB
testcase_27 AC 124 ms
28,584 KB
testcase_28 AC 128 ms
28,652 KB
testcase_29 AC 127 ms
29,012 KB
testcase_30 AC 128 ms
28,708 KB
testcase_31 AC 123 ms
28,980 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define REP(i, n) FOR(i,0,n)
template <typename T> bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template <typename T> bool chmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; }

// 0-indexed BIT (binary indexed tree / Fenwick tree) (i : [0, len))
template <typename T> struct BIT {
    int n;
    std::vector<T> data;
    BIT(int len = 0) : n(len), data(len) {}
    void reset() { std::fill(data.begin(), data.end(), T(0)); }
    void add(int pos, T v) { // a[pos] += v
        pos++;
        while (pos > 0 and pos <= n) data[pos - 1] += v, pos += pos & -pos;
    }
    T sum(int k) const { // a[0] + ... + a[k - 1]
        T res = 0;
        while (k > 0) res += data[k - 1], k -= k & -k;
        return res;
    }
    T sum(int l, int r) const { return sum(r) - sum(l); } // a[l] + ... + a[r - 1]
};

int main() {
    cin.tie(nullptr), ios::sync_with_stdio(false);
    int N;
    cin >> N;
    vector<int> A(N);
    for (auto &a : A) cin >> a, a--;
    vector<int> pos(N);
    REP(i, N) pos[A[i]] = i;
    BIT<int> bit(N + 1);

    vector<pair<int, int>> lohi(N);
    vector<list<int>> bg(N + 1), ed(N + 1);
    REP(i, N) {
        bool md = false;
        int lo = 0, hi = N;
        if (i == 0) {
            md = (A[i + 1] < A[i]);
        } else {
            md = (A[i - 1] < A[i]);
        }
        if (md) {
            hi = N;
            if (i) chmax(lo, A[i - 1] + 1);
            if (i + 1 < N) chmax(lo, A[i + 1] + 1);
        } else {
            lo = 0;
            if (i) chmin(hi, A[i - 1]);
            if (i + 1 < N) chmin(hi, A[i + 1]);
        }
        lohi[i] = make_pair(lo, hi);
        bg[lo].push_back(A[i]);
        ed[hi].push_back(A[i]);
    }

    uint64_t ret = 0;
    REP(a, N) {
        for (auto b : bg[a]) {
            bit.add(b, 1);
        }
        for (auto b : ed[a]) {
            bit.add(b, -1);
        }
        auto [l, r] = lohi[pos[a]];
        ret += bit.sum(l, r);
    }
    cout << (ret - N) / 2 << '\n';
}
0